Beispiel #1
0
    def read_packet(self, Protocol, timeout = 10):
        packet = ''
        # self._bufer = ''
        stop_time = time.time() + timeout
        while time.time() < stop_time:
            data = self.get_row_data()
            if data is None:
                # problem with fake packet end:
                # 19 26 00 03 01 0A 11 A4 00 99 00 00 00 04 09 AA 07 02 80 4D 02 03 A1 0A D8 0A
                time.sleep(0.09)
                continue
            #Reading 1st byte
            packet = packet + data
            # if (len(packet) >= Protocol.MIN_PACKET_LEN) and (packet.rfind(Protocol.EOM) == len(packet) - len(Protocol.EOM)):
                #Found EOM = 0x0A and packets length is more than packet_type.MIN_PACKET_LEN
                #Checking if packet is correct
            for char_index in range(len(packet) - Protocol.MIN_PACKET_LEN):
                offset = self._check_packet(packet[char_index:], Protocol.NAME)
                if type(offset) is not bool:
                    self._bufer = packet[offset:]
                    packet = packet[char_index:char_index + offset]
                    # log.write('bufer=%s' % hexdump(self._bufer))
                    log.write('={0}= [RX]: {1}'.format(self.COMPort, hexdump(packet)), "debug")

                    return packet

                #Packet is incorrect. No byte found that corresponds to packet length
                #Continue collecting data
        #print '(def read_packet) Output due to a timeout {0} sec.'.format( timeout )
        return None
Beispiel #2
0
    def read_packet(self, Protocol, timeout=10):
        packet = ''
        # self._bufer = ''
        stop_time = time.time() + timeout
        while time.time() < stop_time:
            data = self.get_row_data()
            if data is None:
                # problem with fake packet end:
                # 19 26 00 03 01 0A 11 A4 00 99 00 00 00 04 09 AA 07 02 80 4D 02 03 A1 0A D8 0A
                time.sleep(0.09)
                continue
            #Reading 1st byte
            packet = packet + data
            # if (len(packet) >= Protocol.MIN_PACKET_LEN) and (packet.rfind(Protocol.EOM) == len(packet) - len(Protocol.EOM)):
            #Found EOM = 0x0A and packets length is more than packet_type.MIN_PACKET_LEN
            #Checking if packet is correct
            for char_index in range(len(packet) - Protocol.MIN_PACKET_LEN):
                offset = self._check_packet(packet[char_index:], Protocol.NAME)
                if type(offset) is not bool:
                    self._bufer = packet[offset:]
                    packet = packet[char_index:char_index + offset]
                    # log.write('bufer=%s' % hexdump(self._bufer))
                    log.write(
                        '={0}= [RX]: {1}'.format(self.COMPort,
                                                 hexdump(packet)), "debug")

                    return packet

                #Packet is incorrect. No byte found that corresponds to packet length
                #Continue collecting data
        #print '(def read_packet) Output due to a timeout {0} sec.'.format( timeout )
        return None
Beispiel #3
0
    def send_packet(self, packet):
        self.ser.read(self.ser.inWaiting())

        log.write('={0}= [TX]: {1}'.format(self.COMPort, hexdump(packet)), "debug")
        self.ser.write(packet)
