Files
ProjectorPi/serialdevice.py
2022-06-12 15:38:24 +02:00

35 lines
935 B
Python

from doctest import REPORT_CDIFF
from serial import Serial
class SerialDevice:
def __init__(
self, serial_port: str, baudrate: int = 9600, verbose: bool = False
) -> None:
self.serial_port: str = serial_port
self.baudrate = baudrate
self.verbose = verbose
self.prefix = ""
def send_command(self, command: str) -> str:
with Serial(self.serial_port, self.baudrate, timeout=3) as s:
if self.verbose:
print(self.prefix, "send:", command)
s.write(command.encode())
response_raw = s.readline()
if response_raw:
response = response_raw.decode().strip()
else:
response = ""
if not response:
print(self.prefix, "No response")
elif self.verbose:
print(self.prefix, "resp:", response)
return response