2.3.4. Error Handling
Two types of errors can be distinguished:
Client-side Errors: Errors that occur in the Python client - e.g., logic errors in the Python code or failed network connection.
Server-side Errors: Errors that occur in the EtherCAT master application - e.g., misconfiguration or connection problems on the EtherCAT bus.
The status of the EtherCAT master application (server) can be obtained by ethercat.get_system_info(). For more
details related to the server-side error handling, refer to the EtherCAT master documentation. Client-side errors are
typically handled as Python exceptions. Log messages from the master application are also available through the Python
logger: logging.getLogger("voraus_ecat.master").
Available options for error handling can best be demonstrated through an example.
2.3.4.1. Example Code
Error Handling Example
1# wupi: Encryption=False
2# pylint: disable=duplicate-code
3"""A very simple example for error handling."""
4
5import logging
6import time
7from os import environ
8
9from fieldbus import Inputs, Outputs
10
11from voraus_ecat import EtherCAT, SystemInfo, SystemState
12from voraus_ecat.exceptions import EtherCATError, PDOError
13
14# create a simple application logger
15_logger = logging.getLogger("application")
16logging.getLogger().addHandler(logging.StreamHandler())
17
18
19def try_to_resolve_error(ecat: EtherCAT, info: SystemInfo) -> None:
20 """A simple error reaction that tries to restart the master application."""
21 _logger.info(f"System State: {info.state.name}")
22 _logger.info(f"Error: {info.error_message} (#{info.error_number})")
23
24 if info.state == SystemState.ERR_TRANS:
25 # in the "error transition" state, we could just ignore the error and try to continue...
26 ecat.continue_with_error()
27
28 # ...or try to get the slave devices in a safe state before shutting down.
29 # (see EtherCAT master documentation for more details about the state machine)
30
31 else:
32 _logger.info("Attempting restart...")
33 ecat.restart()
34 _logger.info("Waiting for READY state...")
35 ecat.wait_for_state(SystemState.READY)
36 _logger.info("Trying to get back to OP...")
37 ecat.set_op_state()
38 _logger.info("Ready to go.")
39
40
41if __name__ == "__main__":
42 ethercat = EtherCAT(inputs=Inputs(), outputs=Outputs())
43 url = environ.get("VECAT_URL", "opc.tcp://localhost:4841")
44
45 try:
46 with ethercat.connection(url):
47 # Set the master to operational state.
48 ethercat.set_op_state()
49
50 while True:
51
52 ethercat.read_pdos()
53 # [do something...]
54 ethercat.write_pdos()
55
56 time.sleep(0.1)
57
58 # check for errors
59 system_info = ethercat.get_system_info()
60 if system_info.error_number != 0:
61 try_to_resolve_error(ethercat, system_info)
62
63 except EtherCATError as e:
64 # this exception will occur if the master is not initialised correctly (FAILED SETUP state)
65 _logger.error(e)
66 except ConnectionError as e:
67 # connection to EtherCAT master was lost (or never established)
68 _logger.error(f"No connection to EtherCAT master ({url})")
69 except PDOError as e:
70 # This happens if PDO values can not be read (e.g. if master is not in OP state)
71 _logger.error(e)