예제 #1
0
    def _get_picture(self):
        """
        sends the GET PICTURE command and receives the corresponding DATA command.
        Returns the number of packets to be read.
        """
        self._write(commands.get_picture('01'))
        assert self._matches(commands.ack('04', '..'), self._wait_for_bytes(6))
        # receive DATA
        data = self._wait_for_bytes(6)
        print("data is ", data)
        assert self._matches(commands.data('01', '..', '..', '..'), data)

        print("hexlify(data) is: ", hexlify(data))

        print("data is", data)
        print("data[-3:] is ", data[-3:])

        # below line is too redundant... to avoid "UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 0: invalid start byte"
        # img_size = unpack('<I', (codecs.decode(codecs.encode(unhexlify(hexlify(data[-3:])), 'hex'), 'hex') + b'\x00'))[0]
        # img_size = unpack('<I', (unhexlify(hexlify(data[-3:])) + b'\x00'))[0]

        # this is simple, maybe best
        img_size = unpack('<I', (data[-3:] + b'\x00'))[0]

        print("image size is {}".format(img_size))

        ### num_pkgs must be int
        # num_pkgs = img_size / (512 - 6)
        num_pkgs = math.floor(img_size / (512 - 6))

        print("num packages: {}".format(num_pkgs))

        self._write(commands.ack('00', '00'))
        return img_size
예제 #2
0
    def _write_picture(self, img_size, name='pic.jpeg'):

        ### num_pkgs must be int
        # num_pkgs = img_size / (512 - 6)
        num_pkgs = math.floor(img_size / (512 - 6))

        with open(name, 'wb+') as f:
            for i in range(1, num_pkgs + 1):
                # print("getting package {}".format(i))
                read = self._wait_for_bytes(512)
                # print(read)
                f.write(read[4:-2])

                ### hex_idx must be str
                # hex_idx = hexlify(pack('H', i))
                hex_idx = hexlify(pack('H', i)).decode()

                # print("hex_idx is {}".format(hex_idx))
                self._write(commands.ack('00', '00', hex_idx[:2],
                                         hex_idx[-2:]))
            f.write(self._wait_for_bytes(img_size - num_pkgs * (512 - 6) + 2))
            f.close()
        # ACK end of data transfer
        self._write(commands.ack('f0', 'f0'))
        print("taken picture, finish!")
예제 #3
0
 def _sync(self):
     time.sleep(.05)
     self._write(commands.sync())
     read = self.ser.read(6)
     if self._matches(commands.ack('0d', '..'), read):
         if self._matches(commands.sync(), self.ser.read(6)):
             self._write(commands.ack('0d', '00'))
             return True
     return False
예제 #4
0
    def _get_picture(self):
        """
        sends the GET PICTURE command and receives the corresponding DATA command.
        Returns the number of packets to be read.
        """
        self._write(commands.get_picture('02'))
        assert self._matches(commands.ack('04', '..'), self._wait_for_bytes(6))
        # receive DATA
        data = self._wait_for_bytes(6)
        assert self._matches(commands.data('02', '..', '..', '..'), data)
        img_size = unpack('<I', (data[-3:] + b'\x00'))[0]
        self._write(commands.ack('00', '00'))

        return img_size
예제 #5
0
 def _write_picture(self, name):
     read = self._wait_for_bytes(4800)
     gray = np.array(read).reshape(60, 80)
     cv2.imshow('image', gray)
     cv2.waitKey(1)
     # ACK end of data transfer
     self._write(commands.ack('0A', '00', '01', '00'))
예제 #6
0
 def _write_picture(self, name):
     read = self._wait_for_bytes(4800)
     gray = np.array(read).reshape(60, 80)
     img = Image.fromarray(gray)
     img.save(name)
     # ACK end of data transfer
     self._write(commands.ack('0A', '00', '01', '00'))
     print("taken picture, finish!")
예제 #7
0
    def _initial(self):
        init_cmd = commands.initial('03', '01', '03')

        self._write(init_cmd)

        read = self._wait_for_bytes(6)

        assert self._matches(commands.ack('01', '..'), read)
예제 #8
0
    def _get_picture(self):
        """
        sends the GET PICTURE command and receives the corresponding DATA command.
        Returns the number of packets to be read.
        """
        self._write(commands.get_picture('02'))
        assert self._matches(commands.ack('04', '..'), self._wait_for_bytes(6))
        # receive DATA
        data = self._wait_for_bytes(6)
        print("data is ", data)
        assert self._matches(commands.data('02', '..', '..', '..'), data)

        print("hexlify(data) is: ", hexlify(data))

        img_size = unpack('<I', (data[-3:] + b'\x00'))[0]

        print("image size is {}".format(img_size))

        self._write(commands.ack('00', '00'))

        return img_size
예제 #9
0
    def _write_picture(self, img_size, name='pic.jpeg'):

        num_pkgs = 1

        read = self._wait_for_bytes(4800)
        print(hexlify(read))
        gray = np.array(read).reshape(60, 80)
        img = Image.fromarray(gray)
        img.save('test.png')
        # ACK end of data transfer
        self._write(commands.ack('0A', '00', '01', '00'))
        print("taken picture, finish!")
예제 #10
0
    def _initial(self):
        init_cmd = commands.initial('07', '07', '07')
        print("init cmd {}".format(init_cmd))

        self._write(init_cmd)
        # print("send init commandだよ")
        # print(init_cmd)
        # self.ser.write(bytearray(unhexlify('aa0100070707')))

        read = self._wait_for_bytes(6)

        print("return ack, maybe AA 0E 01 xx 00 00")
        print('ack {}'.format(read))

        assert self._matches(commands.ack('01', '..'), read)
예제 #11
0
 def ack(self, frame, receipt=None):
     """Create an **ACK** frame for a received **MESSAGE** frame."""
     self.__check('ack', [self.CONNECTED])
     frame = commands.ack(frame, self._transactions, receipt)
     self._receipt(receipt)
     return frame
예제 #12
0
 def _set_baudrate(self, first_divider, second_divider):
     self._write(commands.set_baudrate('01', '00'))
     assert self._matches(commands.ack('07', '..'), self._wait_for_bytes(6))
예제 #13
0
 def _snapshot(self):
     self._write(commands.snapshot('00', '00', '00'))
     assert self._matches(commands.ack('05', '..'), self._wait_for_bytes(6))
예제 #14
0
 def _set_pkg_size(self):
     # set package size 512 bytes
     self._write(commands.set_pkg_size('00', '02'))
     assert self._matches(commands.ack('06', '..'), self._wait_for_bytes(6))