Weight scale data via MAX3232 to Arduino

Hi All,

I have this scale -
chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/http://www.gandg.com.cn/en/tbc258_upfile/2021281510824740.pdf

which is inbuilt Rs232 output port.

I'm struggling to receive scale data via MAX3232 to Arduino serial port.

My project is to create a small water filling system from this scale and a pump. So according the scale data pump power ( relay ) will cut of the power.

Please help me on this code, Thank you all.

Some general advice:
The description of the RS232 protocol in the manual is terrible. If you are already very familiar with using Arduino serial comms to talk to other devices then maybe debugging the protocol using an Arduino is viable. On the other hand, if using Arduino serial comms is relatively new to you, then it might be better to get yourself a USB-RS232 device for your PC and try talking to the scale using a terminal emulator. That should allow you to work out the correct way to send commands to the scale, and see exactly how it responds. When you have that sorted out you could move on to writing the Arduino code to do what you were doing with the terminal emulator.
If you want specific help with your code:
Then you really need to post your code, details (ideally a schematic) of how you have the serial link from the scale wired to the Arduino, what you have tried doing so far, and what works / what doesn't work.

Your link contains a security risk and will not download. Please copy the relevant page on the communication to this thread so we can see it.

I've just been reading the manual. I didn't get a security warning from Malware Bytes Browser Guard. As I've already got the manual in my downloads folder I'll attach the whole manual. It's not very big. Section 8 page 15 is the start of the serial communications details. I think they may have got bits and bytes mixed up.
2021281510824740.pdf (2.6 MB)

I have tried 3 codes, similar to each, but didnt work
1)

#include<SoftwareSerial.h>
SoftwareSerial SUART(0,1);  

void setup()
{
     Serial.begin(9600);
    SUART.begin(9600);
}

void loop()
{
     SUART.print('A');    //sending A using SUART port
     byte n = SUART.available();
     if(n != 0)
     {
           char x = SUART.read();
           Serial.println(x);
     }
   delay(1000);
}




#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>
uint8_t recv_char;
char recv_str[20];
int i=0;

SoftwareSerial mySerial(6, 7); // RX, TX
#define LED 8
void setup()
{

Serial.begin( 2400 ); //Set the baund rate as 2400 for arduino serial
Serial.println("USB Serial is ready");
mySerial.begin( 2400 ); // set the baund rate for the SoftwareSerial port
pinMode(LED, OUTPUT);
digitalWrite(LED,LOW);
}

void loop()
{
if ( mySerial.available() )
{
recv_char = mySerial.read();
if(recv_char == '\r'){
recv_str[i] = '\0';
if(!strcmp(recv_str, "ON")){
digitalWrite(LED,HIGH);
Serial.println("high");
}
if(!strcmp(recv_str, "OFF")){
digitalWrite(LED,LOW);
Serial.println("low");
}
Serial.println(recv_str); // Write the serial from MAX3232 to Arduino serial
i=0;
memset(recv_str, 0, sizeof(recv_str));
}
else{
if(recv_char == '\r' || recv_char == '\n'){
}
else{
recv_str[i++] = recv_char;
}
}

}

if ( Serial.available() )
{
recv_char = Serial.read();
if(recv_char == '\r'){
recv_str[i] = '\0';
mySerial.println(recv_str); // Write the serial from MAX3232 to Arduino serial
i=0;
memset(recv_str, 0, sizeof(recv_str));
}
else{
if(recv_char == '\r' || recv_char == '\n'){
}
else{
recv_str[i++] = recv_char;
}
}

}
}

3)`void setup() {
// Initialize serial communication at the baud rate of the weighing scale
Serial.begin(9600); // Change 9600 to the baud rate of your weighing scale
}

void loop() {
// Check if data is available to read
if (Serial.available() > 0) {
// Read the incoming data
String data = "";
while (Serial.available() > 0) {
char inChar = (char)Serial.read();
data += inChar;
delay(5); // Small delay to ensure the complete data is read
}

// Print the data to the Serial Monitor
Serial.println("Weighing Scale Data: " + data);

}
}`

