def open(self, port: str = DEFAULT_PORT, baudrate: int = DEFAULT_BAUDRATE): try: self._serial = serial.serial_for_url(port) self._serial.baudrate = baudrate self._serial.timeout = self._timeout self._serial.write_timeout = self._timeout except (serial.SerialException, ValueError) as e: raise InterfaceIOError(str(e))
def read(self): rx_bytes = self._connection.recv(TcpSocketBase.DEFAULT_RECV_CHUNK_SIZE) if len(rx_bytes) == 0: msg = 'Connection remotely closed' log.error(msg) raise InterfaceIOError(msg) log.debug('RX bytes: {}'.format(str(rx_bytes))) return rx_bytes
def _wrapper(self, *args, **kwargs): try: ret = func(self, *args, **kwargs) except serial.SerialException as e: msg = str(e) log.error(msg) raise InterfaceIOError(str(e)) return ret
def _wrapper(self, *args, **kwargs): try: ret = func(self, *args, **kwargs) except socket.error as e: msg = str(e) if msg == 'timed out': raise InterfaceTimeoutError(str(e)) raise InterfaceIOError(str(e)) except socket.timeout as e: raise InterfaceTimeoutError(str(e)) return ret
def write(self, bytes_buf): tx_size = 0 total_size = len(bytes_buf) while tx_size < total_size: size = self._connection.send(bytes_buf[tx_size:]) if size <= 0: msg = 'Connection remotely closed' log.error(msg) raise InterfaceIOError(msg) tx_size += size log.debug('TX bytes: {}'.format(str(bytes_buf)))
def connect(self, host: str = TcpSocketBase.DEFAULT_HOST, port: int = TcpSocketBase.DEFAULT_PORT): server_address = ( host, port, ) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(self._timeout) log.info('Connecting to: {}'.format(str(server_address))) try: self.sock.connect(server_address) except socket.error as e: log.error('Connecting error: {}'.format(str(e))) raise InterfaceIOError(str(e)) log.info('Connected to: {}'.format(str(server_address))) self._connection = self.sock
def _wrapper(self, *args, **kwargs): if not self._serial: raise InterfaceIOError('Serial port closed') return func(self, *args, **kwargs)
def _wrapper(self, *args, **kwargs): if not self._connection: raise InterfaceIOError('Connection not established') return func(self, *args, **kwargs)