a1ea02771c
Drop timeout_ms; read now returns immediately with whatever data is available (0 if none), so callers don't block the rest of their loop waiting on serial I/O. telemetry's poll loop now sleeps interval_ms itself between empty reads via a small sleep_ms helper.
35 lines
1.2 KiB
C
35 lines
1.2 KiB
C
#pragma once
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
/* Opaque serial port handle */
|
|
typedef struct cel_serial_port cel_serial_port;
|
|
|
|
/* Open a serial port. path is platform-specific:
|
|
* Windows: "COM3"
|
|
* Linux/macOS: "/dev/ttyUSB0"
|
|
* baud_rate: 400000 is standard for ELRS CRSF
|
|
* Returns NULL on failure. */
|
|
cel_serial_port* cel_serial_open(char const* path, int baud_rate);
|
|
|
|
/* Close and free the serial port */
|
|
void cel_serial_close(cel_serial_port* port);
|
|
|
|
/* Read up to len bytes without blocking.
|
|
* Returns bytes read immediately available, or 0 if none/error. */
|
|
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len);
|
|
|
|
/* Write data. Returns bytes written, 0 on error. */
|
|
size_t cel_serial_write(cel_serial_port* port, uint8_t const* buf, size_t len);
|
|
|
|
/* Flush output buffer */
|
|
void cel_serial_flush(cel_serial_port* port);
|
|
|
|
/* List available serial ports.
|
|
* Returns the number of ports found, or -1 on error.
|
|
* Caller must free the returned array with cel_serial_free_ports(). */
|
|
int cel_serial_list_ports(char*** out_ports, int max_ports);
|
|
|
|
/* Free the port list returned by cel_serial_list_ports */
|
|
void cel_serial_free_ports(char** ports, int count);
|