Beispiel #4
0
 def run(self):
     packet_found = False
     packet = ''
     offset = 0
     while(not self._stop_listening):
         if(self._port.inWaiting() > 0):
             self._is_runned = True
             data_size = self._port.inWaiting()
             add = self._port.read(data_size)
             self._bufer += add
             # self.log("raw {0}[RX] {1}".format(self._port.port, hexdump(add)), "debug")
             if self._repeater_port:
                 self._repeater_port.write(add)
  
         else:
             if self._repeater_port and (self._repeater_port.inWaiting() > 0):
                 data = self._repeater_port.read(self._repeater_port.inWaiting())
                 self._port.write(data)
                 
             if len(self._pool_out) > 0:
                 msg = self._pool_out.pop(0)
                 self.log("{0}[TX] {1}".format(self._port.port, hexdump(msg)), "debug")
                 self._port.write(msg)
             else:
                 time.sleep(0.01)
             continue
         offset = 0
         # self.log('bufer = %s for %s' % (hexdump(self._bufer), self._port.port))
         while offset < len(self._bufer):
             # seek to STM
             # STM = 0 means any byte
             # print "fist byte=%02x" % ord(self._bufer[offset])
             while (self._STM != 0) and (self._bufer[offset] != self._STM):
                 offset += 1
                 # print "seeking for STM: %d" % offset
                 if offset == len(self._bufer):
                     break
             else:
                 #STM found
                 if(self._bufer.find(self._EOM) != -1):
                     # print "EOM found " 
                     #print "buf[%d]: %s" % (len(self._bufer),hexdump(self._bufer))
                     for handler in self._handlers:
                         #self.log("protoparser::handler_run %s (%s)" % (handler, self._port.port))
                         #print "handlers:%d for %s" % (len(self._handlers), self)
                         packet = handler(self._bufer[offset:])
                         if (packet != None):
                             self._pool.append(packet)
                             self.log("{0}[RX] {1}".format(self._port.port, hexdump(packet)), "debug")
                             #crop bufer
                             self._bufer = self._bufer[len(packet) + offset:]
                             # self.log("croped bufer = %s" % hexdump(self._bufer), "debug")
                             # offset = 0
                             break
                     else:
                         offset += 1
                 else:
                     break
         if self._bufer and self._show_unparsed: pass
             # self.log("UNP[%d](%s): %s" % (threading.current_thread().ident,self._port.port, hexdump(self._bufer)))
         #self._bufer = ''
     self._is_runned = False
Beispiel #5
0
    def run(self):
        packet_found = False
        packet = ''
        offset = 0
        while (not self._stop_listening):
            if self._pause:
                time.sleep(0.2)
                continue
            if (self._port.inWaiting() > 0):
                self._is_runned = True
                data_size = self._port.inWaiting()
                add = self._port.read(data_size)
                self._bufer += add
                # self.log("raw {0}[RX] {1}".format(self._port.port, hexdump(add)), "debug")
                if self._repeater_port:
                    self._repeater_port.write(add)

            else:
                if self._repeater_port and (self._repeater_port.inWaiting() >
                                            0):
                    data = self._repeater_port.read(
                        self._repeater_port.inWaiting())
                    self._port.write(data)

                if len(self._pool_out) > 0:
                    msg = self._pool_out.pop(0)
                    self.log(
                        "{0}[TX] {1}".format(self._port.port, hexdump(msg)),
                        "debug")
                    self._port.write(msg)
                else:
                    time.sleep(0.01)
                continue
            offset = 0
            # self.log('bufer = %s for %s' % (hexdump(self._bufer), self._port.port))
            while offset < len(self._bufer):
                # seek to STM
                # STM = 0 means any byte
                # print "fist byte=%02x" % ord(self._bufer[offset])
                while (self._STM != 0) and (self._bufer[offset] != self._STM):
                    offset += 1
                    # print "seeking for STM: %d" % offset
                    if offset == len(self._bufer):
                        break
                else:
                    #STM found
                    if (self._bufer.find(self._EOM) != -1):
                        # print "EOM found "
                        #print "buf[%d]: %s" % (len(self._bufer),hexdump(self._bufer))
                        for handler in self._handlers:
                            #self.log("protoparser::handler_run %s (%s)" % (handler, self._port.port))
                            #print "handlers:%d for %s" % (len(self._handlers), self)
                            packet = handler(self._bufer[offset:])
                            if (packet != None):
                                self._pool.append(packet)
                                self.log(
                                    "{0}[RX] {1}".format(
                                        self._port.port, hexdump(packet)),
                                    "debug")
                                #crop bufer
                                self._bufer = self._bufer[len(packet) +
                                                          offset:]
                                # self.log("croped bufer = %s" % hexdump(self._bufer), "debug")
                                # offset = 0
                                break
                        else:
                            offset += 1
                    else:
                        break
            if self._bufer and self._show_unparsed: pass
            # self.log("UNP[%d](%s): %s" % (threading.current_thread().ident,self._port.port, hexdump(self._bufer)))
            #self._bufer = ''
        self._is_runned = False
Beispiel #6
0
    def send_packet(self, packet):
        self.ser.read(self.ser.inWaiting())

        log.write('={0}= [TX]: {1}'.format(self.COMPort, hexdump(packet)),
                  "debug")
        self.ser.write(packet)