Reading rs232 printer output on some industrial equipment with photon

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).

Ok I changed my data type to unsigned char

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?

Change your serial read code to:

void loop() {
    // read from port 1, send to port 0:
    while (Serial1.available())
    {
      char c = Serial1.read();
      Serial.write(c);
    }
}

You were both double-reading (dropping a character), as well as not reading fast enough to get full lines.

I hope that helps!

Thanks,
David

Actually it is an int and if you Serial1.read() with nothing available you’ll get (int)-1.

int USARTSerial::read(void)

And @Dave in order to echo the incoming data Serial.write() would be the way to go.

2 Likes

This is my new code

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”.

Have you noticed in Dave’s response that he used while() instead if if()?
He’s also provided a reason why.

Also double check the baudrate of Serial1.begin() and your transmitter.

I didn’t! I thought he was only referring to my using Serial.read twice.

unsigned char inChar = Serial1.read();
Serial.print(Serial1.read());

Thank you for pointing out that oversight.

@smn8600, try this:

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().

1 Like

That’s the reason. Without the loop you’ll be reading at max. 1 byte per millisecond when connected to the cloud in single threaded mode.

1 Like

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);
    }

@smn8600, you either have the wrong baudrate or the protocol is not 8N1. How exactly is everything connected?

2 Likes

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, which airconsole model? How are the Serial1 inputs connected? Are you using a TTL-to-RS232 converter?

1 Like

If you actually connected the RS232 output direct you might have fried your pins.

Try to check the interface on your device by bridging RX to TX and send some data via USB and see if you get back what you send out.

1 Like

I’m using the XL.

What’s a TTL converter :grimacing:

It still seems to be working

@smn8600, have you tried flipping the RX/TX line on the Photon?

I tried flipping the wires and it doesn’t echo anything, if that’s what you are talking about

@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.