-1

I'm working on Linux system that using modbus rtu RS485 to send and receive data. My device is master and just send "request latest data"(8Bytes include 2Byte CRC) to slaver (from now is only 1 slaver) every 1second. when Slaver receive request, they will prepare data (71Bytes include CRC) and send back to Master. I can't see the source of Slaver because this is commercial product. Both of Master and Slaver using same baud rate 38400.

Result:

  1. When check communication between Master and Slaver, sometimes (average is 1-2hours) data from Slaver is lost some first Bytes, and first Byte that received has been modified with other value that sent from Slaver (sometime lost some first Bytes only)
  2. Sometime data from Slaver not come (timeout but do not receive any data). I tried to increase timeout by 500ms or 1sec but still occurs without any change
  3. I tested Slaver by communication with Teraterm and there is no error like above. Data that sent and received are OK. With Master, I also tested with Teraterm and there is no error.
  4. When I try to catching data while Master and Slaver send & receive data, when problem are occurred(not receive any data or lost some first bytes) on both of Master side and on my PC side (try to catching data byte Teraterm on PC).

I believe that problem is on Master side, and maybe on serial port setting, but I don't know where were wrong. Please help.

Sorry for my poor English!

user1234
  • 25
  • 9
  • 1
    You have only posted fragments of code rather than a minimal & complete example. Return codes from all syscalls are not properly handled. Numerous identifiers are undefined, e.g. `assW_buf` in `while( 0 < read( fd, &buf[0], sizeof(assW_buf)) );` Baudrate is set in bogus `new_options` structure. Termios initialization is improper and unreliable by using a zeroed-out structure. See [Setting Terminal Modes Properly](https://www.gnu.org/software/libc/manual/html_node/Setting-Modes.html) and [Serial Programming Guide for POSIX Operating Systems](http://www.cmrr.umn.edu/~strupp/serial.html) – sawdust Oct 13 '21 at 22:30
  • I believe that in this case zeroed-out Termios structure is no problem, because I was referred your link but there is no improvement. The problem may not be in software. Sorry about undefined variable in sourcecode, that is typing mistake, no problem in program and please ignore it. – user1234 Oct 14 '21 at 01:35
  • 1
    *"The problem may not be in software. "* -- Then if the problem is in hardware, why does the problem not occur when you use Teraterm? A general rule for troubleshooting: if substituting X with Y removes the symptom, then suspect X as the cause, not Z. But if you do believe it's HW as you commented below, then your post is off-topic for this site. – sawdust Oct 14 '21 at 05:34
  • @sawdust: Thanks for your point out. In teraterm, the time of sending back data from received data from Master is longer (because of I used Marco and it take 30-50ms to process and send back data) than Slaver device. I was added log and see that sometime slaver sendback data less than 1ms. And as my comment below, I add delay timing on Slaver and see improvement. I need to known why switching receive mode and transmit mode on RS485 bus driver was take long time as usually to avoid or fix it. Can you help me? – user1234 Oct 14 '21 at 06:25

1 Answers1

1

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.
Vladislav
  • 26
  • 2
  • Thank for you answer. And I believe that you are right because of I tried to add delay 5-10ms before sending data back to Master on Slaver side and see improvement but still occurs. There is one things that I do not understand is Why sometimes bus driver switched to receive mode too late? I believe that if normally switched to receive mode, there is no problem. Maybe other interrupt or somethings that can effect to switch mode. I cant change hardware, I need do some retry send and receive in software to avoid this problem but I need to know root cause before do anything. Please help. – user1234 Oct 14 '21 at 01:29
  • To be honest, I have no idea. It depends on what HW are you using to connect RS485 to your computer. It may also depend on SW you are using. These things are usually constructed like USB to UART (for example FT232) and then UART to RS485 driver. These RS485 driver have RX & TX to connect UART, A & B to connect RS485 and direction pin that is usually driven by FT232. – Vladislav Oct 14 '21 at 07:54
  • Thanks for your help. I will try to using oscilloscope to determine that problem on which side. – user1234 Oct 15 '21 at 00:53