def test_can_read_datagram(self): """ Checks if we can read a whole datagram. """ fd = BytesIO(serial_datagram.encode(b'\x01\x02\x03')) dt = serial_datagram.read(fd) self.assertEqual(dt, b'\x01\x02\x03')
def test_what_happens_if_timeout(self): """ Checks that we return None if there was a timeout. """ data = serial_datagram.encode(b'\x01\x02\x03') # Introduce a timeout before receiving the last byte fd = BytesIO(data[:-1]) self.assertIsNone(serial_datagram.read(fd))
def receiver(port): global error_count, data_count, datagram_count while True: try: data = serial_datagram.read(port) data_count += len(data) datagram_count += 1 except (serial_datagram.CRCMismatchError, serial_datagram.FrameError): error_count += 1
def run(self): while True: try: dtgrm = serial_datagram.read(self.fdesc) data = msgpack.unpackb(dtgrm) for idx, var in enumerate(self.variables): val = variable_path_extract_value(var, data) if val is not None: plot.lines[idx].updateData(val) except (serial_datagram.CRCMismatchError, serial_datagram.FrameError): print("CRC error")
def sock_recv(self): while True: try: return serial_datagram.read(self.dev) except (serial_datagram.CRCMismatchError, serial_datagram.FrameError): print("CRC error") except serial.serialutil.SerialException as e: print(e) self.reopen() except TypeError as e: # serial doesn't correctly handle the OSError exception on OS X if str(e) == "'OSError' object is not subscriptable": print('serial device error') self.reopen() else: raise e
def main(): if len(sys.argv) > 2: baud = sys.argv[2] else: baud = 115200 if len(sys.argv) == 1: fdesc = os.fdopen(0, "rb") else: fdesc = serial.Serial(sys.argv[1], baudrate=baud) while True: try: dtgrm = serial_datagram.read(fdesc) data = msgpack.unpackb(dtgrm) pprint(data) except (serial_datagram.CRCMismatchError, serial_datagram.FrameError): sys.stderr.write("CRC error")
import serial import serial_datagram import sys import time import statistics dev = serial.Serial(sys.argv[1], 115200) # dev.write(serial_datagram.encode(b'\x42\xaa\x00'+b'hello')) # dev.write(serial_datagram.encode(b'\x42\xaa\x00'+b'hello')) dev.write(serial_datagram.encode(b'\x42\xaa\x00' + b'hello')) # print("write done") s = [] for i in range(1000): t1 = time.time() dev.write(serial_datagram.encode(b'\x42\xaa\x00' + b'hello' * 10)) res = serial_datagram.read(dev) t2 = time.time() s.append(t2 - t1) # print(res) print('mean {} ms'.format(statistics.mean(s) * 1000)) print('std {} ms'.format(statistics.stdev(s) * 1000)) print('max {} ms'.format(max(s) * 1000)) print('below 1 ms: {}'.format(sum(1 for i in s if i < 0.001) / len(s))) print('below 2 ms: {}'.format(sum(1 for i in s if i < 0.002) / len(s)))
def main(): if len(sys.argv) > 2: baud = sys.argv[2] else: baud = 115200 if len(sys.argv) == 1: print('Please give the serial port to use') return else: fdesc = serial.Serial(sys.argv[1], baudrate=baud) #unsed to align the texts correctly names = ('accelerometer', 'gyroscope', 'time', 'battery', 'distance') max_length = len(max(names, key=len)) #value for the rgb led blue = 255 while True: try: #return a complete decoded frame dtgrm = serial_datagram.read(fdesc) #unpack the message pack frame data = msgpack.unpackb(dtgrm, use_list=False, encoding='utf-8') #{} is replaced by the variables in the .format() function #{:<{}} means the text is aligned to the left and the max size of the text is given by #a variable in the .format() function #{:6.2f} means the number is a float with 6 digits and a precision of two #print acc datas only if they are present in the frame received field_to_found = 'acc' if (field_to_found in data): buf = data[field_to_found] print('{:<{}} : x = {:6.2f}, y = {:6.2f}, z = {:6.2f}'.format( names[0], max_length, buf[0], buf[1], buf[2])) #send a ping command if we get the acc field in the response send_ping_cmd(fdesc) send_set_led_cmd(fdesc, 0, 2) if (blue): blue = 0 else: blue = 255 send_set_rgb_led_cmd(fdesc, 0, 0, 0, blue) #print gyro datas only if they are present in the frame received field_to_found = 'gyro' if (field_to_found in data): buf = data[field_to_found] print('{:<{}} : x = {:6.2f}, y = {:6.2f}, z = {:6.2f}'.format( names[1], max_length, buf[0], buf[1], buf[2])) #print time data only if they are present in the frame received field_to_found = 'time' if (field_to_found in data): time = data[field_to_found] print('{:<{}} : {:.2f} seconds\n'.format( names[2], max_length, time)) #print time data only if they are present in the frame received field_to_found = 'batt' if (field_to_found in data): batt = data[field_to_found] print('{:<{}} : {:.2f} V\n'.format(names[3], max_length, batt)) #print time data only if they are present in the frame received field_to_found = 'dist' if (field_to_found in data): dist = data[field_to_found] print('{:<{}} : {} mm\n'.format(names[4], max_length, dist)) #print the ping response field_to_found = 'ping' if (field_to_found in data): print(data) except (serial_datagram.CRCMismatchError, serial_datagram.FrameError): sys.stderr.write("CRC error")
import serial import serial_datagram import sys dev = serial.Serial(sys.argv[1], 115200) dev.write(serial_datagram.encode(b'\x43\xaa\x00'+b'hello')) while 1: print(serial_datagram.read(dev))