def recv(self, full_frame=False):
        """
		Receive a C1218Packet, the payload data is returned.

		:param bool full_frame: If set to True, the entire C1218 frame is
		  returned instead of just the payload.
		"""
        payloadbuffer = b''
        tries = 3
        while tries:
            tmpbuffer = self.serial_h.read(1)
            if tmpbuffer != b'\xee':
                self.loggerio.error(
                    'did not receive \\xee as the first byte of the frame')
                self.loggerio.debug(
                    'received \\x' +
                    binascii.b2a_hex(tmpbuffer).decode('utf-8') + ' instead')
                tries -= 1
                continue
            tmpbuffer += self.serial_h.read(5)
            sequence, length = struct.unpack('>xxxBH', tmpbuffer)
            payload = self.serial_h.read(length)
            tmpbuffer += payload
            chksum = self.serial_h.read(2)
            if chksum == packet_checksum(tmpbuffer):
                self.serial_h.write(ACK)
                data = tmpbuffer + chksum
                self.loggerio.debug(
                    "received frame, length: {0:<3} data: {1}".format(
                        len(data),
                        binascii.b2a_hex(data).decode('utf-8')))
                payloadbuffer += payload
                if sequence == 0:
                    if full_frame:
                        payloadbuffer = data
                    if sys.version_info[0] == 2:
                        payloadbuffer = bytearray(payloadbuffer)
                    return payloadbuffer
                else:
                    tries = 3
            else:
                self.serial_h.write(NACK)
                self.loggerio.warning('crc does not match on received frame')
                tries -= 1
        self.loggerio.critical('failed 3 times to correctly receive a frame')
        raise C1218IOError('failed 3 times to correctly receive a frame')
    def recv(self, full_frame=False):
        """
		Receive a C1218Packet, the payload data is returned.

		@type full_frame: Boolean
		@param full_frame: If set to True, the entire C1218 frame is returned
		instead of just the payload.
		"""
        payloadbuffer = ''
        tries = 3
        while tries:
            tmpbuffer = self.serial_h.read(1)
            if tmpbuffer != '\xee':
                self.loggerio.error(
                    'did not receive \\xee as the first byte of the frame')
                self.loggerio.debug('received \\x' + tmpbuffer.encode('hex') +
                                    ' instead')
                tries -= 1
                continue
            tmpbuffer += self.serial_h.read(3)
            sequence = ord(tmpbuffer[-1])
            length = self.serial_h.read(2)
            tmpbuffer += length
            length = struct.unpack('>H', length)[0]
            payload = self.serial_h.read(length)
            tmpbuffer += payload
            chksum = self.serial_h.read(2)
            if chksum == crc_str(tmpbuffer):
                self.serial_h.write(ACK)
                data = tmpbuffer + chksum
                self.loggerio.debug(
                    "received frame, length: {0:<3} data: {1}".format(
                        len(data), data.encode('hex')))
                payloadbuffer += payload
                if sequence == 0:
                    if full_frame:
                        return data
                    return payloadbuffer
                else:
                    tries = 3
            else:
                self.serial_h.write(NACK)
                self.loggerio.warning('crc does not match on received frame')
                tries -= 1
        self.loggerio.critical('failed 3 times to correctly receive a frame')
        raise C1218IOError('failed 3 times to correctly receive a frame')
