即使没有连接也收到PyModbus RTU响应

时间:2018-06-26 04:14:54

标签: python arduino-uno modbus pymodbus

我正在Linkit MK7688上使用PyModbus,它已通过MAX485连接到Arduino UNO。以下是我写线圈的代码。

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)

我正在尝试在Modbus上没有任何连接的情况下进行尝试。在此设置中,我关闭了Arduino,并期望对写入请求无响应。但是,在调试日志中,我看到事务已完成。这是错误的,因为我不应该收到.write_coil()的任何回复。如果某处发生环回,该如何检测?

感谢您的回应。

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 个答案:

答案 0 :(得分:0)

取决于写功能。许多Modbus程序可让您指定是要进行写操作还是要进行带回读的写操作。 PyModbus只是将数据写入线圈,而无需尝试回读。这意味着它只是在485线上发送消息,而不检查它是否真正被写入。

执行.read_coil()时是否会出现超时错误?

此外,超时参数以秒为单位指定。您的2000值表示33分钟的超时。