请在这里找人帮忙。我正在与 modbus 设备通信并按预期取回数据,我对此很陌生,所以我需要一些建议,我试图读取我的设备(伊顿 SC200)上的所有位寄存器以用于自定义 HMI。如果我尝试 for next 循环,或者甚至在发布的代码中,com 端口会失去通信。
#!/usr/bin/env python
import minimalmodbus
import time
instrument = minimalmodbus.Instrument('COM11', 1)
minimalmodbus.TIMEOUT = 1
DO =[]
instrument.address # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
#port name, slave address (in decimal)#0 = instrument.read_bit(1,1)
#digital = instrument.read_registers(1,32,4)
Version =instrument.read_registers(3001,3,4)
serialupper = instrument.read_registers(6001,1,4)
serialLower = instrument.read_registers(6002,1,4)
busvolt= instrument.read_registers(7001,4,4)
print type(serialupper[0])
print(serialupper, serialLower)
upper = serialupper[0] << 16 # shift upper left 16 bits
Snumber = upper + serialLower[0]
print ('Serial no: = ' + str(Snumber))
print(busvolt)
print ('Getting i-o')
print instrument.read_bit(1001,2)
print instrument.read_bit(1002,2)
print instrument.read_bit(1003,2)
print instrument.read_bit(1004,2)
print instrument.read_bit(1101,2)
print instrument.read_bit(1102,2)
print instrument.read_bit(1201,2)
print instrument.read_bit(1202,2)
print instrument.read_bit(1203,2)
print instrument.read_bit(1204,2)
print instrument.read_bit(1201,2)
print instrument.read_bit(1201,2)
我得到的回应是这样的:
<type 'int'>
([3754], [53255])
Serial no: = 246075399
[16989, 42144, 32704, 0]
Getting i-o
0
0
Traceback (most recent call last):
File "modbusRTU.py", line 29, in <module>
print instrument.read_bit(1003,2)
File "C:\Python27\lib\site-packages\minimalmodbus.py", line 193, in read_bit
return self._genericCommand(functioncode, registeraddress)
File "C:\Python27\lib\site-packages\minimalmodbus.py", line 697, in _genericCommand
payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
File "C:\Python27\lib\site-packages\minimalmodbus.py", line 795, in _performCommand
response = self._communicate(request, number_of_bytes_to_read)
File "C:\Python27\lib\site-packages\minimalmodbus.py", line 930, in _communicate
raise IOError('No communication with the instrument (no answer)')
IOError: No communication with the instrument (no answer)
我已将 Timeout 更改为从 0.5 到 2 的各种值,以查看这是否是问题所在,有时我会运行完整的数据,而有时我什么也得不到。任何帮助将不胜感激。