simat / modbus-tk

Automatically exported from code.google.com/p/modbus-tk
Other
0 stars 0 forks source link

RtuServer request is received in two parts instead of one #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a RTU device connected to the serial port of my computer. The device is 
configured to write 32 coils (for example 0xCC 0xCC 0xCC 0xCC) using the 
WRITE_MULTIPLE_COILS command (FC15) to slave 10.

1. I configure the RtuServer with proper settings
server = modbus_rtu.RtuServer(serial.Serial(port=0, baudrate=9600, bytesize=8, 
parity='E', stopbits=1, xonxoff=0))

2. Add the proper slave as configured in the device
slave_1 = server.add_slave(10)

3. I make a block with proper size
slave_1.add_block("block",  cst.COILS, 0, 256)

4. And finally start the server
server.start()

The output that I get is following:

RtuServer COM1 is opened
running...
-->10-15-0-0-0-32-4-204
handle request failed: Invalid CRC in request
<--129-4
-->204-204-204-75-150
handle request failed: Invalid CRC in request
<--129-4

As you know 204 = 0xCC, so my 32 coils are the last value in the first request 
and the first 3 values in the second request, where I expect only one request 
like this:

-->10-15-0-0-0-32-4-204-204-204-204-75-150

which is not the case. That follows to wrong CRC and impossibility to correctly 
fulfill the FC15. 

I'm using version 0.4.1 on Windows XP SP3.

I tried the same configuration with Modbus Slave program from Modbus Tools and 
it's working flawless.

Original issue reported on code.google.com by petar.e...@gmail.com on 11 Oct 2011 at 8:27

GoogleCodeExporter commented 9 years ago
Hello,
I agree that the behaviour should be what you are expecting.

I've just pushed a new version with a new unit-test that tries to reproduce 
your problem. 

Can you please try to get it and run:
python functest_modbus_rtu.py RtuTestQueries.testWriteMultipleCoilsIssue23

Does the test pass? In my case, it is ok and the queries seems to be what you 
are expecting.

Did you notice similar issue on other function codes?

Best
luc

Original comment by luc.jean@gmail.com on 11 Oct 2011 at 9:20

GoogleCodeExporter commented 9 years ago
Hello,
Thanks for the quick reaction. I did the test connecting two of my serial ports 
one to each other. The request now looks how it should be, however the test 
failed. The output is following:

Hello
RtuMaster COM6 is opened
RtuServer COM1 is opened
-> 10-15-0-0-0-32-4-204-204-204-204-75-150
-->10-15-0-0-0-32-4-204
handle request failed: Invalid CRC in request
<--129-4
-->204-204-204-75-150
handle request failed: Invalid CRC in request
<--129-4
<- 129-4-129-4
Emodbus_tk.modbus_rtu.RtuServer has stopped

======================================================================
ERROR: testWriteMultipleCoilsIssue23 (__main__.RtuTestQueries)
Write the values of a multiple coils and check that it is correctly written
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Documents and Settings\Petar\Desktop\functest_modbus.py", line 287, in testWriteMultipleCoilsIssue23
    result = self.master.execute(10, modbus_tk.defines.WRITE_MULTIPLE_COILS, 0,output_value=[0, 0, 1, 1]*8)
  File "C:\Documents and Settings\Petar\Desktop\modbus_tk\utils.py", line 26, in new
    raise excpt
ModbusInvalidResponseError: Response address 129 is different from request 
address 10

----------------------------------------------------------------------
Ran 1 test in 1.203s

FAILED (errors=1)

---
One more thing: unittest.sys.argv is somehow not valid for me, so I used only 
"sys.argv" instead.
I forget to mention, I use Python 2.7

Original comment by petar.e...@gmail.com on 11 Oct 2011 at 12:04

GoogleCodeExporter commented 9 years ago
OK it seems that there is a "blank" on the serial link and that it causes the 
rtu server to see 2 different queries.
Can you try to increase the serial timeout by changing the value of 
server._serial.interCharTimeout and server._serial.timeout

I've pushed a new version when timeout is set to 5*to rather that 1.5*to (which 
seems wrong). May be it is noot enough?

Tell me if it fix the issue.
Best
luc

Original comment by luc.jean@gmail.com on 12 Oct 2011 at 6:07

GoogleCodeExporter commented 9 years ago
So, I did some tests with my own code. I played a bit wit the two 
aforementioned parameters and found out, that the best working solution is:
self._serial.interCharTimeout = 1.5 * self._t0 #Line 148
self._serial.timeout = 10 * self._t0 #Line 149

If .timeout is above 5 and less than 10, it's still working, but from time to 
time it's splitting again some queries.
I've just tested with FC5, FC6 and FC16 commands and they are working fine too.

BR
Petar

Original comment by petar.e...@gmail.com on 12 Oct 2011 at 7:32

GoogleCodeExporter commented 9 years ago
Hello Petar,
I am ok to increase this timeout to 10*to.
I will commit and publish a new version soon.
Thanks for feedback and your contribution to modbus-tk.
Best
luc

Original comment by luc.jean@gmail.com on 12 Oct 2011 at 7:46

GoogleCodeExporter commented 9 years ago
modbus-tk 0.4.2 has been released and fix the problem

Original comment by luc.jean@gmail.com on 12 Oct 2011 at 8:30