簡體   English   中英

最小modbus讀取數據位

[英]minimalmodbus read data bits

我似乎無法讀取的數據塊

我對 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。

使用minimalmodbus

instrument.read_register(1536)

返回:0

instrument.read_registers(1536, 1)

返回:[0]

instrument.read_bit(1536)

返回:錯誤

2018 年 9 月 12 日更新:

控制關閉/待機時讀取寄存器。

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

2018 年 9 月 12 日更新:

讀:

您正在讀取寄存器 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"MSByte = "00000001" LSByte = "00000001"
  • 將待機狀態更改為 0: MSByte = "00000001"MSByte = "00000001" LSByte = "00000000"
  • 將冷藏室燈更改為 1: MSByte = "00000010"MSByte = "00000010" LSByte = "00000010"
  • 將冷藏室燈更改為 0: MSByte = "00000010"MSByte = "00000010" LSByte = "00000000"

您必須使用從 int 到 bin 的轉換,更改 MSByte 和 LSByte 的位值,再次從 bin 轉換為 int,然后寫入值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM