{"library":"pyserial","code":"import serial\nimport time\n\n# Configure the serial port\n# Replace 'COM3' with your actual serial port ('/dev/ttyUSB0' on Linux, 'COMx' on Windows)\n# Ensure baudrate matches your device\nser = serial.Serial(\n    port='COM_PORT_HERE', \n    baudrate=9600, \n    bytesize=serial.EIGHTBITS, \n    parity=serial.PARITY_NONE, \n    stopbits=serial.STOPBITS_ONE,\n    timeout=1 # Read timeout in seconds\n)\n\ntry:\n    if not ser.is_open:\n        ser.open()\n    print(f\"Serial port {ser.name} opened successfully.\")\n\n    # Write data (must be bytes in Python 3)\n    message_to_send = b\"Hello, device!\\n\"\n    ser.write(message_to_send)\n    print(f\"Sent: {message_to_send.decode().strip()}\")\n\n    time.sleep(0.1) # Give the device some time to respond\n\n    # Read data\n    received_data = ser.readline() # Reads until newline or timeout\n    if received_data:\n        print(f\"Received: {received_data.decode().strip()}\")\n    else:\n        print(\"No data received within timeout.\")\n\nexcept serial.SerialException as e:\n    print(f\"Serial port error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\nfinally:\n    if ser.is_open:\n        ser.close()\n        print(\"Serial port closed.\")\n","lang":"python","description":"This quickstart demonstrates how to open, configure, write to, and read from a serial port using PySerial. It includes error handling and ensures the port is closed. Remember to replace `'COM_PORT_HERE'` with your actual serial port name and adjust `baudrate` as needed for your device. Data sent and received must be handled as `bytes` in Python 3.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}