2

I am using modbus-tk library for modbus serial server. All the communication is up and working. There is one instance where master is writing one register and next request is read but modbus-tk is merging the two request and hence getting CRC error

2019-01-31 17:19:59,881 DEBUG   modbus._handle  Thread-2    -->2-16-0-11-0-1-2-0-128-178-123-2-3-0-4-0-1-197-248
2019-01-31 17:19:59,881 ERROR   modbus.handle_request   Thread-2    invalid request: Invalid CRC in request

Actual request should be 2-16-0-11-0-1-2-0-128-178-123 and othe request is 2-3-0-4-0-1-197-248

Any ideas why I am having the issues

For setup, Modbus slave is connected via serial 232 and running two slave instances on single server.

Sumit
  • 1,953
  • 6
  • 32
  • 58
  • Can you add delay between two request (delay bet read & write) say 500ms. Sometimes device unable to handle two request. Also let me know is there two different instance of application querying to slave ? – Sushil Jadhav Feb 06 '19 at 09:14
  • Adding a delay on master side is not in my hands. Using a device as a master. – Sumit Feb 06 '19 at 11:47

1 Answers1

0

You must create Thread-safe read/write. If you read or write, you cant do it with uncontrolled threads. You need to lock threads when you read or write. I cant explain why, but last time I was working with modbus, I had similar problem. Modbus simply cant handle threads very well. Lock helped a lot, but still most safe it is to do it threadless.

Idea:

import threading

lock = threading.Lock()

def read():
    with lock:
        read....

def write():
    with lock:
        write....
Martin
  • 3,333
  • 2
  • 18
  • 39