Exemple #3
0
    def send(self, data):
        """
		This sends a raw C12.18 frame and waits checks for an ACK response.
		In the event that a NACK is received, this function will attempt
		to resend the frame up to 3 times.
		
		@type data: either a raw string of bytes which will be placed into
		a c1218.data.C1218Packet or a c1218.data.C1218Packet instance to
		be sent
		@param: the data to be transmitted
		"""
        if not isinstance(data, C1218Packet):
            data = C1218Packet(data)
        if self.toggle_control:  # bit wise, f**k yeah
            if self.__toggle_bit__:
                data.control = chr(ord(data.control) | 0x20)
                self.__toggle_bit__ = False
            elif not self.__toggle_bit__:
                if ord(data.control) & 0x20:
                    data.control = chr(ord(data.control) ^ 0x20)
                self.__toggle_bit__ = True
        elif self.toggle_control and not isinstance(data, C1218Packet):
            self.loggerio.warning(
                'toggle bit is on but the data is not a C1218Packet instance')
        data = str(data)
        self.loggerio.debug('sending frame, length: ' + str(len(data)) +
                            ' data: ' + hexlify(data))
        for pktcount in xrange(0, 3):
            self.write(data)
            response = self.serial_h.read(1)
            #print 'read send----' + hexlify(response) + '\n'
            if response == NACK:
                self.loggerio.warning('received a NACK after writing data')
                sleep(0.10)
            elif response == '':
                self.loggerio.error(
                    'received empty response after writing data')
                sleep(0.10)
            elif response != ACK:
                self.loggerio.error('received unknown response: ' +
                                    hex(ord(response)) + ' after writing data')
            else:
                return
        self.loggerio.critical('failed 3 times to correctly send a frame')
        raise C1218IOError('failed 3 times to correctly send a frame')
    def send(self, data):
        """
		This sends a raw C12.18 frame and waits checks for an ACK response.
		In the event that a NACK is received, this function will attempt
		to resend the frame up to 3 times.

		:param data: the data to be transmitted
		:type data: str, :py:class:`~c1218.data.C1218Packet`
		"""
        if not isinstance(data, C1218Packet):
            data = C1218Packet(data)
        if self.toggle_control:  # bit wise, f**k yeah
            if self._toggle_bit:
                data.set_control(ord(data.control) | 0x20)
                self._toggle_bit = False
            elif not self._toggle_bit:
                if ord(data.control) & 0x20:
                    data.set_control(ord(data.control) ^ 0x20)
                self._toggle_bit = True
        elif self.toggle_control and not isinstance(data, C1218Packet):
            self.loggerio.warning(
                'toggle bit is on but the data is not a C1218Packet instance')
        data = data.build()
        self.loggerio.debug("sending frame,  length: {0:<3} data: {1}".format(
            len(data),
            binascii.b2a_hex(data).decode('utf-8')))
        for pktcount in range(0, 3):
            self.write(data)
            response = self.serial_h.read(1)
            if response == NACK:
                self.loggerio.warning('received a NACK after writing data')
                time.sleep(0.10)
            elif len(response) == 0:
                self.loggerio.error(
                    'received empty response after writing data')
                time.sleep(0.10)
            elif response != ACK:
                self.loggerio.error('received unknown response: ' +
                                    hex(ord(response)) + ' after writing data')
            else:
                return
        self.loggerio.critical('failed 3 times to correctly send a frame')
        raise C1218IOError('failed 3 times to correctly send a frame')
