Send and receive data from a sensor on RX1,TX1

Hi to all,

I have a sensor that communicates over the RS232.
If I connect the sensor directly to the pc and then I use a terminal software to send simple commands ending with , like "get all" then I get the full answer from the sensor.

I would like to do the same with Arduino MEGA in order to store the answer from "get all" and send it over a network.

I did this wiring:
RX(sensor) -> TX1
TX(sensor) -> RX1
GND(sensor) -> GND(arduino)

and then I tried to use this simple code:

int i=0;
void setup() {

  // initialize both serial ports:

  Serial.begin(115200);

  Serial1.begin(115200);
  
}

void loop() {


    if(i==0){
    Serial1.write("get all");
    Serial1.write('\r');
    }

  // read from port 1, send to port 0:

  if (Serial1.available()) {

    int inByte = Serial1.read();

    Serial.write(inByte);

  }
 i=1;

}

However, I get nothing as answer.
I can't understand if the error is with the carriage return or with something else.
Each command must be sent to the sensor by ending it with a carriage return.

And with line feed "\n"?

You can send a get all from the Serial monitor and send that to the sensor. What happens then?

If I connect the sensor to the USB port of the pc and then I open the serial monitor and I type get all, then I get the answer.

If I try to do the same while using arduino, I get nothing.

Try this ...

void loop()
{
  while (Serial.available() > 0)
    Serial1.write(Serial.read());

  while (Serial1.available() > 0)
    Serial.write(Serial1.read());
}

Then try different line ending options in the monitor.

Serial monitor add cariage return AND line feed if you don't uncheck them.

Send the same thing to the arduino and forward it to the sensor.

edit: like red_car also say.

Hi,
What type and if possible link of your sensor?
How are you connecting your sensor to the PC?
Are you using some interface module?
If yes, which model?
Post a schematic (even if done by hand) of your connection with the PC and the arduino mega.

1 Like

It's the same, I can't read anything.
However, I see the TX led on arduino blinking so it receives something for sure but I cannot see it in the terminal!

Post your sensor comm specs. Almost certain it NOT 115200 ...

1 Like

It is 9600 8N1.
I attached the datasheet, the sensor is quite expensive since it is used for marine applications.
SVS-603HR User Manual.pdf (4.6 MB)

Then you need to talk to it at that speed.

Serial1.begin(9600);

2 Likes

I already tried that but it doesn't work.

could you give details of the RS232 shield you are using to communicate with the sensor?
upload a schematic of the wiring showing how you connected the Mega to the RS232 shield and the shield to the sensor
to test the RS232 shield can you do a loopback test? connect pins 2 and 3 on the 9 pin Dtype connector - character transmitted from the Mega should echo back
check the voltages on pins 2 and 3 of the 9pin Dtype connector - should be -12 volt when communications are idle

1 Like

I'm not using any RS232 shield, I'm directly using the RX1 and TX1 pins available on the Arduino MEGA as explained in the first post. Why should I use a shield in this case?

Because RS-232-C is +-12V and the Mega is only expecting 0-5V. You have to match baud rate (9600), format (8N1), AND voltage (0-5V vs +-12V) for it to work.

1 Like

I assumed when post #1 indicated connecting a RS232 device to a Mega a RS232 shield or equivalent circuit would be used to convert the -12 to +12 volt RS232 signals to 5V to 0v for the Mega
connecting directly stands a good chance of destroying the Mega and possibly the RS232 device

1 Like

Um... did you read this bit in the user manual...

"The RS232 output is at true RS232 levels (±10V). In order to connect directly to a micro controller, you will need to use an RS232 transceiver."

1 Like

You are totally correct. I did a mistake.

I have a rs232 shield based on the max3232 chip: should it be enough in this case, is it correct?

yes - can you upload a schematic showing how you connected the shield?
also details of the shield?

I'm using this adapter:


based on max3232

I still haven't done the test, however, I'm planning to connect TXD, RXD and GND from the adapter to Arduino MEGA RXD, TXD and GND.
I do not know if I have to swap TXD and RXD on Arduino, but I can try this in case it doesn't work with the first configuration.

Is anything else that I need to connect?

this worked for me
Mega RX1 (pin 18) to RS232 TXD
Mega TX1 (pin 19) to RS232 RXD
Mega 5V to RS232 VCC
Mega GND to RS232 GND

Mega Serial1 test program (change Serial1 baud rate to suit target)

// Arduino Mega serial1 test

// mega pin 18 is Tx
//      pin 19 is Rx
// for loopback test connect pin 18 to pin 19

// for RS232 shield connect pin 18 to Tx and pin 19 to Rx
// for loopback test connect 9 pin D connector pins 2 and 3

unsigned long time;

void setup() {
  Serial.begin(115200);   // initialise serial monitor port
  Serial1.begin(115200);  // initialise Serial1
  Serial.write("Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19\n");
}

void loop() {
  if (Serial1.available())        // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  if (Serial.available()) {       // read from Serial outut to Serial1
    int inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

a simple test is to connect RS232 9pin Dtype connector pins 2 and 3 (short with length of wire)
this forms a loopback test where characters entered on the serial monitor are echoed back to the display