Serial Port (Rs -232) Connection in C++

C linux: Serial communication lost some first bytes when receive data

I used to work with RS485 bus a lot. And one problem that sometimes appeared was very similar to your. Because RS485 is half duplex bus, there is mechanism that switching receive mode and transmit mode on RS485 bus driver. And this was exactly cause of my problems.

When master device sent some data, slave was ready to reply (and replied) before bus driver (on master side) was switched to receive mode. This behavior ended with data loss.

May I suggest you to check, using oscilloscope, that slave sent data correctly? If so, you probably don't have too much options to do as possible solutions are:

  • Slave have to wait some time before sending reply to master.
  • Change HW, some RS485 driver that will be faster in switching modes or use different BUS.

Serial programming RS485

  1. Are you also setting the proper baud rate on the Windows side?
  2. Use an oscilloscope to check the actual data present on the wire(s). Debugging serial communications is what oscilloscopes were invented for. Almost. :)

Raising or lowering RTS on serial port (C++)

Take a look at EscapeCommFunction.

EscapeCommFunction(hPort, SETRTS);

The hardware handshaking must be disabled, i.e. dcb.fRtsControl should be set to something other than RTS_CONTROL_HANDSHAKE when calling SetCommState.

Serial port receives same bytes it just sent

This is the expected behaviour for a serial port. These were typically used to connect a terminal like a vt100 to a computer. When you type on the keyboard, the character is sent to the computer, and the serial port echoes it back to the vt100 screen where it is displayed.

If you run stty -a -F /dev/ttyTHS0 on the remote you will see the setting echo is on (before you ran your program on the remote). You can switch it off with stty -echo -F /dev/ttyTHS0, when it will show as -echo.

Typically, when serial ports are used for comms between computers, the application will set the port to raw, noecho. This is to avoid certain translations being done by the serial port driver, so the data arrives unchanged at the application.



Related Topics



Leave a reply



Submit