Interpreting serial from an old instrument

I am trying to use an Uno (but later a Nano RP2040) to read the UART output from an old current meter. If I connect to a Moxa Uport and open with CoolTerm, it looks just fine, and prints in seven 'packets' (one for each sensor):
In ASCII:
0896 .0021 .0061 .0703 .0000 .0155 .0894

When I just run the following on the Uno (same serial setup):

const int BUFFER_SIZE = 50;
byte buf[BUFFER_SIZE];

void setup() {
    Serial.begin(1200, SERIAL_8N2);
    }

void loop() {
 if (Serial.available() > 0) {
    // read the incoming bytes:
    int rlen = Serial.readBytes(buf, BUFFER_SIZE);
    for(int i = 0; i < rlen; i++)
      Serial.write(buf[i]);
      Serial.println();
      }
}

I get packets of very strange characters: boxes, arrows, letters, etc..

If I use Serial.print(buf[i]);, I get:
6129950330
6610025330
6125112330
61006101330
6666330
6103101101330
6129925312161

Any ideas on how to get from my feeble Arduino attempts to something like the CoolTerm output? I looked at the Hex in Coolterm, but that doesn't match up with the Arduino serial output, and there's always a new line (0D 0A) after the seventh packet.

Have you tried using the (OD OA) as a sentence marker?

Consider that at the end of every packet is period, full stop. If the code waited to start collecting info till after a period is received and collect info till period is received data synchronization may be achieved.

read about serial input basics, do a search, on this site for more info.

That looks like a wrong baudrate. Did you connect the signal GND?

It doesn't make much sense to wait for a first byte and then read many more bytes without waiting. I'd wait for the BUFFER_SIZE bytes available before calling readBytes().

Or you copy the characters into the buffer until you encounter a CR or LF, then print out the entire buffer.

Please format your code (CTRL-T) before posting. I thought that you print a NL after each single character, until I noticed that there is no opening brace after for(). Such things can be very confusing people which see your code for the first time.

0896 .0021 .0061 .0703 .0000 .0155 .0894

Is this complete, or missing a leading '.'?
But regardless,

Maybe, start in setup by looking for 0D 0A, then you're sync'd with the input stream and can simply stuff chars into buf until you catch another 0D0A.
But best would be to look at the Serial Basics for a foolproof guide to serial stuff, so you actually understand what's going on.

You could try to see what this does:

void loop()
{
  if (Serial.available())
  {
    uint8_t cc = Serial.read();
    if (cc == 0x0A) Serial.println(); //Line feed = new line
    else if (cc != 0x0D) //Skip carriage return
    {
      //Print as hex + space
      Serial.print(cc, HEX);
      Serial.print(' ');
    }
  }
}


The characters are sent as ascii code. Your buffer is byte, not char, so you are printing the numeric value of the ascii codes, not their text representation.

Edit - my mistake, you are using write not print.

Is the RS232 from the device, and are you using a converter?

I should have clarified, I only see the '0D 0A' hex in CoolTerm, it doesn't show up in the Arduino output. I tried your code here and get much of the same before, in a single line, e.g.
32 63 63 3 3 0 6 6 6 6 3 3 0 6 67 65 65 3 3 0 6 64 32 64 3 79 3D 6 C
and on and on...

It is RS232 from the device.

You know that RS-232 is the inverse of the UART signal?
And of a voltage swing (-12V to +12V) that can kill your Arduino!
An RS-232 to UART converter is highly recommended.

A UNO has only ONE hardware serial port and that is generally used to display information on the Serial monitor over the USB connection

You shouldn't connect your meter to the same port!

if you want to display onto serial monitor what is being read by the arduino, then you have to create software serial port to which your METER connects, and THEN output what was read from that port to serial monitor.

you can have a look at this example is you wish.

hope that helps...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.