Communication with Serial Device

Hello,

I am new to Arduino programming and I have trouble with a task I would like to perform. I have a scale which is able to communicate via serial port. For example, I can turn the scale on and off, or I can ask for the scaling value. The manual of the scale states that if I send the sign combination SS + CR + LF, I can turn the scale on or off. So I would like to write a quick program which does exactly that. I am using a Mega 2560 and I have the wiring from the scale with RX, TX and GND. I connected GND to Arduino GND for common ground, and then I connected RX and TX to TX0 and RX0. Now I have already some problems in understanding the difference between Serial.print and Serial.write. Whats the difference? The example code I am running and which does nothing, is as follows

void setup()
{
   Serial.begin(19200);

}

void loop()
{

   Serial.print('SS + CR + LF');
   delay(10000);

}

Is this code even remotely doing what it is supposed to? Is the wiring right? Do I have to state which TX/RX I chose?

Thank you for any help in advance!

could you share the link the the manual you are using?

I cannot see that code working but this might (UNTESTED, no idea if the baudrate you set for the device is correct either!)....

//Carriage Return (CR) character - '\r'
//Line Feed (LF) character - '\n'

char poweroff[4] = {'S','S','\r',\n'}; //SS + CR + LF command

void setup()
{
   Serial.begin(19200);

}

void loop()
{

   for(uint8_t i=0; i<4; ++i){
     Serial.write(poweroff[i]);
   }
   
   delay(10000);

}

please note that you probably will need a RS232 adapter to connected the scale to the arduino to match the signal levels

hope that helps

Unfortunately, it is in german, does that work for you? I will attach pictures out of the manual.

according to this the baud rate is 4800.

so you would need to change 'Serial.begin(19200);' to 'Serial.begin(4800);' in the code I shared is post #1.

hope that helps.

Thank you for your help! Unfortunately, it still does not work. There were minor mistakes in your code, but only a missing ' and a missing }. So after that fix, still nothing is happening. Right now I could imagine that the scale has some kind of setting which is not set up properly. I will consult the customer support of the scale and ask them if there is such setting. If I find out if thats the problem, I will post again.

Concerning the wiring, that should be right?

The shape of the connector in the image in Reply #3 (and the reference at the top of the image to "RS232C") strongly suggest that the scale operates at RS232 voltage levels rather than the TTL voltage levels that an Arduino uses. RS232 voltage levels can damage an Arduino - as well as being unintelligible.

You need an RS232 to TTL serial adapter such as a MAX232.

...R
Serial Input Basics - simple reliable ways to receive data.

Not a good idea to use TX0 and RX0, those are being used for the USB serial connection. The mega has three additional serial ports, use one of those.

@Robin2 thank you, I will take a look into the tutorial and check for an adapter.

@david_2018 And how do I implement which TX RX I want to use?

The extra serial ports are Serial1 (RX1=pin 19, TX1=pin 18), Serial2 (RX2=pin 17, TX2=pin 16) and Serial3 (RX3=pin 15, TX3=pin 14). Those ports are used the same as Serial (USB. RX0 and TX0).

Can you upload sketches (like Blink) to the Mega? If not, you may have damaged the board by connecting the high voltage RS232 signals.

loewph:
@david_2018 And how do I implement which TX RX I want to use?

Instead of Serial, use Serial1 for TX1/RX1, Serial2 for TX2/RX2, or Serial3 for Tx3/RX3. Each serial port has its own baud rate, so you can run the scale at 4800 baud and the serial monitor at 19200 simultaneously.

Alright, blink does compile and runs without any problems (LED blinks on the board), so the boards seems to be ok. I changed the code from Serail to Serial1, however, I do not get any output on the serial monitor any more. i picked the baud rate for Serial1 to 4800, that should not be a problem in this case, or should it?

//Carriage Return (CR) character - '\r'
//Line Feed (LF) character - '\n'
char poweroff[4] = {'S','S','\r','\n'}; //SS + CR + LF command
void setup()
{
   Serial1.begin(4800);

}

void loop()
{
   
   for(uint8_t i=0; i<4; ++i){
     Serial1.write(poweroff[i]);
   }
   
   delay(1000);

}

1. This is the connection that has been recommended in the previous posts of veterans among Scale, RS323TOTTLConverter, and MEGA.
uartEsZ.png
Figure-1:

2. Upload the following sketch(untested) and see if you get some response in the Serial Monitor.

void setup()
{
    Serial.begin(9600);
    Serial1.begin(4800);   //Bd of Scale
   //----------------------------------------
}

void loop()
{
   Serial1.print("SS") ;
    Serial.wrte(0x0D):     //ASCII code of CR
    Serial.write(0x0A);    //ASCII code of LF
   //---------------------------------------------

   while(Serial1.available()>0)
   if(n !=0 )
   {
         char x = Serial1.read();
         Serial.print(x);
    }
    Serial.println();
    delay(1000);
}

uartXY.png

uartEsZ.png

Unfortunately, no response on the serial monitor...

loewph:
Unfortunately, no response on the serial monitor...



Figure-1:

Testing the functionality of RS3232TOTTL Converter (tested):

  1. Remove the jumper/connecting wires between Scale and RS232 side of converter.
  2. Short Pin-2 and Pin-3 at the RS232 side of converter.
  3. Upload the following sketch. Check that Hello appears on Serial Monitor at 1 sec interval.
    smV.png
    Figure-2:

Sketch:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(4800);   //Bd of Scale
  //----------------------------------------
}

void loop()
{
  Serial1.print("Hello");
  while (Serial1.available() > 0)
  {
    char x = Serial1.read();
    Serial.print(x);
  }
  Serial.println();
  delay(1000);
}

Note: Please, retest Post#11 with the corrected diagram and revised code posted there.

uartT.png

smV.png