Skip to content

Instantly share code, notes, and snippets.

@git-developer
Last active June 25, 2023 07:11
Show Gist options
  • Save git-developer/ec0c12d82ca3fe0fe7a9cbead819061c to your computer and use it in GitHub Desktop.
Save git-developer/ec0c12d82ca3fe0fe7a9cbead819061c to your computer and use it in GitHub Desktop.
Python script to debug Modbus communication with a SBC ALD1D5FD energy meter
#!/usr/bin/env python3
#
# required modules: pymodbus 'pymodbus[serial]'
#
from pymodbus.client import ModbusSerialClient
from pymodbus.framer.rtu_framer import ModbusRtuFramer
from pymodbus.framer.binary_framer import ModbusBinaryFramer
from pymodbus.framer.ascii_framer import ModbusAsciiFramer
import logging
def main():
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
client = ModbusSerialClient(
port='/dev/ttyUSB0',
baudrate=19200,
bytesize=8,
parity='E',
stopbits=1,
timeout=1,
framer=ModbusRtuFramer
)
print('connected:', client.connect())
print(client)
for address in range(3):
print('try address:', hex(address))
for slave_id in range(30,31):
print(f'{slave_id=}')
print('read_holding_registers', client.read_holding_registers(address=address, count=1, slave=slave_id))
print('read_device_information', client.read_device_information(address=address, count=1, slave=slave_id))
client.close()
main()
@git-developer
Copy link
Author

Output:

connected: True
ModbusSerialClient(<pymodbus.framer.rtu_framer.ModbusRtuFramer object at 0x7f39568902e0> baud[19200])
try address: 0x0
slave_id=30
DEBUG:pymodbus.logging:Current transaction state - IDLE
DEBUG:pymodbus.logging:Running transaction 1
DEBUG:pymodbus.logging:SEND: 0x1e 0x3 0x0 0x0 0x0 0x1 0x86 0x65
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 4 bytes (1 received))
DEBUG:pymodbus.logging:Getting transaction 30
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
read_holding_registers Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 4 bytes (1 received)
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 2
DEBUG:pymodbus.logging:SEND: 0x1e 0x2b 0xe 0x1 0x0 0xe5 0xb5
DEBUG:pymodbus.logging:Changing state to IDLE - Last Frame End - None Current Time stamp - 1687674463.205598
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
DEBUG:pymodbus.logging:Getting transaction 30
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
read_device_information Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
try address: 0x1
slave_id=30
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 3
DEBUG:pymodbus.logging:Clearing current Frame: - 0x0
DEBUG:pymodbus.logging:Resetting frame - Current Frame in buffer - 0x0
DEBUG:pymodbus.logging:SEND: 0x1e 0x3 0x0 0x1 0x0 0x1 0xd7 0xa5
DEBUG:pymodbus.logging:Changing state to IDLE - Last Frame End - None Current Time stamp - 1687674463.36781
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:Incomplete message received, Expected 7 bytes Received 1 bytes !!!!
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 0x0
DEBUG:pymodbus.logging:Getting transaction 30
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
read_holding_registers Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 4
DEBUG:pymodbus.logging:Clearing current Frame: - 0x0 0x0
DEBUG:pymodbus.logging:Resetting frame - Current Frame in buffer - 0x0 0x0
DEBUG:pymodbus.logging:SEND: 0x1e 0x2b 0xe 0x1 0x0 0xe5 0xb5
DEBUG:pymodbus.logging:Changing state to IDLE - Last Frame End - None Current Time stamp - 1687674464.533712
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
DEBUG:pymodbus.logging:Getting transaction 30
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
read_device_information Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
try address: 0x2
slave_id=30
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 5
DEBUG:pymodbus.logging:Clearing current Frame: - 0x0 0x0 0x0
DEBUG:pymodbus.logging:Resetting frame - Current Frame in buffer - 0x0 0x0 0x0
DEBUG:pymodbus.logging:SEND: 0x1e 0x3 0x0 0x2 0x0 0x1 0x27 0xa5
DEBUG:pymodbus.logging:Changing state to IDLE - Last Frame End - None Current Time stamp - 1687674464.691069
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:Incomplete message received, Expected 7 bytes Received 1 bytes !!!!
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 0x0
DEBUG:pymodbus.logging:Getting transaction 30
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
read_holding_registers Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 6
DEBUG:pymodbus.logging:Clearing current Frame: - 0x0 0x0 0x0
DEBUG:pymodbus.logging:Resetting frame - Current Frame in buffer - 0x0 0x0 0x0
DEBUG:pymodbus.logging:SEND: 0x1e 0x2b 0xe 0x1 0x0 0xe5 0xb5
DEBUG:pymodbus.logging:Changing state to IDLE - Last Frame End - None Current Time stamp - 1687674465.850269
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
DEBUG:pymodbus.logging:Getting transaction 30
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
read_device_information Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment