Przeglądaj źródła

Fix serial echo causing GRBL communication failure on Pi 3B+

Root cause: On Pi 3B+ with /dev/ttyAMA0, the serial port had echo
enabled at the TTY level. This caused:
1. Sent G-code commands echoed back as "responses"
2. Motion thread waiting for 'ok' but receiving its own commands
3. Buffer corruption merging commands (G1 G53 -> G10G53)
4. GRBL error from corrupted command, stopping pattern

Fix: Disable ECHO and related flags via termios when opening the
serial connection. This ensures raw serial communication without
any TTY processing that could interfere.

Also re-enables real-time scheduling which wasn't the cause.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris 1 tydzień temu
rodzic
commit
382806095b

+ 17 - 0
modules/connection/connection_manager.py

@@ -74,6 +74,23 @@ class SerialConnection(BaseConnection):
         self.lock = threading.RLock()
         logger.info(f'Connecting to Serial port {port}')
         self.ser = serial.Serial(port, baudrate, timeout=timeout)
+
+        # Disable serial echo - critical for Pi 3B+ where ttyAMA0 may have echo enabled
+        # Without this, sent commands are echoed back and the motion thread never sees 'ok'
+        try:
+            import termios
+            # Get current terminal attributes
+            attrs = termios.tcgetattr(self.ser.fileno())
+            # Disable echo (ECHO) and other local flags that could cause issues
+            attrs[3] = attrs[3] & ~termios.ECHO & ~termios.ECHOE & ~termios.ECHOK & ~termios.ECHONL
+            # Set raw mode for input (no special processing)
+            attrs[3] = attrs[3] & ~termios.ICANON
+            # Apply the new attributes immediately
+            termios.tcsetattr(self.ser.fileno(), termios.TCSANOW, attrs)
+            logger.info(f'Serial echo disabled on {port}')
+        except Exception as e:
+            logger.warning(f'Could not disable serial echo (may not be needed): {e}')
+
         state.port = port
         logger.info(f'Connected to Serial port {port}')
 

+ 0 - 6
modules/core/scheduling.py

@@ -172,12 +172,6 @@ def setup_realtime_thread(tid: Optional[int] = None, priority: int = 50) -> None
         priority: SCHED_RR priority (1-99). Higher = more important.
                   Motion should use higher than LED (e.g., 60 vs 40).
     """
-    # TEMPORARILY DISABLED: Testing if SCHED_RR causes serial timeout on Pi 3B+
-    # The theory is that real-time scheduling on CPU 0 (which handles serial interrupts)
-    # may be blocking the kernel from delivering serial data to the thread.
-    logger.info(f"Real-time scheduling DISABLED for testing (would use priority {priority})")
-    return
-
     cpu_count = get_cpu_count()
 
     # Elevate priority (logs internally on success)