Esempio n. 1
0
 def __unpack_header(self, data):
     pay_len = len(data) - 4
     ncmd, length, cmd_data = struct.unpack('!bb%ds' % pay_len,
                                            check_crc(data))
     if pay_len != length:
         raise LengthError("Wrong command length")
     return ncmd, length, cmd_data
Esempio n. 2
0
 def __unpack_header(self, data):
     pay_len = len(data) - 4
     ncmd, length, cmd_data = struct.unpack('!bb%ds' % pay_len,
                                            check_crc(data))
     if pay_len != length:
         raise LengthError("Wrong command length")
     return ncmd, length, cmd_data
Esempio n. 3
0
    def test_check_crc(self):
        packets = [
            bytearray([0x00, 0x61, 0x61]),
            bytearray([0x02, 0xbc, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67]),
        ]

        for packet in packets:
            assert check_crc(packet) == packet[2:]

        self.assertRaises(CRCError, check_crc, bytearray([0x00, 0x61, 0x62]))
Esempio n. 4
0
    def send_command(self, cmd, ret_fmt):
        """Build a command packet, send it to the openDAQ and process the
        response

        Args:
            cmd: Command ID
            ret_fmt: Payload format using python 'struct' format characters
        Returns:
            Command ID and arguments of the response
        Raises:
            LengthError: The legth of the response is not the expected
        """
        if self.measuring:
            self.stop()

        # Add 'command' and 'length' fields to the format string
        fmt = '!BB' + ret_fmt
        ret_len = 2 + struct.calcsize(fmt)
        packet = crc(cmd) + cmd
        self.ser.write(packet)
        ret = self.ser.read(ret_len)
        if self.debug:
            print 'Command:  ',
            for c in packet:
                print '%02X' % ord(c),
            print
            print 'Response: ',
            for c in ret:
                print '%02X' % ord(c),
            print

        if ret == NAK:
            raise IOError("NAK response received")

        data = struct.unpack(fmt, check_crc(ret))

        if len(ret) != ret_len:
            raise LengthError("Bad packet length %d (it should be %d)" %
                              (len(ret), ret_len))
        if data[1] != ret_len-4:
            raise LengthError("Bad body length %d (it should be %d)" %
                              (ret_len-4, data[1]))
        # Strip 'command' and 'length' values from returned data
        return data[2:]
Esempio n. 5
0
    def send_command(self, command, ret_fmt):
        """Build a command packet, send it to the openDAQ and process the
        response

        Args:
            cmd: Command string
            ret_fmt: Payload format of the response using python
            'struct' format characters
        Returns:
            Command ID and arguments of the response
        Raises:
            LengthError: The legth of the response is not the expected
        """
        fmt = '!BB' + ret_fmt
        ret_len = 2 + struct.calcsize(fmt)
        self.ser.write(command)
        ret = self.ser.read(ret_len)
        if self.debug:
            print 'Command:  ',
            for c in command:
                print '%02X' % ord(c),
            print
            print 'Response: ',
            for c in ret:
                print '%02X' % ord(c),
            print

        if ret == NAK:
            raise IOError("NAK response received")

        data = struct.unpack(fmt, check_crc(ret))

        if len(ret) != ret_len:
            raise LengthError("Bad packet length %d (it should be %d)" %
                              (len(ret), ret_len))
        if data[1] != ret_len - 4:
            raise LengthError("Bad body length %d (it should be %d)" %
                              (ret_len - 4, data[1]))
        # Strip 'command' and 'length' values from returned data
        return data[2:]
Esempio n. 6
0
    def test_check_crc(self):
        assert check_crc('\x00\x61' + 'a') == 'a'
        assert check_crc('\x02\xbc' + 'abcdefg') == 'abcdefg'

        self.assertRaises(CRCError, check_crc, '\x00a' + 'b')
Esempio n. 7
0
    def test_check_crc(self):
        assert check_crc('\x00\x61' + 'a') == 'a'
        assert check_crc('\x02\xbc' + 'abcdefg') == 'abcdefg'

        self.assertRaises(CRCError, check_crc, '\x00a' + 'b')