16bit decode issue help wanted
TristanLowell opened this issue · 0 comments
Versions
- Python: Python 3.12.2
- OS: windows
- Pymodbus: latest
- Modbus Hardware (if used): modbus slave simulator
Pymodbus Specific
- Client: tcp - sync
Description
When i try to decode a single modbus register 16bit input register i get an error indicated "unpack requires a buffer of 2 bytes. I get the same behavior when i use the serial version in sync. I have tried searching to see if i am performing this operation wrong, but when i do this with 2 registers 32bit decoding, i do not get this error. Thank you for your help.
Code and Logs
from pymodbus.client import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
import logging
logger = logging.basicConfig(level=logging.DEBUG)
client = ModbusTcpClient('127.0.0.1') # Create client object
client.connect() # connect to device
result = client.read_input_registers(1, 2, slave=1) # get information from device
print(result.registers)
decoder = BinaryPayloadDecoder.fromCoils(result.registers)
out = decoder.decode_16bit_uint()
print(out) # use information
client.close()
PS C:\Temp\pymodbusTesting> python .\testmodbus.py
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('127.0.0.1', 1884)
DEBUG:pymodbus.logging:Current transaction state - IDLE
DEBUG:pymodbus.logging:Running transaction 1
DEBUG:pymodbus.logging:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x4 0x0 0x1 0x0 0x2
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 0x0 0x1 0x0 0x0 0x0 0x7 0x1 0x4 0x4 0x0 0x0 0x43 0xe4
DEBUG:pymodbus.logging:Processing: 0x0 0x1 0x0 0x0 0x0 0x7 0x1 0x4 0x4 0x0 0x0 0x43 0xe4
DEBUG:pymodbus.logging:Factory Response[ReadInputRegistersResponse': 4]
DEBUG:pymodbus.logging:Frame advanced, resetting header!! DEBUG:pymodbus.logging:Adding transaction 1 DEBUG:pymodbus.logging:Getting transaction 1 DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
[0, 17380]
Traceback (most recent call last):
File "C:\Temp\pymodbusTesting\testmodbus.py", line 13, in
out = decoder.decode_16bit_uint()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tlowell\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymodbus\payload.py", line 367, in decode_16bit_uint
return unpack(fstring, handle)[0]
^^^^^^^^^^^^^^^^^^^^^^^
struct.error: unpack requires a buffer of 2 bytes