34 from serial
import SerialException
35 from serial.tools
import list_ports
40 def setup(vid_pid=None, com_port='', baudrate=921600):
45 devices = UartInterface.enum_device(vid_pid)
48 raise RuntimeError(
"No UART device found with {}".format(vid_pid))
53 except SerialException:
56 raise RuntimeError(
"Cound not connect to any device with {}".format(vid_pid))
58 def __init__(self, port, baud=1000000, rtscts=True):
61 self.
__ser = serial.Serial(port, baud, rtscts=rtscts)
62 self.__ser.reset_input_buffer()
63 self.__ser.reset_output_buffer()
73 return self.__ser.write(message)
75 raise RuntimeError(
'Error writing to device')
77 def read(self, number_of_bytes):
79 return bytearray(self.__ser.read(number_of_bytes))
81 raise RuntimeError(
'Error while reading from device')
87 self.__ser.timeout = 0
97 return self.__ser.timeout
100 prev_timeout = self.__ser.timeout
101 self.__ser.timeout = new_timeout
109 self.__ser.reset_input_buffer()
110 self.__ser.reset_output_buffer()
114 return list_ports.grep(vid_pid)
def set_timeout(self, new_timeout)
def set_mode_nonblocking(self)
def set_mode_blocking(self)
def setup(vid_pid=None, com_port='', baudrate=921600)
def read(self, number_of_bytes)
def __init__(self, port, baud=1000000, rtscts=True)