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