class SlipTcpClient(SlipClient): def __init__(self, address, port, database, ostream, response_timeout=3.0, database_response_timeout=10.0): self.socket = SocketDevice('uart', '0', address, port) self.socket.start() super(SlipTcpClient, self).__init__(database, ostream, response_timeout, database_response_timeout) def write(self, buf): self.socket.write(buf) def read(self, size): buf = self.socket.read(size) if not buf: print('Connection closed.', file=self.ostream) _thread.interrupt_main() raise ClientClosedError() return buf
class Bus(object): """A stub communicating over a TCP socket instead of a CAN bus. """ def __init__(self, device, *args, **kwargs): del args del kwargs self.device = SocketDevice('can', device) self.device.start() def send(self, message): """Write given message to the application. """ line = "id={:08x},extended={},size={},data=".format( message.arbitration_id, 1 if message.extended_id else 0, len(message.data)) line = line.encode('ascii') line += binascii.hexlify(bytes(message.data)) line += b"\r\n" self.device.write(line) def recv(self): """Read a message from the application. """ line = self.device.readline() line = line.strip(b'\r\n') mo = re.match(rb'id=(\w{8}),extended=(\d),size=(\d),data=(\w+)', line) arbitration_id = int(mo.group(1), 16) extended_id = (mo.group(2) == b'1') length = int(mo.group(3)) data = bytearray(binascii.unhexlify(mo.group(4)[0:2*length])) return Message(arbitration_id, extended_id, data)
class Serial(object): """A stub communicating over a TCP socket instead of a serial port. """ def __init__(self, device, *args, **kwargs): del args del kwargs self.device = SocketDevice('uart', device) self.device.start() def close(self): self.device.stop() def write(self, buf): self.device.write(buf) def read(self, length=1): return self.device.read(length)
class Bus(object): """A stub communicating over a TCP socket instead of a CAN bus. """ def __init__(self, device, *args, **kwargs): del args del kwargs self.device = SocketDevice('can', device) self.device.start() def send(self, message): """Write given message to the application. """ line = "id={:08x},extended={},size={},data=".format( message.arbitration_id, 1 if message.extended_id else 0, len(message.data)) line = line.encode('ascii') line += binascii.hexlify(bytes(message.data)) line += b"\r\n" self.device.write(line) def recv(self): """Read a message from the application. """ line = self.device.readline() line = line.strip(b'\r\n') mo = re.match(rb'id=(\w{8}),extended=(\d),size=(\d),data=(\w+)', line) arbitration_id = int(mo.group(1), 16) extended_id = (mo.group(2) == b'1') length = int(mo.group(3)) data = bytearray(binascii.unhexlify(mo.group(4)[0:2 * length])) return Message(arbitration_id, extended_id, data)