Add pont-bascule-api project
This commit is contained in:
26
serial_bridge.py
Normal file
26
serial_bridge.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import threading
|
||||
from dataclasses import dataclass
|
||||
|
||||
def hex2b(s: str) -> bytes:
|
||||
return bytes(int(x, 16) for x in s.split())
|
||||
|
||||
@dataclass
|
||||
class SerialConfig:
|
||||
port: str
|
||||
baudrate: int = 9600
|
||||
|
||||
class SerialBridge:
|
||||
def __init__(self, cfg: SerialConfig):
|
||||
self.cfg = cfg
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def busy(self):
|
||||
return self._lock.locked()
|
||||
|
||||
def send_and_read_once(self, payload: bytes):
|
||||
if not self._lock.acquire(blocking=False):
|
||||
return {"busy": True, "error": "BUSY"}
|
||||
try:
|
||||
return {"ok": True, "request": payload.hex()}
|
||||
finally:
|
||||
self._lock.release()
|
||||
Reference in New Issue
Block a user