尝试通过 python 控制 DGH D8500 modbus
Trying to Control a DGH D8500 modbus via python
我有这个名为 DGH D8500 的双通道模拟输出 modbus RTU 协议 USB 接口。我需要通过 python 动态和自动化地控制通道的电压。我连接它 我用它自己的程序试过它,但我无法使用 python 与它通信,我尝试使用 pymodbus 和 minimalmodbus 但我似乎没有收到任何类型的响应:
波特率=9600,奇偶校验=none,字节大小=8,停止字节1
Pymodbus 代码
from pymodbus.client.sync import ModbusSerialClient
import serial
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
instrument = ModbusSerialClient(method='rtu', port='COM3', baudrate=9600, parity=serial.PARITY_NONE, stopbits=1, bytesize=8)
print instrument.connect()
print instrument
value = instrument.read_holding_registers(40113, unit=1)
print value
Pymodbus 错误
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x9c 0xb1 0x0 0x1 0xfa 0x7d
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
True
ModbusSerialClient(rtu baud[9600])
Exception Response(131, 3, IllegalAddress)
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x1 0x83 0x2 0xc0 0xf1
DEBUG:pymodbus.framer.rtu_framer:Getting Frame - 0x83 0x2
DEBUG:pymodbus.factory:Factory Response[131]
DEBUG:pymodbus.framer.rtu_framer:Frame advanced, resetting header!!
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Process finished with exit code 0
最小 modbus 代码
import minimalmodbus
instrument = minimalmodbus.Instrument('COM3', 1, mode='rtu', debug=True)
instrument.serial.baudrate = 9600
print instrument
value = instrument.read_register(registeraddress=40113, number_of_decimals=3, functioncode=3, signed=False)
print value
最小 modbus 错误
Connected to pydev debugger (build 192.5728.105)
MinimalModbus debug mode. Create serial port COM3
minimalmodbus.Instrument<id=0x39aee88, address=1, mode=rtu, close_port_after_each_call=False, precalculate_read_size=True, clear_buffers_before_each_transaction=True, handle_local_echo=False, debug=True, serial=Serial<id=0x3ae1518, open=True>(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=0.05, xonxoff=False, rtscts=False, dsrdtr=False)>
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x03\x9c\xb1\x00\x01\xfa}' (01 03 9C B1 00 01 FA 7D)
MinimalModbus debug mode. Clearing serial buffers for port COM3
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1574707870601.00 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '' () (0 bytes), roundtrip time: 63.0 ms. Timeout for reading: 50.0 ms.
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2060, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2054, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1405, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1412, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Users/naxso/PycharmProjects/modbus_virt/minimal_modbus.py", line 8, in <module>
value = instrument.read_register(registeraddress=40113, number_of_decimals=3, functioncode=3, signed=False)
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 447, in read_register
payloadformat=_PAYLOADFORMAT_REGISTER,
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 1170, in _generic_command
payload_from_slave = self._perform_command(functioncode, payload_to_slave)
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 1240, in _perform_command
response = self._communicate(request, number_of_bytes_to_read)
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 1406, in _communicate
raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
我真的不太了解这是我第一次使用它,我正在按照步骤说明进行操作,但由于我的知识很少,这可能只是我的一个小错误。
一个非常常见的问题的非常简单的解决方案。
如果你查看 pymodbus 日志,你会看到这一行:
Exception Response(131, 3, IllegalAddress)
这意味着您要读取的寄存器不存在。
发生这种情况是因为您引用 Modbus 寄存器地址的方式有点混乱。大多数设备使用地址 40xxx 来保存寄存器,但是当你实际读取这些寄存器时,你需要引用它们减去 40000。所以如果你想读取寄存器 40113,你应该这样做:
value = instrument.read_holding_registers(113, unit=1)
如果要打印存储在寄存器中的值,请注意必须显式调用它:
print value.registers[0]
编辑:正如下面讨论的,有时偏移量是 40001 而不是 40000。所以如果你想读取寄存器 40114,你需要查询 40114-40001=113。
我有这个名为 DGH D8500 的双通道模拟输出 modbus RTU 协议 USB 接口。我需要通过 python 动态和自动化地控制通道的电压。我连接它 我用它自己的程序试过它,但我无法使用 python 与它通信,我尝试使用 pymodbus 和 minimalmodbus 但我似乎没有收到任何类型的响应:
波特率=9600,奇偶校验=none,字节大小=8,停止字节1
Pymodbus 代码
from pymodbus.client.sync import ModbusSerialClient
import serial
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
instrument = ModbusSerialClient(method='rtu', port='COM3', baudrate=9600, parity=serial.PARITY_NONE, stopbits=1, bytesize=8)
print instrument.connect()
print instrument
value = instrument.read_holding_registers(40113, unit=1)
print value
Pymodbus 错误
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x9c 0xb1 0x0 0x1 0xfa 0x7d
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
True
ModbusSerialClient(rtu baud[9600])
Exception Response(131, 3, IllegalAddress)
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x1 0x83 0x2 0xc0 0xf1
DEBUG:pymodbus.framer.rtu_framer:Getting Frame - 0x83 0x2
DEBUG:pymodbus.factory:Factory Response[131]
DEBUG:pymodbus.framer.rtu_framer:Frame advanced, resetting header!!
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Process finished with exit code 0
最小 modbus 代码
import minimalmodbus
instrument = minimalmodbus.Instrument('COM3', 1, mode='rtu', debug=True)
instrument.serial.baudrate = 9600
print instrument
value = instrument.read_register(registeraddress=40113, number_of_decimals=3, functioncode=3, signed=False)
print value
最小 modbus 错误
Connected to pydev debugger (build 192.5728.105)
MinimalModbus debug mode. Create serial port COM3
minimalmodbus.Instrument<id=0x39aee88, address=1, mode=rtu, close_port_after_each_call=False, precalculate_read_size=True, clear_buffers_before_each_transaction=True, handle_local_echo=False, debug=True, serial=Serial<id=0x3ae1518, open=True>(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=0.05, xonxoff=False, rtscts=False, dsrdtr=False)>
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x03\x9c\xb1\x00\x01\xfa}' (01 03 9C B1 00 01 FA 7D)
MinimalModbus debug mode. Clearing serial buffers for port COM3
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1574707870601.00 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '' () (0 bytes), roundtrip time: 63.0 ms. Timeout for reading: 50.0 ms.
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2060, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2054, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1405, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1412, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Users/naxso/PycharmProjects/modbus_virt/minimal_modbus.py", line 8, in <module>
value = instrument.read_register(registeraddress=40113, number_of_decimals=3, functioncode=3, signed=False)
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 447, in read_register
payloadformat=_PAYLOADFORMAT_REGISTER,
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 1170, in _generic_command
payload_from_slave = self._perform_command(functioncode, payload_to_slave)
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 1240, in _perform_command
response = self._communicate(request, number_of_bytes_to_read)
File "C:\Users\naxso\PycharmProjects\modbus_virt\venv\lib\site-packages\minimalmodbus.py", line 1406, in _communicate
raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
我真的不太了解这是我第一次使用它,我正在按照步骤说明进行操作,但由于我的知识很少,这可能只是我的一个小错误。
一个非常常见的问题的非常简单的解决方案。
如果你查看 pymodbus 日志,你会看到这一行:
Exception Response(131, 3, IllegalAddress)
这意味着您要读取的寄存器不存在。
发生这种情况是因为您引用 Modbus 寄存器地址的方式有点混乱。大多数设备使用地址 40xxx 来保存寄存器,但是当你实际读取这些寄存器时,你需要引用它们减去 40000。所以如果你想读取寄存器 40113,你应该这样做:
value = instrument.read_holding_registers(113, unit=1)
如果要打印存储在寄存器中的值,请注意必须显式调用它:
print value.registers[0]
编辑:正如下面讨论的,有时偏移量是 40001 而不是 40000。所以如果你想读取寄存器 40114,你需要查询 40114-40001=113。