serial-device/transport/serial/__init__.py

29 lines
607 B
Python
Raw Normal View History

2023-10-01 22:41:21 +03:00
import serial
from transport.base import TransportBase
class Transport(TransportBase):
def __init__(self, config):
super().__init__(config)
self.ser = serial.Serial()
self.ser.port = config["tty_name"]
self.ser.open()
self.ser.flushInput()
self.ser.flushOutput()
self.addr = None
def write_line(self, cmd_str):
2023-10-01 22:41:21 +03:00
self.ser.write(str.encode(cmd_str + "\n"))
return 1
def read_line(self):
2023-10-01 22:41:21 +03:00
buffer = self.ser.readline()
return buffer.decode('utf-8').rstrip()
def close(self):
self.ser.close()