Reading RS232 data with arduino uno

The Project: To indicate whether a weight on the scale is over or under 8 Oz.

The basic plan is to hook up a scale to an Arduino uno and use an LED to indicate if it is above or below the ideal weight.

My current setup is a Tor Rey L-Pc 40L scale (TORREY L-PC SERIES USER MANUAL Pdf Download | ManualsLib) which has an RS-232 port. I have connected the RS232 port to a MAX 232 Converter.
I have taken the rx line and connected to pin0 on aruduino uno and the V+ is connected to 3.3 and gnd to gnd.

Using this code

I get this result

How do I get to some data that I can sort out. I only want to know the active weight on the scale? Many thanks!

1 Like

Sorry, but Arduino UNO has pins 0 and 1 already taken by the USB connection to your PC. You cannot use those pins for more than what they are designed for.
Look into using software serial for your scale communications.
Paul

Use the Tx line of the RS232 and feed it to the Rx pin on the controller.
The "ear" of the RS232 doesn't talk....
To be sure, post a drawing. Use code tags, not artworks, when posting code in the future.
Take a deeper look how mySerial.read works, what it delivers. The output You show looks like a type mismatch.

The data may make more sense if you use:
Serial.print(char(mySerial.read());

You are seeing the ASCII codes that the scale is sending. The char() make it print the characters instead of the codes.

The serial input basics tutorial may be of interest.

Screenshot images of code are not the optimal way to post code. Read the forum guidelines. Post your code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

Well that is “ NEG. “ in ASCII, what do you have on the scale whilst getting this?

If you haven’t, put some weight in the scale, gather everything between two “D” print lines. And record the weight.

Do that for a few different weights on the pan.

Note whether the repeated sequence between Ds is consistent for a stable weight on the pan.

Maybe we get lucky and see what it is saying.

@groundFungus brilliant, that’s why you get the big bucks. :wink:

a7

Thanks paul!
I tried putting the Tx line into pin 10 on the arduino and changed my code to say " SoftwareSerial mySerial(10,11)". I got a similar response in my serial terminal. Is that what you meant for software serial? Thanks

That was brilliant Im getting a real value!
You are a hero!!!

Thanks, guys. I will endevor to remain modest.

3 Likes

This may be a dumb question ( I'm new to this stuff)


I now have this result, do you have suggestions for just taking out the number part of that so I can do my simple calculation.

First you need to read the data into a variable. Easiest would be to read into a string and then use atof()* to convert the string into a number that you can use in calculations. The serial input basics tutorial that I linked will help with reading the string. I will work on an example for you.

  • edit: Corrected atoi to atof to return a float instead of an int.

Code to read the scale and return a number (float data type) that can be used for calculations using methods from the serial input basics tutorial and software serial. Untested.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,-1);  // -1 if not using TX to save a pin

const byte numChars = 16;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

float number = 0;


void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>");
   mySerial.begin(9600);
}

void loop()
{
   recvWithEndMarker();

   if (newData)
   {
      showNewData();
      number = atof(receivedChars);
      Serial.print("the number is = ");
      Serial.println(number);
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\r'; // 0x0d carriage return
   char rc;

   while (mySerial.available() > 0 && newData == false)
   {
      rc = mySerial.read();
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   Serial.print("This just in ... ");
   Serial.println(receivedChars);   
}

That worked perfectly! Can not thank you enough!!!

1 Like

The standard Arduino method Serial.readStringUntil( ) replaces most of the code above with a single statement

Strings provide protection against buffer overflow due to coding mistakes.

You could also look at Arduino Software Solutions for other ways to read and parse from Serial inputs.

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