import serial import time import sys def run_diagnostic(port): print(f"Opening serial port {port}...") try: ser = serial.Serial(port, 115200, timeout=1) except Exception as e: print("Failed to open serial port:", e) return False print("Interrupting board execution (Ctrl+C)...") for _ in range(30): ser.write(b'\x03') time.sleep(0.02) time.sleep(0.5) ser.read_all() print("Entering raw REPL...") ser.write(b'\x01') time.sleep(0.2) resp = ser.read_all() if b'raw REPL' not in resp and b'OK' not in resp and b'raw' not in resp: print("Could not enter raw REPL:", resp) ser.close() return False # Python code to test playback test_code = """ import audio_util try: print("Starting play_tone test...") audio_util.play_tone(440, 1000, 50) print("play_tone test finished!") except Exception as e: import sys sys.print_exception(e) """ print("Running diagnostic code on the board...") # Send code ser.write(test_code.encode('utf-8')) ser.write(b'\x04') # Ctrl+D to execute time.sleep(0.2) # Read output print("\n--- Board Output ---") start = time.time() while time.time() - start < 5.0: line = ser.readline() if line: print(line.decode('utf-8', 'ignore').strip()) # Reset the board to return to normal print("Resetting board to normal...") ser.write(b'\x02') # Ctrl+B to exit raw REPL time.sleep(0.1) ser.write(b'\x03') time.sleep(0.1) ser.write(b"import machine\nmachine.reset()\n") ser.close() return True if __name__ == "__main__": run_diagnostic('/dev/cu.usbmodem101')