Go Down

Topic: Problem communicating with device via serial, signal formatting issue (Read 176 times) previous topic - next topic

lasbainas

Hello,

I want to communicate with a sensor that uses RS232 protocol, and I am having trouble in formatting the signal properly. Thanks for reading and hope you can help!

I am using an Arduino Mega 2650. It is connected to my sensor using RX and TX pins. It is also connected to my PC on this same RX and TX pins with a USB-Serial adapter.

I am using a serial monitor software on my PC to see what is going through the RX,TX pins.

To read the sensor, I need to send to it the following command ~~~~~~031D0000000011\r  , with 7 Databit, 2 Stopbits and no parity.

I can read my sensor if I send this command from my PC through the USB-Serial adapter. The serial monitor software sees this:

Code: [Select]

#        Time                     Function     Direction   Status               Data                                                            Data (chars)         Data Length    Req. length        Port

27773    17/10/2018 07:42:27      IRP_MJ_WRITE DOWN                       7e 7e 7e 7e 7e 7e 30 33 31 44 30 30 30 30 30 30 30 30 31 31 0d    ~~~~~~031D0000000011.        21             21             COM8  
27774    17/10/2018 07:42:27      IRP_MJ_WRITE UP       STATUS_SUCCESS    7e 7e 7e 7e 7e 7e 30 33 31 44 30 30 30 30 30 30 30 30 31 31 0d    ~~~~~~031D0000000011.        21                            COM8  


I want to achieve the same, sending the signal from my Arduino board. For this, I have implemented this code:

Code: [Select]


String outString;

void setup() {
  
Serial.begin(9600, SERIAL_7N2);

  outString = "~~~~~~031D0000000011\r";
}

void loop() {

  Serial.print(outString);
  delay(2000);
}



In the Serial monitor software in my PC, I see this:

Code: [Select]

#        Time                     Function   Direction     Status              Data     Data (chars)  Data Length  eq. length        Port

28100    17/10/2018 07:44:00      IRP_MJ_READ  DOWN                                                                  8065             COM8  
28101    17/10/2018 07:44:00      IRP_MJ_READ  UP         STATUS_SUCCESS    7e 7e 7e 7e     ~~~~         4                            COM8  
28114    17/10/2018 07:44:00      IRP_MJ_READ  DOWN                                                                  8061             COM8  
28115    17/10/2018 07:44:00      IRP_MJ_READ  UP         STATUS_SUCCESS    7e 7e 30 33     ~~03         4                            COM8  
28121    17/10/2018 07:44:00      IRP_MJ_READ  DOWN                                                                  8057             COM8  
28122    17/10/2018 07:44:00      IRP_MJ_READ  UP         STATUS_SUCCESS    31 44 30 30     1D00         4                            COM8  
28136    17/10/2018 07:44:00      IRP_MJ_READ  DOWN                                                                  8053             COM8  
28137    17/10/2018 07:44:00      IRP_MJ_READ  UP         STATUS_SUCCESS    30 30 30 30     0000         4                            COM8  
28150    17/10/2018 07:44:00      IRP_MJ_READ  DOWN                                                                  8049             COM8  
28151    17/10/2018 07:44:00      IRP_MJ_READ  UP         STATUS_SUCCESS    30 30 31 31     0011         4                            COM8  
28164    17/10/2018 07:44:00      IRP_MJ_READ  DOWN                                                                  8045             COM8  
28165    17/10/2018 07:44:00      IRP_MJ_READ  UP         STATUS_SUCCESS    0d                           1                            COM8  



It seems my message is "split in blocks" and the sensor does no react... but I do not really have the background to understand this, so if anyone could help me understand what is going on and how to fix it, I would be very grateful.

Cheers!!

Gorka

Robin2

Is the device you want to talk to using RS232 voltage levels? If so you need to convert them to TTL voltage levels for an Arduino or you risk damaging the Arduino. You can use a MAX232 chip to do the conversion.

Post a link to the datasheet for your sensor.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R
Serial Input Basics - simple reliable ways to receive data.
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

Besides the RS-232 problem, why don't you use one of the spare serials of the Mega for your sensor?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

