Python minimalmodbus输出ValueError

时间:2015-01-11 16:07:39

标签: python python-2.7 modbus

我编写了以下脚本来读取modbus寄存器:

__author__ = 'oz'
import minimalmodbus
import serial

instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 70)
instrument.serial.baudrate = 19200
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.stopbits = 1
instrument.serial.timeout = 1.0
instrument.address = 70
instrument.mode = minimalmodbus.MODE_RTU

holdingunknown = instrument.read_register(40076, 1)

print holdingunknown

我收到如下错误值:

/usr/bin/python2.7 /home/oz/PycharmProjects/untitled1/tst2.py
Traceback (most recent call last):
  File "/home/oz/PycharmProjects/untitled1/tst2.py", line 14, in <module>
    holdingunknown = instrument.read_register(40076, 1)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 254, in read_register
    return self._genericCommand(functioncode, registeraddress,    numberOfDecimals=numberOfDecimals, signed=signed)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 693, in _genericCommand
    payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 794, in _performCommand
    payloadFromSlave = _extractPayload(response, self.address, self.mode, functioncode)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 1054, in _extractPayload
    raise ValueError(text)
ValueError: Checksum error in rtu mode: '\x01d' instead of '\te' . The response is: 'F\x03    \x9c\x8c\x00\x01d' (plain response: 'F\x03\x9c\x8c\x00\x01d')

Process finished with exit code 1

为什么会这样?当我将模式切换为ASCII时,同样的事情发生在我身上。 我可以改变什么来获得响应?

2 个答案:

答案 0 :(得分:0)

几乎可以肯定,你的设备上的波特率不是19200.我有同样的问题使用Modbus-tk模拟一个modbusRTU从机,后来才发现我使用的示例代码初始化了一个串口到波特率为9600,而不是默认值。解决这个问题解决了我的问题。

答案 1 :(得分:0)

回复工具是你写的寄存器地址错误。

写入0x0.1以读取第一个寄存器。修改读取0x1,1读取第二个寄存器或0x0,8读取8个寄存器。需要了解如何写入寄存器值。