2

我在 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'
4

1 回答 1

0

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

当你做 a 时它会给你一个超时错误.read_coil()吗?

此外,超时参数以秒为单位指定。你的价值2000意味着33几分钟的超时。

于 2018-06-26T17:34:15.320 回答