Exemple #5
0
    def recv(self):
        """
		Receive a C1218Packet, the payload data is returned.
		"""
        payloadbuffer = ''
        tries = 3
        while tries:
            tmpbuffer = self.serial_h.read(1)
            #print 'tmpbuffer --- ' + hexlify(tmpbuffer) + '\n'
            if tmpbuffer != '\xee':
                self.loggerio.error(
                    'did not receive \\xee as the first byte of the frame')
                self.loggerio.debug('received \\x' + tmpbuffer.encode('hex') +
                                    ' instead')
                tries -= 1
                continue

            tmpbuffer += self.serial_h.read(3)
            #print 'tmpbuffer --- ' + hexlify(tmpbuffer) + '\n'
            sequence = ord(tmpbuffer[-1])
            length = self.serial_h.read(2)
            print 'length --- ' + hexlify(length) + '\n'
            tmpbuffer += length
            print 'tmpbuffer + length --- ' + hexlify(tmpbuffer) + '\n'
            tmpbuffer_log = tmpbuffer
            length = unpack('>H', length)[0]
            payload = self.serial_h.read(length)
            print 'payload   --- ' + hexlify(payload)
            tmpbuffer += payload
            print 'tmpbuffer + payload ---- ' + hexlify(tmpbuffer)
            chksum = self.serial_h.read(2)
            print 'chksum ---- ' + hexlify(chksum)

            self.cur = self.con.cursor()
            self.cur.execute(
                'SELECT * FROM response where response = ? and payload = ? and chksum  = ?',
                (str(hexlify(tmpbuffer_log)), str(
                    hexlify(payload)), str(hexlify(chksum))))
            rows = self.cur.fetchall()
            test = len(rows)
            lid = 0
            if test == 0:
                self.cur.execute(
                    'INSERT INTO response(response,payload,chksum) values (?,?,?)',
                    (str(hexlify(tmpbuffer_log)), str(
                        hexlify(payload)), str(hexlify(chksum))))
                lid = self.cur.lastrowid
            else:
                lid = rows[0][0]

            print " lid:" + str(lid)
            self.cur.execute(
                'INSERT INTO request(write,response_id) values (?,?)',
                (str(hexlify(self.write_data)), str(lid)))
            self.con.commit()
            self.write_log(',')
            self.write_log(hexlify(chksum))
            self.write_log('\n')
            if chksum == crc_str(tmpbuffer):
                self.serial_h.write(ACK)
                data = tmpbuffer + chksum
                self.loggerio.debug('received frame, length: ' +
                                    str(len(data)) + ' data: ' + hexlify(data))
                payloadbuffer += payload
                if sequence == 0:
                    return payloadbuffer
                else:
                    tries = 3
            else:
                print 'bad crc'
                self.serial_h.write(NACK)
                self.loggerio.warning('crc does not match on received frame')
                tries -= 1
        self.loggerio.critical('failed 3 times to correctly receive a frame')
        raise C1218IOError('failed 3 times to correctly receive a frame')
	def recv2(self, full_frame=False):
		"""
		Receive a C1218Packet, the payload data is returned.

		:param bool full_frame: If set to True, the entire C1218 frame is
		  returned instead of just the payload.
		"""
		payloadbuffer = b''
		tries = 3
		while tries:
			tmpbuffer = self.serial_h.read(1)
			#Debug Code - checked
			#print("connection.py - 100: {} and {}".format("tmpbuffer.read(1):",tmpbuffer))

			if tmpbuffer != b'\xee':
				#Debug Code - skipped
				#print("Inside tmpbuffer != b... ")
				self.loggerio.error('did not receive \\xee as the first byte of the frame')
				self.loggerio.debug('received \\x' + binascii.b2a_hex(tmpbuffer).decode('utf-8') + ' instead')
				tries -= 1
				continue
			tmpbuffer += b'\xee'
			tmpbuffer += self.serial_h.read(4)
			#Debug code
			#print("{} and {}".format("tmpbuffer += self.serial_h.read(4)",tmpbuffer))
			sequence, length = struct.unpack('>xxxBH', tmpbuffer)
			#Debug code
			#print("conenction.py - 282: sequence: {} and Length {}".format(sequence,length))
			payload = self.serial_h.read(length)
			#Debug code
			#print("connection.py - 199: Payload: {}".format(payload))
			tmpbuffer += payload
			#Debug code
			#print("196: tmpbuffer: {}".format(tmpbuffer))
			chksum = self.serial_h.read(2)
			#Debug code
			#print("chksum: {}".format(chksum))
			if chksum == packet_checksum(tmpbuffer):
				#Debug code
				#print("chksum: {} and packet_checksum(tmpbuffer): {}".format(chksum,packet_checksum(tmpbuffer)))
				#Debug code
				#print("ACK: {}".format(ACK))
				self.serial_h.write(ACK)
				#Debug code
				#print("tmpbuffer: {} and chksum {}".format(tmpbuffer,chksum))
				data = tmpbuffer + chksum
				#Debug code
				#print("210: data: {}".format(data))
				self.loggerio.debug("received frame, length: {0:<3} data: {1}".format(len(data), binascii.b2a_hex(data).decode('utf-8')))
				payloadbuffer += payload
				#Debug code
				#print("connection.py- 218 payloadbuffer: {}".format(payloadbuffer))
				if sequence == 0:
					#Debug code
					#print("217: sequence {} and full_frame {}".format(sequence,full_frame))
					if full_frame:
						#Debug code
						#print("220: Inside <if> full_frame: {}".format(full_frame))
						payloadbuffer = data
					#Debug code
					#print("223: sys.version_info[0]: {}".format(sys.version_info[0]))
					if sys.version_info[0] == 2:
						#Debug code
						#print("226: Inside <if> sys.version_info[0]: {}".format(sys.version_info[0]))
						payloadbuffer = bytearray(payloadbuffer)
					#Debug code
					#print("229: Before return payloadbuffer: {}".format(payloadbuffer))
					return payloadbuffer
					#Debug code
					#print("232:After return payloadbuffer: {}".format(payloadbuffer))
				else:
					#Debug Code
					#print("Inside Else")
					tries = 3
			else:
				#Debug Code
				#print("Inside Else")
				self.serial_h.write(NACK)
				self.loggerio.warning('crc does not match on received frame')
				tries -= 1
		#Debug Code
		#print("Does it fail?")
		self.loggerio.critical('failed 3 times to correctly receive a frame')
		raise C1218IOError('failed 3 times to correctly receive a frame')