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 only函数读取它们时,似乎只有错误出现,但是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}À'`
答案 0 :(得分:1)
read_bit(registeraddress, functioncode=2)
read_bit(1536, 2)
编辑: 该功能只能读取地址的第一位。如果地址中有多个位,则无法使用此功能,否则您将收到错误消息。
read_register(registeraddress, numberOfDecimals=0, functioncode=3, signed=False)
read_register(1536,0,3,False)
作为输出,您将获得一个 Unsigned Int
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值与图片相关联即可。
写作:
您可以在文档中阅读
这是正确的命令:
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对应位。
例如:
MSByte = "00000001"
和LSByte = "00000001"
MSByte = "00000001"
和LSByte = "00000000"
MSByte = "00000010"
和LSByte = "00000010"
MSByte = "00000010"
和LSByte = "00000000"
您必须使用从int到Bin的转换,更改MSByte和LSByte的位值,再次从bin转换为int,然后写入该值。