Skip to content

Commit 3378a73

Browse files
authored
Add convert_from to simple examples. (#2458)
1 parent b861b7c commit 3378a73

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

examples/simple_async_client.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import pymodbus.client as ModbusClient
1515
from pymodbus import (
16-
ExceptionResponse,
1716
FramerType,
1817
ModbusException,
1918
pymodbus_apply_logging_config,
@@ -75,14 +74,24 @@ async def run_async_simple_client(comm, host, port, framer=FramerType.SOCKET):
7574
client.close()
7675
return
7776
if rr.isError():
78-
print(f"Received Modbus library error({rr})")
77+
print(f"Received exception from device ({rr})")
78+
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
79+
client.close()
80+
return
81+
try:
82+
# See all calls in client_calls.py
83+
rr = await client.read_holding_registers(10, count=2, slave=1)
84+
except ModbusException as exc:
85+
print(f"Received ModbusException({exc}) from library")
7986
client.close()
8087
return
81-
if isinstance(rr, ExceptionResponse):
82-
print(f"Received Modbus library exception ({rr})")
88+
if rr.isError():
89+
print(f"Received exception from device ({rr})")
8390
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
8491
client.close()
85-
92+
return
93+
value_int32 = client.convert_from_registers(rr.registers, data_type=client.DATATYPE.INT32)
94+
print(f"Got int32: {value_int32}")
8695
print("close connection")
8796
client.close()
8897

examples/simple_sync_client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# --------------------------------------------------------------------------- #
1616
import pymodbus.client as ModbusClient
1717
from pymodbus import (
18-
ExceptionResponse,
1918
FramerType,
2019
ModbusException,
2120
pymodbus_apply_logging_config,
@@ -74,13 +73,24 @@ def run_sync_simple_client(comm, host, port, framer=FramerType.SOCKET):
7473
client.close()
7574
return
7675
if rr.isError():
77-
print(f"Received Modbus library error({rr})")
76+
print(f"Received exception from device ({rr})")
77+
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
78+
client.close()
79+
return
80+
try:
81+
# See all calls in client_calls.py
82+
rr = client.read_holding_registers(10, count=2, slave=1)
83+
except ModbusException as exc:
84+
print(f"Received ModbusException({exc}) from library")
7885
client.close()
7986
return
80-
if isinstance(rr, ExceptionResponse):
81-
print(f"Received Modbus library exception ({rr})")
87+
if rr.isError():
88+
print(f"Received exception from device ({rr})")
8289
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
8390
client.close()
91+
return
92+
value_int32 = client.convert_from_registers(rr.registers, data_type=client.DATATYPE.INT32)
93+
print(f"Got int32: {value_int32}")
8494

8595
print("close connection")
8696
client.close()

0 commit comments

Comments
 (0)