Skip to content

pyserial

pyserial is a Python library for serial communication. It is used to read detection data from OSECHI and send commands programmatically.


Overview

Item Value
Role Serial communication with OSECHI from Python scripts
Package pyserial >= 3.5
Install Included in the dev dependency group (uv sync --group dev)

OSECHI Usage

OSECHI outputs detection events as JSONL (one JSON object per line) at 115200 baud over USB serial. pyserial lets you read this stream and send text commands from Python.

Read Detection Events

import serial
import json

with serial.Serial("/dev/cu.usbserial-0001", baudrate=115200, timeout=1) as ser:
    while True:
        line = ser.readline().decode("utf-8").strip()
        if line:
            event = json.loads(line)
            print(event)

Send a Command

import serial

with serial.Serial("/dev/cu.usbserial-0001", baudrate=115200, timeout=1) as ser:
    ser.write(b"GET_STATUS\n")
    response = ser.readline().decode("utf-8").strip()
    print(response)

Find the Serial Port

ls /dev/cu.usbserial-*
ls /dev/cu.SLAB_USBtoUART*
ls /dev/ttyUSB*
ls /dev/ttyACM*

Check Device Manager → Ports (COM & LPT) for the COM port number.


Troubleshooting

Problem Cause Fix
SerialException: [Errno 2] No such file Wrong port path Verify port with ls /dev/cu.*
SerialException: [Errno 13] Permission denied Port access denied (Linux) Add user to dialout group: sudo usermod -aG dialout $USER
Empty output Another process holds the port Close the serial monitor before running the script
Garbled output Wrong baud rate Use baudrate=115200

References