Read Rs232c data with arduino

I'm trying to get my Arduino UNO to read data from my weight indicator model XK3190-A12(E) and display them on the Arduino IDE Serial Monitor, but a lot of the code scripts I've tried don't work. Here's two of our setups with and without a converter:


Here's one of the codes I tried for both of them:

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
String Data = "";
void setup()  {
    // Define pin modes for TX and RX
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    Serial.begin(9600);
    
    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
}

void loop() {
    if (mySerial.available() > 0) {
        Serial.println("mySerial is available");
        delay(3000);
        
        char character = mySerial.read(); // Receive a single character from the software serial port
        Data.concat(character); // Add the received character to the receive buffer
        if (character == '\n')
        {
            Serial.print("Received: ");
            Serial.println(Data);

            // Clear receive buffer so we're ready to receive the next line
            Data = "";
        }else{
          Serial.println("No data read.");
          }
    }else{
      Serial.println("mySerial is not available");
      delay(3000);
    }
}

but the output is:

It reads as available once but doesn't read any data then becomes unavailable completely after.

Here's the manual for the weight indicator:
A12(E)(OC).pdf (zemiceurope.com)

And some screenshots of the instructions the manual above provides:




Here's a photo of setup 1:

Setup 2 is difficult to photograph clearly.

Hope to get more insight on this!

1 Like

Well, the first thing I must say is: if the device is RS232 you must use the RS232-TTL converter because Arduino has TTL levels. For more information, see THIS for example.

Then, the code. Before talking about communication data structure, you need to be able to get the data and at least print it over Serial Monitor.
If you do this:

    if (mySerial.available() > 0) {
        Serial.println("mySerial is available");
        delay(3000);        
        char character = mySerial.read(); // Receive a single character from the software serial port
        Data.concat(character); // Add the received character to the receive buffer
        if (character == '\n')
        {
            Serial.print("Received: ");
            Serial.println(Data);
            // Clear receive buffer so we're ready to receive the next line
            Data = "";
        }else{
          Serial.println("No data read.");
          }
    }else{
      Serial.println("mySerial is not available");
      delay(3000);
    }
}

Ignoring those senseless 3-seconds delays, you'll get "No data read" every time you receive any character different than '\n', so the output you posted here shows you received "something". And avoid using "String" variables, you should learn using "C strings" (aka "char arrays").
Give this version a try (not tested, just drawn on the fly):

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

// Buffer
char Data[20];
byte pos = 0;

void setup()  {
// NOT NEEDED!
//    pinMode(rxPin, INPUT);
//    pinMode(txPin, OUTPUT);
  Serial.begin(9600);
    
  // Set the baud rate for the SoftwareSerial object
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available() > 0) {
    char character = mySerial.read(); // Receive a single character from the software serial port
    // End of packet? ('\n' or LF, packet byte #15)
    if (character == '\n')
    {
      Serial.print("Received: ");
      Serial.println(Data);
      // Clear receive buffer so we're ready to receive the next line
      pos = 0;
      Data[pos] = 0x0;
    } else {
      // Ignore '\r' (CR or 0x0d, packet byte #14)
      if (character != '\r') {
        // Add the received character to the receive buffer
        Data[pos++] = character;
        Data[pos] = 0x0;
      }
    }
  }
}

Use the converter and power it properly:

  • 5V for the Arduino Uno (TTL) side
  • e.g. 12V for the RS-232 side - see converter data sheet

I don't see any of the required transistors on your converter picture. Please provide a link to the data sheet.

The 3 second delays are mainly just for my eyes to keep up with tracking if it reads as available if ever. I tried the code with setup 2 since it had the Rs232 to TTL converter chip but it doesn't display anything.

Here's the data sheet I could find on the converter:

Use 5V with a 5V Arduino UNO.

Connected it to 5V and used @docdoc's code, the serial monitor still showed nothing.

The String type is not usable on a UNO. Use C strings (char[]) instead.

The picture you showed of the converter does not show ANY IC device. Is there one on the other side of the circuit board? If so, what is it?

On the other side, it's this:
image

Used @docdoc's code that did use C strings, still showed nothing.

I'd check the converter, are you experienced in electronics?

There was a thread very similar to this one a few days ago.

https://forum.arduino.cc/t/digital-scale-rs232-max3232-with-arduino-mega/1259275

The reason that the scale would not communicate with the Arduino, was that the GND pin inside the scale was connected to a different pin to that which was shown on the schematic.

It might pay you to look inside your scale, and check the connector wiring.

I'd check the source with a LED and series resistor. Does the weight indicator show anything with no weight on it? Surely the OP has something to be weighed.

Nothing at all? My first concern is when you started testing: if you connected RS232 directly to Arduino, the risk is an I/O pin damage... So DON'T.
Try adding a few more debug prints (and change the pins, just to make sure 10 and 11 aren't working anymore):

#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 9

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

// Buffer
char Data[20];
byte pos = 0;

void setup()  {
  Serial.begin(9600);
  // Set the baud rate for the SoftwareSerial object
  mySerial.begin(9600);

  Serial.println("STARTED");    
}

void loop() {
  if (mySerial.available() > 0) {
    char character = mySerial.read(); // Receive a single character from the software serial port
    Serial.print("Byte: "); Serial.println(character, HEX);
    // End of packet? ('\n' or LF, packet byte #15)
    if (character == '\n')
    {
      Serial.print("Received: ");
      Serial.println(Data);
      // Clear receive buffer so we're ready to receive the next line
      pos = 0;
      Data[pos] = 0x0;
    } else {
      // Ignore '\r' (CR or 0x0d, packet byte #14)
      if (character != '\r') {
        // Add the received character to the receive buffer
        Data[pos++] = character;
        Data[pos] = 0x0;
        Serial.println("Stored.");
      }
    }
  }
}

Remember to power the TTL converter with 5V, not 3.3V, and see what the Serial monitor reports (just in case, make a copy/paste here of the output).

But check also the weight indicator before, just to make sure it is set to 9600 baud:

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