Your post is hard to read. Can you edit it to put the code in code tags?
Code version 1:
It looks like you are trying to use pins 0 and 1, which I wouldn't expect to work because the hardware Serial uses those pins.
What do you expect to happen when you send 'A' to the scale? It seems to me that most commands start with Esc (Ascii code 0x1B), e.g. Esc+p
"1BH+70H(ACSⅡ code p): print(scale required to send date once);"
Code version 2:
This looks better in the sense that you are now using 2 pins which aren't 0 and 1.
You changed the baud rate to 2400 and the scale's manual says the default is 9600. Why did you change the baud rate?
I can't see where you are sending any command to the scale.
"mySerial.println(recv_str); // Write the serial from MAX3232 to Arduino serial"
That's not doing what the comment says.
The code looks overly complicated, but I can't easily work out what you intended it to do.
Code version 3:
I'm not sure what you are trying to do here. There's a comment saying you are initialising Serial to the baud rate of the scale, then in loop() there's a comment saying you are using Serial to print to the serial monitor.
My current conclusion
Version 2 looks most viable. Put the baud rate back to 9600 for software serial. Try sending Esc + p (two characters) and print out what comes back one character at a time. Don't try to parse or reformat what you receive.

Are you using one of these modules?

The pin names are a little confusing

If yes then you need to connect the module RXD to Arduino software serial RX and module TXD to Arduino software serial TX
You will also need a crossover cable to connect the scale to the TTL converter

I tried to make an example using loopback (a jumper between mySerial tx pin and mySerial rx pin). I've just found out that this can't work because the software serial is 1/2 duplex, it was a schoolboy error :-). Software serial can either send or receive, but not both at the same time. You might be able to get it to work, but it would remove some uncertianty if you used an Arduino board with > 1 serial port, such as Leonardo, or Mega2560. At least while you get it working. Once working you could try switching to using software serial on a board that only has 1 hardware serial.

looking at the manual, there is no command 'A' so even if you had the baud rate correct, nothing would happen.

You have to send the ESC byte (0x1B or 27) followed by a single char
'p' to get the results from the scale
'q' for calibration
'r' for count
's' for unit conversion
't' for tare
'u' for backlight

so try opening your serial port at 9600 baud and sendning ESC+p

mySerial.write(27); // send ESC
mySerial.print('p');

and then see what is returned.

Also the reply from the command 0x1b 0x70 is the 41 byte sequence ( it says bits but are bytes )

WT□□□12.345□kg↓UW□□12.34□g↓QT□□1000□Pcs↓←41 bits

The left down arrow is a CR the down left arrow is a LF
The UW is the unity weight on which the number of pieces is calculated ( QT )

For chinese, writing manuals is a waste of time... no need to complete sentences, examples... let you imagination fly!

To display accumulation
MW□□12345.6□kg↓ 15 bits
MN□□□12□□□□↓ 12 bits
MQ□12345□Pcs↓← 14 bits

Thank you for all the support, I brought the reading up this level

,`#include <SoftwareSerial.h>
SoftwareSerial SUART(6, 7);

void setup() {
Serial.begin(9600);
SUART.begin(9600);

}

void loop() {
char inData[21]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

SUART.print('A'); //sending A using SUART port
byte n = SUART.available();
if (n != 0) {

  if (index <20)  // One less than the size of the array
  {
    inChar = SUART.read();  // Read a character
    inData[index] = inChar;  // Store it
    index++;                 // Increment where to write next
    inData[index] = '\0';    // Null terminate the string
  }
}



  Serial.println(inData );

  delay(1000);

}

and reads like this
`

and my scale details as follows, ( complete manual - TJ-Y.docx (gandg.com.cn) )


I need to get this conversion array which is really complicated for me. Can someone help me with the code. Thanks alot

You are sending command 'A' to the scale ( and the scale is responding in some non understandable mode ).
What is command 'A' as it is not documented?
Did you try esc+p ( as explained in post #9 ), which will reply with a 41 byte sequence including weight?

It doesn't look like you took anybody's advice. Try this code to see if you get the weight results returned.

#include <SoftwareSerial.h>
SoftwareSerial SUART(6, 7);

void setup() {
  Serial.begin(9600);
  SUART.begin(9600);

  Serial.println("Sending ESC+p");
  delay(1000);

  SUART.write(0x1B);  // ESC
  SUART.print('p');   // ESC+p to get weight
}

void loop() {
  if (SUART.available() ) {
    char inChar = SUART.read();  // Read a character
    if ( inChar < ' ' | inChar > '~' ) {
      Serial.print( "Hex: 0x");
      if ( inChar < 0x10 ) Serial.print('0');
      Serial.println(inChar, HEX );
    }
    else {
      Serial.println(inChar);
    }
  }
}

Note that it will only run 1 time since the command is send in setup().

I see from other post you are testing different scales, different converters... with little success.
So, my opinion is 'stick with one solution' ( no need to go forth and back... ) use this scale.

  • Did you test the code from post #13 ( it is a good starting point )?
  • does it print the 'informations' from the scale ( press the reset button to execute it multiple times )?
  • attach here the output of the serial monitor