minimalmodbus 读取数据位
minimalmodbus read data bits
data blocks I cant seem to be able to read
我只是 python 和 modbus 的新手,我一直在努力研究如何使用 pymodbus 和 minimalmodbus 读取这个控制器的 MSBytes 和 LSBytes 一两个星期,希望如此这里的智囊团中的某个人可能会向我指出正确的方向。
这个特定的控制器有 3 个 digital/coil 寄存器(2 个寄存器地址是只读的,8 MSBytes 和 8 LSBytes 和一个寄存器 1536,如上图所示,它具有读写 8 MSBytes 和 8 LSBytes)但是我'我很困惑,因为我似乎无法弄清楚如何正确阅读它们。
我似乎只在尝试使用 read_coil/bits 函数读取它们时出现错误,但 read_register 和 read_registers 函数 returns 的单个布尔结果0 或 1,计数为 1 个寄存器。
使用 minimalmodbus
instrument.read_register(1536)
returns: 0
instrument.read_registers(1536, 1)
returns: [0]
instrument.read_bit(1536)
returns: 错误
更新 12-09-2018:
控制为Off/standby时读取寄存器。
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,它正确地 returns 了 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,然后写入值。
data blocks I cant seem to be able to read
我只是 python 和 modbus 的新手,我一直在努力研究如何使用 pymodbus 和 minimalmodbus 读取这个控制器的 MSBytes 和 LSBytes 一两个星期,希望如此这里的智囊团中的某个人可能会向我指出正确的方向。
这个特定的控制器有 3 个 digital/coil 寄存器(2 个寄存器地址是只读的,8 MSBytes 和 8 LSBytes 和一个寄存器 1536,如上图所示,它具有读写 8 MSBytes 和 8 LSBytes)但是我'我很困惑,因为我似乎无法弄清楚如何正确阅读它们。
我似乎只在尝试使用 read_coil/bits 函数读取它们时出现错误,但 read_register 和 read_registers 函数 returns 的单个布尔结果0 或 1,计数为 1 个寄存器。
使用 minimalmodbus
instrument.read_register(1536)
returns: 0
instrument.read_registers(1536, 1)
returns: [0]
instrument.read_bit(1536)
returns: 错误
更新 12-09-2018:
控制为Off/standby时读取寄存器。
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,它正确地 returns 了 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,然后写入值。