tic2mqtt/tic_device.py

42 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-05-19 18:23:53 +02:00
import logging
log = logging.getLogger(__name__)
class TicDevice:
def __init__(self, name):
self._is_serial = False
self.device = None
try:
self.device = serial.Serial(name,
bytesize=serial.SEVENBITS,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
timeout=2.0)
self.device.baudrate = 9600
self._is_serial = True
except:
if self.device:
self.device.close()
self.device = None
if not self.device:
self.device = open(name, 'rb')
self._is_serial = False
def set_baudrate(self, rate):
if self._is_serial:
self.device.baudrate = rate
def get_char(self):
c = self.device.read(1)
if not c:
if self._is_serial:
log.debug("RX Timeout")
else:
log.debug("EOF.")
return c
def is_serial(self):
return self._is_serial