Esempio n. 1
0
 def readpacket(self, timeout=None):
     total_read = 4
     initial_read = self.read(total_read)
     if initial_read != []:
         all_data = initial_read
         if ((sys.version_info.major > 2) and (initial_read[0] == 1)) or \
            ((sys.version_info.major <= 2) and ord(initial_read[0]) == 1):
             command = initial_read[3]
             data_number = struct.unpack('<H', initial_read[1:3])[0]
             if data_number > 6:
                 toread = abs(data_number - 6)
                 second_read = self.read(toread)
                 all_data += second_read
                 total_read += toread
                 out = second_read
             else:
                 out = ''
             suffix = self.read(2)
             if len(suffix) < 2:
                 raise constants.Error('Packet header too short!')
             sent_crc = struct.unpack('<H', suffix)[0]
             local_crc = crc16.crc16(all_data, 0, total_read)
             if sent_crc != local_crc:
                 raise constants.CrcError("readpacket Failed CRC check")
             return ReadPacket(command, out)
         else:
             raise constants.Error('Error reading packet header!')
     return None
Esempio n. 2
0
 def readpacket(self, timeout=None):
     total_read = 4
     try:
         initial_read = self.read(total_read)
     except serial.SerialException as e:
         #print ('readpacket() : Exception =', e)
         if sys.version_info < (3, 0):
             sys.exc_clear()
         return None
     if initial_read != []:
         all_data = initial_read
         if ord(initial_read[0]) == 1:
             command = initial_read[3]
             data_number = struct.unpack('<H', initial_read[1:3])[0]
             if data_number > 6:
                 toread = abs(data_number - 6)
                 second_read = self.read(toread)
                 all_data += second_read
                 total_read += toread
                 out = second_read
             else:
                 out = ''
             suffix = self.read(2)
             if len(suffix) < 2:
                 raise constants.Error('Packet header too short!')
             sent_crc = struct.unpack('<H', suffix)[0]
             local_crc = crc16.crc16(all_data, 0, total_read)
             if sent_crc != local_crc:
                 raise constants.CrcError("readpacket Failed CRC check")
             num1 = total_read + 2
             return ReadPacket(command, out)
         else:
             raise constants.Error('Error reading packet header!')
Esempio n. 3
0
 def WritePacket(self, packet):
     if not packet:
         raise constants.Error('Need a packet to send')
     packetlen = len(packet)
     if packetlen < 6 or packetlen > 1590:
         raise constants.Error('Invalid packet length')
     self.flush()
     self.write(packet)
Esempio n. 4
0
 def WritePacket(self, packet):
     if not packet:
         raise constants.Error('Need a packet to send')
     packetlen = len(packet)
     if packetlen < 6 or packetlen > 1590:
         raise constants.Error('Invalid packet length')
     self.flush()
     # Used encode to change string from unicode to bytes
     self.write(packet.encode())
Esempio n. 5
0
 def readpacket(self, timeout=None):
     total_read = 4
     initial_read = self.read(total_read)
     all_data = initial_read
     # Removed 'ord()' from if statement below to avoid conversion to str
     if initial_read[0] == 1:
         command = initial_read[3]
         data_number = struct.unpack('<H', initial_read[1:3])[0]
         if data_number > 6:
             toread = abs(data_number - 6)
             second_read = self.read(toread)
             # Added print() statement to visualize data in second_read
             all_data += second_read
             total_read += toread
             out = second_read
         else:
             out = ''
         suffix = self.read(2)
         sent_crc = struct.unpack('<H', suffix)[0]
         local_crc = crc16.crc16BINARY(all_data, 0, len(all_data))
         if sent_crc != local_crc:
             raise constants.CrcError("readpacket Failed CRC check")
         num1 = total_read + 2
         return ReadPacket(command, out)
     else:
         raise constants.Error('Error reading packet header!')
Esempio n. 6
0
 def readpacket(self, timeout=None):
     total_read = 4
     initial_read = self.read(total_read)
     all_data = initial_read
     if ord(initial_read[0]) == 1:
         command = initial_read[3]
         data_number = struct.unpack('<H', initial_read[1:3])[0]
         if data_number > 6:
             toread = abs(data_number - 6)
             second_read = self.read(toread)
             all_data += second_read
             total_read += toread
             out = second_read
         else:
             out = ''
         suffix = self.read(2)
         sent_crc = struct.unpack('<H', suffix)[0]
         local_crc = crc16.crc16(all_data, 0, total_read)
         if sent_crc != local_crc:
             raise constants.CrcError("readpacket Failed CRC check")
         num1 = total_read + 2
         return ReadPacket(command, out)
     else:
         raise constants.Error('Error reading packet header!')