2
votes

I'm using PyModbus on Linkit MK7688 that is connected to Arduino UNO using MAX485. Following is my code for writing to coil.

from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import logging

FORMAT = ('%(asctime)-15s %(threadName)-15s '
          '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)

client = ModbusClient(method='rtu', port='/dev/ttyS1', timeout=2000, baudrate=9600)

client.debug=True
print(client)
response = client.write_coil(1, True, unit=1)

I am trying this out with nothing connected on the Modbus. In this setup I have turned off Arduino and expect no response for the write request. However, in the debug log I see that transaction is complete. This is wrong as I should not receive any response for the .write_coil(). If there is loopback that is happening somewhere, how do I detect it?

Appreciate your response.

2018-06-26 03:56:53,331 MainThread      DEBUG    sync           :405      Serial client intialising
2018-06-26 03:56:53,337 MainThread      DEBUG    sync           :42       Initializing...
ModbusSerialClient(rtu baud[9600])
2018-06-26 03:56:53,344 MainThread      DEBUG    transaction    :107      Current transaction state - IDLE
2018-06-26 03:56:53,347 MainThread      DEBUG    transaction    :111      Running transaction 1
2018-06-26 03:56:53,350 MainThread      DEBUG    transaction    :201      SEND: 0x1 0x5 0x0 0x1 0xff 0x0 0xdd 0xfa
2018-06-26 03:56:53,352 MainThread      DEBUG    sync           :77       New Transaction state 'SENDING'
2018-06-26 03:56:53,358 MainThread      DEBUG    transaction    :204      Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2018-06-26 03:56:53,371 MainThread      DEBUG    sync           :554      Finished reading socket....
2018-06-26 03:56:53,374 MainThread      DEBUG    sync           :554      Finished reading socket....
2018-06-26 03:56:53,376 MainThread      DEBUG    transaction    :279      Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2018-06-26 03:56:53,379 MainThread      DEBUG    transaction    :209      RECV: 0x1 0x5 0x0 0x1 0xff 0x0 0xdd 0xfa
2018-06-26 03:56:53,382 MainThread      DEBUG    rtu_framer     :175      Getting Frame - 0x5 0x0 0x1 0xff 0x0
2018-06-26 03:56:53,385 MainThread      DEBUG    factory        :246      Factory Response[WriteSingleCoilResponse: 5]
2018-06-26 03:56:53,388 MainThread      DEBUG    rtu_framer     :110      Frame advanced, resetting header!!
2018-06-26 03:56:53,390 MainThread      DEBUG    transaction    :410      Adding transaction 1
2018-06-26 03:56:53,393 MainThread      DEBUG    transaction    :420      Getting transaction 1
2018-06-26 03:56:53,397 MainThread      DEBUG    transaction    :175      Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
1
Ideally you should not be getting any response when no device is connected, have you tried physically disconnecting the device ? Your response and request both look same,( possible echo?). Unrelated to question , your read time out is 2000 secs, which is unusually high for any setup !Sanju
@Sanju Thanks for the response. My guess is reflection in the circuit. I am not too sure where is this happening. I shall debug and try to figure out. In some of the attempts I had set read timeout to 2000 and I shall change that. Thanks for pointing.prashant

1 Answers

0
votes

Depends on the write function. A lot of Modbus programs let you specify if you want to do a write, or a write with a readback. PyModbus just writes data to the coil without trying to read it back. That means it's just sending the message on the 485 lines without checking to see if it was actually written.

Does it give you a timeout error when you do a .read_coil()?

Also, the timeout parameter is specified in seconds. You your value of 2000 means a timeout of 33 minutes.