data blocks I cant seem to be able to read
我只是python和modbus的新手,我一直在努力研究如何一两个星期同时使用pymodbus和minimalmodbus来读取此控制器的MSBytes和LSBytes,因此希望有人可以相信这里可能朝正确的方向戳我。
这个特定的控制器有3个数字/线圈寄存器(2个寄存器地址只能用8 MSBytes和8 LSBytes读取,还有一个寄存器1536如上图所示,它可以读写8 MSBytes和8 LSBytes)但是我很困惑,因为我不能似乎能够弄清楚如何正确阅读它们。
当我尝试使用仅read_coil / bits函数读取它们时,似乎似乎只有错误,但read_register和read_registers函数返回单个布尔结果0或1,计数为1寄存器。
使用minimalmodbus
instrument.read_register(1536)
返回:0
instrument.read_registers(1536, 1)
返回:[0]
instrument.read_bit(1536)
返回:错误
更新12-09-2018:
控制关闭/待机时读取寄存器。
In: client.read_register(1536, 0, 3, False)Out: 1
控制打开时读取寄存器。
In: client.read_register(1536, 0, 3, False)Out: 0
当控制处于除霜状态时,读取寄存器。
In: client.read_register(1536, 0, 3, False)Out: 4
尝试写入寄存器的响应:
控制文档说使用功能代码6将更改写入寄存器,但是似乎可以毫无错误地获取新值,但是不会更新或更改控制器寄存器。
如果我使用功能码6
In: client.write_register(1536, 1, 0, 6, False)(no error or output, and register value doesn't change)
如果我按照建议使用功能代码16,则会出现以下错误。
In: client.write_register(1536, 1, 0, 16, False)
ValueError Traceback (most recent call last)
<ipython-input-22-66ccb391e76c> in <module>()
----> 1 client.write_register(1536, 1, 0, 16, False)
/usr/local/lib/python3.5/dist-packages/minimalmodbus.py in write_register(self, registeraddress, value, numberOfDecimals, functioncode, signed)
294 _checkNumerical(value, description='input value')
295
--> 296 self._genericCommand(functioncode, registeraddress, value, numberOfDecimals, signed=signed)
297
298
/usr/local/lib/python3.5/dist-packages/minimalmodbus.py in _genericCommand(self, functioncode, registeraddress, value, numberOfDecimals, numberOfRegisters, signed, payloadformat)
695
696 ## Communicate ##
--> 697 payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
698
699 ## Check the contents in the response payload ##
/usr/local/lib/python3.5/dist-packages/minimalmodbus.py in _performCommand(self, functioncode, payloadToSlave)
796
797 # Extract payload
--> 798 payloadFromSlave = _extractPayload(response, self.address, self.mode, functioncode)
799 return payloadFromSlave
800
/usr/local/lib/python3.5/dist-packages/minimalmodbus.py in _extractPayload(response, slaveaddress, mode, functioncode)
1086
1087 if receivedFunctioncode == _setBitOn(functioncode, BITNUMBER_FUNCTIONCODE_ERRORINDICATION):
-> 1088 raise ValueError('The slave is indicating an error. The response is: {!r}'.format(response))
1089
1090 elif receivedFunctioncode != functioncode:
ValueError: The slave is indicating an error. The response is: '\x02\x90\x01}À'`
最佳答案
如果使用read_bit函数:
read_bit(registeraddress, functioncode=2)
read_bit(1536, 2)
编辑:
该功能只能读取地址的第一位。如果地址中有多个位,则无法使用此功能,否则您将收到错误消息。
如果使用read_register函数:
read_register(registeraddress, numberOfDecimals=0, functioncode=3, signed=False)
read_register(1536,0,3,False)
作为输出,您将获得一个Unsigned Int
如果使用read_registers:
read_registers(registeraddress, numberOfRegisters, functioncode=3)
read_registers(1536, 1, 3)
如您在这里阅读:
要要求对设备进行修改,您必须写入MSByte和LSByte。
解:
import minimalmodbus
def _intToBin(toConvert):
#Here you convert the int value to binary, after that to string getting from index 2 to 10
MSByte = str(bin(toConvert))[2:10]
#Here you convert the int value to binary, after that to string getting from index 10 to 18
LSByte = str(bin(toConvert))[10:18]
final = MSByte+LSByte
return final
def _binToInt():
return int(value,2)
def _changeBit(bitToChange, binVal, valueToSet):
#Set the single bit
tmpList = list(binVal)
finalString = ""
tmpList[bitToChange] = str(int(valueToSet))
for x in tmpList:
finalString += x
return finalString
# DEFAULT CONFIG OF minimalmodbus
ReadType = minimalmodbus.MODE_RTU
minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL = True
minimalmodbus.BAUDRATE = 19200
minimalmodbus.PARITY = 'E'
minimalmodbus.BYTESIZE = 8
minimalmodbus.STOPBITS = 1
minimalmodbus.TIMEOUT = 0.05
modbusAddress = 1536
instrument = minimalmodbus.Instrument("/dev/tty.usbserial-A9CVVTT5",1,mode="rtu")
instrument.debug = True
readValue = instrument.read_register(modbusAddress,0,3,False)
#This is to demostrate that the conversion works fine
print "This is the pure readed value: " + str(readValue)
binValue = _intToBin(readValue)
print "This is the value after the binary conversion, if you want to come back to int: " + str(int(binValue,2))
#Here you can change the state of your converted value
print "Before change binary value: " + binValue
changeBit = _changeBit(3,binValue,False)
print "Single bit change: " + str(changeBit)
print "Int after bit value change: " + str(_binToInt(changeBit))
#After that you can write back your register
instrument.write_register(modbusAddress,_binToInt(changeBit),0,16,False)
输出:
This is the pure readed value: 65472
This is the value after the binary conversion, if you want to come back to int: 65472
Before change binary value: 1111111111000000
Single bit change: 1110111111000000
Int after bit value change: 61376
更新12-09-2018:
阅读:
您正在读取寄存器1536,它将正确返回int值。因此,您只需要将int值转换为bin值,并将转换后的bin值与图片相关联即可。
写作:
如您在文档中所读:
功能码6:写单个寄存器
功能码16:写多个寄存器
所以这是正确的命令:
client.write_register(1536, 1, 0, 6, False)
现在,问题是:
如果您在图片下方阅读,则说明正在谈论写入LSByte和MSByte来更改位状态。
因此,将值1写入寄存器1536,但仅将其写入LSByte。
您还必须在MSByte中编写,然后:
LSByte = "00000001" # it is 1 in decimal
MSByte = "00000001" # it is 1 in decimal
ValueToSend = MSByte + LSByte
# The result value will be: "0000000100000001"
# If you convert it to decimal is: 257
#Then here you have to write
client.write_register(1536, 257, 0, 6, False)
必须将MSByte写入1,即LSByte对应位。
例如:
将待机状态更改为1:
MSByte = "00000001"
和LSByte = "00000001"
将待机状态更改为0:
MSByte = "00000001"
和LSByte = "00000000"
将冷室灯更改为1:
MSByte = "00000010"
和LSByte = "00000010"
将冷室灯更改为0:
MSByte = "00000010"
和LSByte = "00000000"
您必须使用从int到Bin的转换,更改MSByte和LSByte的位值,再次从bin转换为int,然后写入该值。
关于python - Minimummodbus读取数据位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52199611/