【问题标题】:minimalmodbus read data bits最小modbus读取数据位
【发布时间】:2018-09-06 08:20:58
【问题描述】:

data blocks I cant seem to be able to read

我对 python 和 modbus 还很陌生,我一直在努力研究如何使用 pymodbus 和 minimummodbus 读取这个控制器的 MSBytes 和 LSBytes 一两个星期,所以希望有头脑的人相信这里或许能把我引向正确的方向。

这个特定的控制器有 3 个数字/线圈寄存器(2 个寄存器地址是只读的,8 MSBytes 和 8 LSBytes 和一个寄存器 1536,如上图所示,它读取和写入 8 MSbytes 和 8 LSBytes)但是我很困惑,因为我似乎无法弄清楚如何正确阅读它们。

我似乎只在尝试使用 read_coil/bits only 函数读取它们时出现错误,但 read_register 和 read_registers 函数返回单个布尔结果 0 或 1,寄存器计数为 1。

使用最小的modbus

instrument.read_register(1536)

返回:0

instrument.read_registers(1536, 1)

返回:[0]

instrument.read_bit(1536)

返回:错误

2018 年 12 月 9 日更新:

控制关闭/待机时读取寄存器。

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}À'`

【问题讨论】:

  • 如果您无法帮助我理解并理解,但真的要投反对票吗?

标签: python minimalmodbus


【解决方案1】:

如果使用 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

2018 年 12 月 9 日更新:

阅读:

您正在读取寄存器 1536,它正确返回了 int 值。所以你只需要将 int 值转换为 bin 值,并将转换后的 bin 值关联到图片。

写作:

您可以在文档中阅读:

  1. 功能代码6:写单个寄存器
  2. 功能代码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`]'是什么让我头疼! read_register(1536, 0, 3) - 返回 0 但我不知道如何将其分解为字符串位并读取它们。
  • 我要在我的modbus设备上做一个测试,稍后我会回复你的!
  • 谢谢。如果我这样做 read_string(1536, 1) 它返回: '\x00\x04' 转换为十六进制的 0 和 4 但这似乎与位应该是什么不相符......
  • 检查我的答案,我在那里写了一个可能的解决方案,我想它可以帮助你!
  • 谢谢 Carlo,我认为那里的文档一定有问题?我可以运行你的代码的第一个块(转换为 python3,它仍然给我一个读取值 0)
猜你喜欢
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2020-12-23
  • 2019-10-08
  • 2019-01-20
  • 2021-12-02
相关资源
最近更新 更多