def handle(self): while True: data = self.request.recv(1024) if not data: break print('raw data received:', data) message = decode(data) print('decoded data:', message) data_to_send = encode(bytes(reversed(message))) print('raw data to send:', data_to_send) self.request.sendall(data_to_send)
UDP_IP = "192.168.32.13" #UDP_IP="127.0.0.1" UDP_PORT = 5566 print("Connecting to " + dev) ser = serial.Serial(dev, 115200, timeout=0.01) sock = socket.socket( socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP while True: try: data = ser.read(128) decoded = sliplib.decode(data) sock.sendto(decoded, (UDP_IP, UDP_PORT)) # print(decoded) except sliplib.ProtocolError as err: # print("protocol error: {0}".format(err)) pass except serial.SerialException as err: print("error reading {0} error: {1}".format(ser.port, err)) quit() pass except socket.error as err: # print("socket error: {0}".format(err)) pass except Exception as err: print("generic error: {0}".format(err)) quit()
def test_message_with_zero_byte_decoding(self): """A packet that contains a NULL byte must be decoded correctly.""" msg = b'a\0b' packet = END + msg + END assert decode(packet) == msg
def test_special_character_decoding(self, packet, msg): """A packet with special escape sequences should result in a message with the appropriate special bytes.""" packet = END + packet + END assert decode(packet) == msg
def test_single_byte_decoding(self): """A packet with a single byte between END bytes must be decoded correctly.""" msg = b'x' packet = END + msg + END assert decode(packet) == msg
def test_simple_message_decoding(self): """A packet without the special escape sequences should result in a message without special bytes.""" msg = b'hallo' packet = END + msg + END assert decode(packet) == msg
def test_empty_packet_decoding(self): """An empty packet should result in an empty message.""" packet = END + END assert decode(packet) == b''
def test_invalid_packet_raises_protocol_error(self, packet): """A packet with an invalid escape sequence should result in a ProtocolError.""" packet = END + packet + END with pytest.raises(ProtocolError) as exc_info: decode(packet) assert exc_info.value.args == (packet, )
import serial import socket import glob UDP_IP = "192.168.32.13" UDP_PORT = 5566 serial_ports = list() for dev in glob.glob("/dev/ttyUSB*"): print("Connect to " + dev) serial_ports.append(serial.Serial(dev, 115200, timeout=0.01)) sock = socket.socket( socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP print("Start reading...") while True: for ser in serial_ports: try: # print("read " + ser.port) data = ser.read(128) decoded = decode(data) sock.sendto(decoded, (UDP_IP, UDP_PORT)) # print(decoded) except: # nothing pass