Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 最小Modbus读取数据位_Python_Minimalmodbus - Fatal编程技术网

Python 最小Modbus读取数据位

Python 最小Modbus读取数据位,python,minimalmodbus,Python,Minimalmodbus,我刚刚接触python和modbus,一两周来我一直在努力研究如何使用pymodbus和minimalmodbus读取该控制器的MSBytes和LSBytes,希望这里的智囊团中有人能给我指明正确的方向 这个特殊的控制器有3个数字/线圈寄存器(2个寄存器地址是只读的,有8个MB和8个LSBytes,还有一个寄存器1536,如上图所示,它读写了8个MB和8个LSBytes),但是我很困惑,因为我似乎无法正确读取它们 我似乎只有在尝试使用只读线圈/位函数读取它们时才会出错,但read_registe

我刚刚接触python和modbus,一两周来我一直在努力研究如何使用pymodbus和minimalmodbus读取该控制器的MSBytes和LSBytes,希望这里的智囊团中有人能给我指明正确的方向

这个特殊的控制器有3个数字/线圈寄存器(2个寄存器地址是只读的,有8个MB和8个LSBytes,还有一个寄存器1536,如上图所示,它读写了8个MB和8个LSBytes),但是我很困惑,因为我似乎无法正确读取它们

我似乎只有在尝试使用只读线圈/位函数读取它们时才会出错,但read_register和read_registers函数返回一个布尔结果0或1,寄存器计数为1

使用最小Modbus

instrument.read_register(1536)
返回:0

instrument.read_registers(1536, 1)
返回:[0]

instrument.read_bit(1536)
返回:错误

2018年9月12日更新:

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
控件关闭/待机时读取寄存器

In:client.read_寄存器(1536,0,3,False)
输出:1

控件打开时读取寄存器

In:client.read_寄存器(1536,0,3,False)
输出:0

控制器处于除霜状态时读取寄存器

In:client.read_寄存器(1536,0,3,False)
输出:4

尝试写入寄存器的响应:

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
控制文档说明使用functioncode 6写入对寄存器的更改,但它似乎没有错误地接受新值,但不会更新或更改控制器寄存器

如果我使用功能代码6

In:client.write_寄存器(1536,1,0,6,False)
(无错误或输出,寄存器值不变)

如果按照建议使用functioncode 16,则会留下以下错误

In:client.write_寄存器(1536,1,0,16,False)


ValueError回溯(最近一次调用)
在()
---->1个客户机写入寄存器(1536,1,0,16,False)
/usr/local/lib/python3.5/dist-packages/minimamodbus.py写入寄存器(self、寄存器地址、值、小数位数、函数代码、签名)
294_checknumeric(值,description='input value')
295
-->296 self.\u genericCommand(函数代码、寄存器地址、值、小数位数、有符号=有符号)
297
298
/usr/local/lib/python3.5/dist-packages/minimamodbus.py in_generic命令(self、functioncode、registeraddress、value、numberOfDecimals、numberOfRegisters、signed、payloadformat)
695
696##沟通##
-->697 payloadFromSlave=自执行命令(功能代码,payloadToSlave)
698
699##检查响应负载中的内容##
/性能命令中的usr/local/lib/python3.5/dist-packages/minimamodbus.py(self、functioncode、payloadToSlave)
796
797#提取有效载荷
-->798 payloadFromSlave=\u提取有效负载(响应、self.address、self.mode、函数代码)
799从从属服务器返回有效负载
800
/usr/local/lib/python3.5/dist-packages/minimamodbus.py in_-extractPayload(响应、slaveaddress、模式、功能代码)
1086
1087如果接收到的函数代码==\u设置项(函数代码、位号\u函数代码\u错误指示):
->1088 raise VALUERROR('从机指示错误。响应为:{!r}'。格式(响应))
1089
1090 elif接收到的功能代码!=功能代码:
ValueError:从设备指示错误。回答是:'\x02\x90\x01}À'`
如果使用读取位功能: 编辑: 此函数只能读取地址的第一位。如果地址中有一个以上的位,则无法使用此函数,否则将收到错误

如果使用read_寄存器功能: 作为输出,您将收到一个无符号Int

如果您使用read_寄存器: 正如你在这里看到的:

要请求修改设备,必须写入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
输出:

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
2018年9月12日更新:

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
阅读:

您正在读取寄存器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,然后写入值。

    如果您无法帮助我获取并理解该值,但真的投票否决了吗?感谢您花时间写回。读取寄存器和寄存器没有问题,但是它只提取布尔整数值0。如果我试着读一点你的例子,它会给出一个错误:[回答是:'\x02\x82\x01q`],这让我头疼!读U寄存器(1536,0,