diff --git a/examples/simple_async_client.py b/examples/simple_async_client.py index 37444ca0c..fceac333f 100755 --- a/examples/simple_async_client.py +++ b/examples/simple_async_client.py @@ -13,7 +13,6 @@ import pymodbus.client as ModbusClient from pymodbus import ( - ExceptionResponse, FramerType, ModbusException, pymodbus_apply_logging_config, @@ -75,14 +74,24 @@ async def run_async_simple_client(comm, host, port, framer=FramerType.SOCKET): client.close() return if rr.isError(): - print(f"Received Modbus library error({rr})") + print(f"Received exception from device ({rr})") + # THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message + client.close() + return + try: + # See all calls in client_calls.py + rr = await client.read_holding_registers(10, count=2, slave=1) + except ModbusException as exc: + print(f"Received ModbusException({exc}) from library") client.close() return - if isinstance(rr, ExceptionResponse): - print(f"Received Modbus library exception ({rr})") + if rr.isError(): + print(f"Received exception from device ({rr})") # THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message client.close() - + return + value_int32 = client.convert_from_registers(rr.registers, data_type=client.DATATYPE.INT32) + print(f"Got int32: {value_int32}") print("close connection") client.close() diff --git a/examples/simple_sync_client.py b/examples/simple_sync_client.py index 1a053a0d9..b65071a68 100755 --- a/examples/simple_sync_client.py +++ b/examples/simple_sync_client.py @@ -15,7 +15,6 @@ # --------------------------------------------------------------------------- # import pymodbus.client as ModbusClient from pymodbus import ( - ExceptionResponse, FramerType, ModbusException, pymodbus_apply_logging_config, @@ -74,13 +73,24 @@ def run_sync_simple_client(comm, host, port, framer=FramerType.SOCKET): client.close() return if rr.isError(): - print(f"Received Modbus library error({rr})") + print(f"Received exception from device ({rr})") + # THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message + client.close() + return + try: + # See all calls in client_calls.py + rr = client.read_holding_registers(10, count=2, slave=1) + except ModbusException as exc: + print(f"Received ModbusException({exc}) from library") client.close() return - if isinstance(rr, ExceptionResponse): - print(f"Received Modbus library exception ({rr})") + if rr.isError(): + print(f"Received exception from device ({rr})") # THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message client.close() + return + value_int32 = client.convert_from_registers(rr.registers, data_type=client.DATATYPE.INT32) + print(f"Got int32: {value_int32}") print("close connection") client.close()