Beispiel #1
0
 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')
Beispiel #2
0
def send_ping_cmd(serial):
    #in python, we can simply write down the MessagePack message and it is automatically built
    #here is an order called "ping" which has a string as data
    msg = msgpack.packb({'ping': 'Hello there :-)'}, use_single_float=True)
    dtgrm = serial_datagram.encode(msg)
    serial.write(dtgrm)
    serial.flush()
Beispiel #3
0
 def test_end_marker(self):
     """
     Checks that the end marker is added.
     """
     data = b'\x00' * 3
     datagram = serial_datagram.encode(data)
     self.assertEqual(b'\xc0', datagram[-1:], "No end marker")
Beispiel #4
0
 def test_esc_is_escaped(self):
     """
     Checks that the ESC byte is escaped.
     """
     data = b'\xDB' # esc marker
     datagram = serial_datagram.encode(data)
     expected = b'\xDB\xDD'  # transposed esc
     self.assertEqual(expected, datagram[0:2], "ESC was not correctly escaped")
Beispiel #5
0
 def test_that_weird_sequences_work(self):
     """
     Decode order matters. This can be revealed by making a packet
     containing ESC + ESC_END, which would cause a problem if the replace
     happens in the wrong order.
     """
     data = serial_datagram.ESC + serial_datagram.ESC_END
     datagram = serial_datagram.encode(data)
     self.assertEqual(data, serial_datagram.decode(datagram))
Beispiel #6
0
    def test_end_is_replaced(self):
        """
        Checks that the end byte is escaped.
        """
        data = b'\xC0'  # end marker is 0xC0
        datagram = serial_datagram.encode(data)
        expected = b'\xDB\xDC'  # transposed end

        self.assertEqual(expected, datagram[0:2], "END was not correctly escaped")
Beispiel #7
0
    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))
Beispiel #8
0
    def test_crc_encapsulation(self):
        """
        Checks that the CRC is correctly encapsulated at the end of the datagram.
        """
        data = b'\x00' * 3
        expected_crc = struct.pack('>I', 4282505490)

        datagram = serial_datagram.encode(data)
        self.assertEqual(expected_crc, datagram[-5:-1], "CRC mismatch")
Beispiel #9
0
def send_set_led_cmd(serial, led, action):
    #in python, we can simply write down the MessagePack message and it is automatically built
    #here is an order called "ping" which has a string as data
    #max uint8_t
    led = led & 255
    action = action & 255

    msg = msgpack.packb({'set_led': [led, action]}, use_single_float=True)
    dtgrm = serial_datagram.encode(msg)
    serial.write(dtgrm)
    serial.flush()
 def sock_send(self, pkg):
     try:
         self.dev.write(serial_datagram.encode(pkg))
     except serial.SerialException:
         self.reopen()
def velocity_set(serial_port, speed):
    msg = msgpack.packb({'velocity_set': [True, float(speed)]},  use_single_float=True)
    dtgrm = serial_datagram.encode(msg)
    serial_port.write(dtgrm)
    serial_port.flush()
Beispiel #12
0
def encode_call(method_name, params):
    """
    Encode a call to the given method with the given parameters.
    """
    data = msgpack.packb([method_name, params], use_single_float=True)
    return serial_datagram.encode(data)
 def sock_send(self, pkg):
     try:
         self.dev.write(serial_datagram.encode(pkg))
     except serial.SerialException:
         self.reopen()
Beispiel #14
0
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))

Beispiel #15
0
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 position_set(serial_port, position):
    msg = msgpack.packb({'position_set': [True, float(position)]},
                        use_single_float=True)
    dtgrm = serial_datagram.encode(msg)
    serial_port.write(dtgrm)
    serial_port.flush()
Beispiel #17
0
def encode_call(method_name, params):
    """
    Encode a call to the given method with the given parameters.
    """
    data = msgpack.packb([method_name, params], use_single_float=True)
    return serial_datagram.encode(data)
def current_set(serial_port, current):
    msg = msgpack.packb({'current_set': [True, float(current)]},  use_single_float=True)
    dtgrm = serial_datagram.encode(msg)
    serial_port.write(dtgrm)
    serial_port.flush()