From 07e207c27d79ecfb9e476ec9e6f479cfddb5e2e5 Mon Sep 17 00:00:00 2001
From: Martin Teichmann <martin.teichmann@xfel.eu>
Date: Tue, 5 Sep 2023 15:33:07 +0200
Subject: [PATCH] hammer the terminals into operational mode

constantly assure that the terminals are in operational mode.

They should not drop out of operational, but if we are slow, they
do.
---
 ebpfcat/ebpfcat.py  | 16 ++++++++++++++++
 ebpfcat/ethercat.py |  7 ++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/ebpfcat/ebpfcat.py b/ebpfcat/ebpfcat.py
index b4d3af1..f5d688f 100644
--- a/ebpfcat/ebpfcat.py
+++ b/ebpfcat/ebpfcat.py
@@ -504,6 +504,22 @@ class SyncGroup(SyncGroupBase):
             dev.update()
         return self.current_data
 
+    async def to_operational(self):
+        try:
+            r = await gather(*[t.to_operational() for t in self.terminals])
+
+            while True:
+                for t in self.terminals:
+                    state, error = await t.get_state()
+                    if state != 8:  # operational
+                        print(f"ERROR AL register {error}")
+                    await t.to_operational()
+                await sleep(1)
+        except Exception:
+            import traceback
+            traceback.print_exc()
+            raise
+
     def start(self):
         self.allocate()
         self.packet_index = SyncGroup.packet_index
diff --git a/ebpfcat/ethercat.py b/ebpfcat/ethercat.py
index f82e9f4..aa1b477 100644
--- a/ebpfcat/ethercat.py
+++ b/ebpfcat/ethercat.py
@@ -546,9 +546,10 @@ class Terminal:
         return ret
 
     async def get_state(self):
-        """get the current state"""
-        ret, = await self.ec.roundtrip(ECCmd.FPRD, self.position, 0x0130, "H")
-        return ret
+        """get the current state and error flags"""
+        state, error = await self.ec.roundtrip(ECCmd.FPRD, self.position,
+                                               0x0130, "H2xH")
+        return state, error
 
     async def to_operational(self, target=8):
         """try to bring the terminal to operational state
-- 
GitLab