2

data blocks I cant seem to be able to read

I'm only quite new to python and modbus and I have been struggling to work out how to read the MSBytes and LSBytes of this controller using both pymodbus and minimalmodbus for a week or two now so hopefully someone in the brains trust here might be able to poke me in the right direction.

This particular controller has 3 digital/coil registers (2 register addresses are read only with 8 MSBytes and 8 LSBytes and one register 1536 as pictured above which has read and write 8 MSbytes and 8 LSBytes) However I'm confused because I can't seem to be able to work out how to read them correctly.

I only seem to get errors when I try to read them with a read_coil/bits only function but read_register and read_registers functions returns a single boolean result of 0 or 1 with a count of 1 register.

Using minimalmodbus

instrument.read_register(1536)

returns: 0

instrument.read_registers(1536, 1)

returns: [0]

instrument.read_bit(1536)

returns: error

UPDATE 12-09-2018:

Reading register when control is Off/standby.

In: client.read_register(1536, 0, 3, False) Out: 1

Reading register when control is On.

In: client.read_register(1536, 0, 3, False) Out: 0

Reading register when control is in defrost.

In: client.read_register(1536, 0, 3, False) Out: 4

Response from trying to write to registers:

The control documentation says to use functioncode 6 to write changes to registers however it seems to take the new value without error but doesn't update or change the controller register.

If I use functioncode 6

In: client.write_register(1536, 1, 0, 6, False) (no error or output, and register value doesn't change)

If I use functioncode 16 as suggested it leaves the following error.

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

1 回答 1

2

如果您使用 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,然后写入值。

于 2018-09-11T07:00:14.213 回答