The printer output is set up with 80 columns, 8 data bits, 1 stop, no flow, no parity. I am simply trying to echo Serial1 to Serial at the moment, however when I do that I get very strange output in the serial monitor.
void setup() {
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available())
{
int inByte = Serial1.read();
Serial.write(inByte);
}
}
@smn8600, 8N1 is the default so no need to set it for Serial1. The return from Serial1.read() is a byte (unsigned char) not an int. Serial.write() will put out the raw byte to the console so anything not ASCII will print weird or do odd things (eg. 0x0A is linefeed).
void loop() {
// read from port 1, send to port 0:
if (Serial1.available())
{
unsigned char inChar = Serial1.read();
Serial.print(Serial1.read()); //changed from "write" to "print"
}
}
now the output resembles something like this
19991919191919199919191919
so do I have to convert this to ascii?
void loop() {
// read from port, send to port 1:
if (Serial.available())
{
unsigned char c = Serial.read();
Serial1.write(c);
}
// read from port 1, send to port 0:
if (Serial1.available())
{
unsigned char c = Serial1.read();
Serial.write(c);
}
}
if I send the word “test” to the electron, the electron echo’s “MF”.
void loop() {
uint8_t c;
// read from port, send to port 1:
while (Serial.available())
{
c = Serial.read();
Serial1.write(c);
}
// read from port 1, send to port 0:
while (Serial1.available())
{
c = Serial1.read();
Serial.write(c);
}
}
Each while() will empty the receive buffer before moving on, reducing any possible overrunning of the receive buffer when the background firmware runs at the end of loop().
I’m using the while loop now as to not miss any bytes. I’m still sending “test” and echoing “MF”.
// read from port, send to port 1:
while (Serial.available())
{
unsigned char c = Serial.read();
Serial1.write(c);
}
while (Serial1.available()) {
unsigned char c = Serial1.read();
Serial.write(c);
}
I connected an airconsole (get-console.com) device to an rs-232 printer output on industrial equipment set to 9600, 8 bits, 1 stop, no flow, no parity. The output looks great.
When I connect the electron with the same settings 8N1 whether I let it default, or force every setting e.g. Serial1.begin(9600, SERIAL_DATA_BITS_8 | SERIAL_PARITY_NO | SERIAL_FLOW_CONTROL_NONE | SERIAL_STOP_BITS_1);
it does not work. I get output, but it’s ugly.
I tried another thing just now. I changed the electron to 7 bits, 1 stop, no flow, even parity and I’m getting the same output. “test” echo’s “MF”. I have a feeling my Serial1.begin settings are not having any effect.
@smn8600, looking at the airconsole site, it’s unclear what your setup is. The airconsole is wirelessly connected to what (where’s the printer!)? The black RJ45-to-DB9 adapter is a NULL modem or a pass-through? I assume the Photon USB is connected to your PC running a serial terminal.