lasbainas

Thank you for both answers, I will try to clarify what you asked:

I can read communicate with my sensor from the PC with a USB-Serial converter (UPort 1150), I guess you can find the info on voltage level from here: Datasheet

The setup I explained is for debugging. I have connected the PC and the sensor to the same RX-TX pins on my arduino so that I can monitor what I am really sending to my sensor:
  • When I send the command from the PC, I can see it in the monitor as shown below, and it works
  • When I send it with Arduino, both  the PC and the Sensor can see it, so I can see with the monitor what the sensor is really getting

With this arrangement, the serial monitor shows that the messages sent in each case are somehow different, and since one works and the other does not, I am trying to understand how can I adapt my Arduino code so that I can generate the same message.

I will check too the info on cstrings.


Robin2

Thank you for both answers, I will try to clarify what you asked:

I can read communicate with my sensor from the PC with a USB-Serial converter (UPort 1150), I guess you can find the info on voltage level from here: Datasheet
That is not the datasheet for the sensor that is generating the serial data.

Also it seems to be an RS232 to USB converter rather than a TTL to USB converter so my first comment in Reply #1 still stands.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

The setup I explained is for debugging.
I have connected the PC and the sensor to the same RX-TX pins on my arduino so that I can monitor what I am really sending to my sensor:
I can not see how creating problems like bus contention (three devices on one serial) helps in debugging.

I would try to debug the target configuration or subsets of it.

If you want to monitor a serial communication nobody hinders you to listen with the remaining two
serial ports to both directions of a communication.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

lasbainas

That is not the datasheet for the sensor that is generating the serial data.

Also it seems to be an RS232 to USB converter rather than a TTL to USB converter so my first comment in Reply #1 still stands.

...R
I am sorry but I do not have the datasheet of the sensor. However, the sensor accepts as good the request signal sent from the converter, so I use this as reference. My converter works with RS-232/422/485 protocols... Can I assume that when I am sending a signal from my PC through it (COM8), it is generating the signal as RS232?

Then, when I send a signal from my arduino (TTL), it goes through the converter, and the monitor on the same COM8 port still sees the message, identifying the characters properly but splitting them in blocks. How does it read if it is expecting RS232 voltage levels?

I am puzzled  :smiley-confuse:

I can not see how creating problems like bus contention (three devices on one serial) helps in debugging.

Lack of knowldege/experience/timetothink  :(

If the problem is in the voltage level, I assume I can use something like this?

https://www.sparkfun.com/products/11189


Thanks!!!

Robin2

I am sorry but I do not have the datasheet of the sensor.
You have not even told us the technical name of the sensor. Maybe there is stuff printed on it and you could post a photo of it?

Quote
Can I assume that when I am sending a signal from my PC through it (COM8), it is generating the signal as RS232?
That seems reasonable

Quote
Then, when I send a signal from my arduino (TTL), it goes through the converter, and the monitor on the same COM8 port still sees the message, identifying the characters properly but splitting them in blocks. How does it read if it is expecting RS232 voltage levels?
The "through the converter" seems very unlikely.

Make a simple diagram showing exactly how you have everything connected and post a photo of the diagram. See this  Simple Image Guide

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

lasbainas

I have found the manual of the device I am trying to read. On page 29, I am connected to port 3. I send to it a command (page 32), and it answers with the measurement result.
Sensor Manual.
I have sketched the connections I used to get the results on the serial monitor software I mention in my first message.

Now I see quite clearly that I need a TTL to RS232 converter between the arduino and my device. But there are still two things that I do not understand:
  • Why does the serial monitor on my PC read the message I sent from Arduino
  • Why is this message split in blocks of four characters, and if this could be a problem for my sensor to accept it

Thank you for your help!

Robin2

But there are still two things that I do not understand:
  • Why does the serial monitor on my PC read the message I sent from Arduino
  • Why is this message split in blocks of four characters, and if this could be a problem for my sensor to accept it

I don't know.

I have no experience of sending messages from an Arduino to a PC through an RS232 to USB converter.

I repeat what I said earlier - RS232 voltages may damage your Arduino.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up