class moteProbe(threading.Thread): def __init__(self, serialport=None): # store params self.serialport = serialport # local variables self.framelength = 0 self.busyReceiving = False self.inputBuf = '' self.dataLock = threading.Lock() self.rxByte = 0 self.prevByte = 0 self.prev_packet_time = 0 self.latency = [ 0.0, 0.0 ] #Here first element represents prev_latency, and second element represents sample count used to calculate the average latency self.prev_pkt = None self.rframe_latency = 0 # flag to permit exit from read loop self.goOn = True self.parser_data = ParserData() self.routing_instance = Routing() # initialize the parent class threading.Thread.__init__(self) # give this thread a name self.name = 'moteProbe@' + self.serialport try: self.serial = serial.Serial(self.serialport, '115200') except Exception as err: print err # start myself self.start() print "serial thread: " + self.name + " started successfully" #======================== thread ========================================== def run(self): while self.goOn: # read bytes from serial port try: self.rxByte = self.serial.read(1) if not self.rxByte: continue #print binascii.hexlify(self.rxByte) if (int(binascii.hexlify(self.rxByte), 16) == 0x7e) and not self.busyReceiving: self.busyReceiving = True self.prevByte = self.rxByte continue except Exception as err: print err break else: if self.busyReceiving and (int(binascii.hexlify(self.prevByte), 16) == 0x7e): #Converting string to integer to make comparison easier self.framelength = int(binascii.hexlify(self.rxByte), 16) self.inputBuf += self.rxByte self.prevByte = self.rxByte continue else: self.inputBuf += self.rxByte if len(self.inputBuf) >= self.framelength: self.busyReceiving = False self._process_inputbuf() def _process_inputbuf(self): if self.inputBuf[1].upper() == 'P': print "received packet: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf) data = [int(binascii.hexlify(x), 16) for x in self.inputBuf] data_tuple = self.parser_data.parseInput(data[2:]) (result, data) = self.routing_instance.meshToLbr_notify(data_tuple) #Added by Nico for error handling if data == None and not result: self.inputBuf = '' print "No correct packet!" return if not result: if self.prev_pkt == data[4:]: print "Duplicate packet" self.inputBuf = '' return curr_packet_time = int(round(time.time() * 1000)) print "received data: " + ':'.join( str(hex(i)) for i in data[4:]) + " , Packet Latency: " + str( curr_packet_time - self.prev_packet_time) print "received data len : " + str(len(data[4:])) x = curr_packet_time - self.prev_packet_time self.latency[1] = self.latency[1] + 1.0 if self.latency[1] > 1.0: x = curr_packet_time - self.prev_packet_time self.running_mean(x) print "average latency: " + str(self.latency[0]) self.prev_packet_time = curr_packet_time self.prev_pkt = data[4:] elif self.inputBuf[1] == 'D': print "debug msg: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) measured_data.append(int(binascii.hexlify(self.inputBuf[2]), 16)) #if(len(measured_data[str(payload_length)]) == 50): #if(len(measured_data) == 50): #print(json.dumps(measured_data)) #f = open('measurement_radio_rcv_to_nxt_slot_127.json','w') #f.write(json.dumps(measured_data)) #f.close() #payload_length = -1 #self.close() elif self.inputBuf[1] == 'A': print "output message: " + ":".join("{:02x}".format(ord(c) - 48) for c in self.inputBuf[2:]) # added by Ayca to print out direct numbers / works differently with letters elif self.inputBuf[1] == 'R': print "command response: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) response1 = "".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) #added by Nico to make the output human readable hilf = response1.split(":") responseArrayCheck = "".join(hilf) # Added by Nico to make the responses human-readable if responseArrayCheck[2:4] == "04": if len(responseArrayCheck) == 18: print "" + responseArrayCheck[4:6] + " TX , " + responseArrayCheck[ 6:8] + " RX , " + responseArrayCheck[ 8:10] + " TXRX (Beacon) , " + responseArrayCheck[ 10:12] + " SRX , " + responseArrayCheck[ 12: 14] + " empty slots " + responseArrayCheck[ 14: 16] + " MACRes slots " + responseArrayCheck[ 16:18] + " MACInit slots " elif len(responseArrayCheck) == 8: print "Channel: " + responseArrayCheck[ 4:6] + " Sent packets: " + responseArrayCheck[6:8] else: print " Slottyp: " + responseArrayCheck[ 4:6] + " ChannelOffset: " + responseArrayCheck[ 6:8] + " NumRX: " + responseArrayCheck[ 8:10] + " NumTX: " + responseArrayCheck[ 10:12] + " NumTxAck: " + responseArrayCheck[ 12: 14] + " SlotOffset: " + responseArrayCheck[ 14:16] elif responseArrayCheck[2:4] == "0b": if responseArrayCheck[4:6] == "01": print "Measurement is now disabled!" elif responseArrayCheck[4:6] == "02": print "Measurement is now enabled!" elif responseArrayCheck[2:4] == "09": #Now Saving the result in a file global seperation if seperation == 0: file = open("measurementNicoChannel.txt", "w") file.write(responseArrayCheck[4:]) file.close seperation = 1 elif seperation == 1: file = open("measurementNicoTimes.txt", "w") file.write(responseArrayCheck[4:]) file.close seperation = 2 elif seperation == 2: file = open("measurementNicoOffsets.txt", "w") file.write(responseArrayCheck[4:]) file.close seperation = 3 elif seperation == 3: print "Times sent in INIT or RESOLUTION slots: ", responseArrayCheck[ 4: 6], "Times sent in RESOLUTION slots: ", responseArrayCheck[ 6:8] seperation = 0 elif responseArrayCheck[2:4] == "45": file = open("measurementNicoEnhancedDAGroot.txt", "a") file.write(responseArrayCheck[4:]) file.write("\n") file.close elif self.inputBuf[1] == 'E': print "------------------------------------------------------------------------" print "error msg: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) print "------------------------------------------------------------------------" elif self.inputBuf[1] == 'S': #Sending commands to mote #Here I am using global variables #curr_packet_time = int(round(time.time() * 1000)) #print "request frame: " + str(curr_packet_time-self.rframe_latency) #self.rframe_latency = curr_packet_time global outputBuf global outputBufLock #global latency if (len(outputBuf) > 0) and not outputBufLock: outputBufLock = True dataToWrite = outputBuf.pop(0) outputBufLock = False #print int(round(time.time() * 1000)) - latency self.serial.write(dataToWrite) #print len(dataToWrite) print "injecting: " + ":".join("{:02x}".format(ord(c)) for c in dataToWrite) self.inputBuf = '' def _process_packet(self, packet): print "process_packet_func" def _fill_outputbuf(self): #When user wants to send some data, data will be pushed to this buffer print __name__ def close(self): self.goOn = False def prepare_UDP_packet(self, payload): print "prepare_UDP_packet" #Running mean implementation by storing only one element, and sample count def running_mean(self, x): #I have used 300 to make sure that first outlier is rejected, while calculating the average tmp = self.latency[0] * max(self.latency[1] - 1, 1) + x self.latency[0] = tmp / self.latency[1]
class moteProbe(threading.Thread): def __init__(self, serialport=None): # store params self.serialport = serialport # local variables self.framelength = 0 self.busyReceiving = False self.inputBuf = '' self.dataLock = threading.Lock() self.rxByte = 0 self.prevByte = 0 # flag to permit exit from read loop self.goOn = True self.parser_data = ParserData() self.routing_instance = Routing() # initialize the parent class threading.Thread.__init__(self) # give this thread a name self.name = 'moteProbe@' + self.serialport try: self.serial = serial.Serial(self.serialport, '115200', timeout=1) except Exception as err: print err # start myself self.start() print "serial thread: " + self.name + " started successfully" #======================== thread ========================================== def run(self): while self.goOn: # read bytes from serial port #Sending commands to mote #Here I am using global variables global outputBuf global outputBufLock if (len(outputBuf) > 0) and not outputBufLock: self.serial.write(outputBuf) #print ':'.join('{:02x}'.format(x) for x in outputBuf) outputBuf = '' try: self.rxByte = self.serial.read(1) if not self.rxByte: continue elif (int(binascii.hexlify(self.rxByte), 16) == 0x7e) and not self.busyReceiving: self.busyReceiving = True self.prevByte = self.rxByte continue except Exception as err: print err time.sleep(1) break else: if self.busyReceiving and (int(binascii.hexlify(self.prevByte), 16) == 0x7e): #Converting string to integer to make comparison easier self.framelength = int(binascii.hexlify(self.rxByte), 16) self.inputBuf += self.rxByte self.prevByte = self.rxByte continue else: self.inputBuf += self.rxByte if len(self.inputBuf) == self.framelength: self.busyReceiving = False self._process_inputbuf() def _process_inputbuf(self): if self.inputBuf[1].upper() == 'P': print "This is a packet" print ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) data = [int(binascii.hexlify(x), 16) for x in self.inputBuf] data_tuple = self.parser_data.parseInput(data[2:]) global isDAOReceived isDAOReceived = self.routing_instance.meshToLbr_notify(data_tuple) #print packet_ipv6['next_header'] elif self.inputBuf[1] == 'D': print "This is debug message" print ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) elif self.inputBuf[1] == 'R': print "This is a command response" print ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) self.inputBuf = '' def _process_packet(self, packet): print "process_packet_func" def _fill_outputbuf(self): #When user wants to send some data, data will be pushed to this buffer print __name__ def close(self): self.goOn = False
class moteProbe(threading.Thread): def __init__(self, serialport=None): # store params self.serialport = serialport # local variables self.framelength = 0 self.busyReceiving = False self.inputBuf = '' self.dataLock = threading.Lock() self.rxByte = 0 self.prevByte = 0 self.prev_packet_time = 0 self.latency = [ 0.0, 0.0 ] #Here first element represents prev_latency, and second element represents sample count used to calculate the average latency self.prev_pkt = None self.rframe_latency = 0 # flag to permit exit from read loop self.goOn = True self.parser_data = ParserData() self.routing_instance = Routing() # initialize the parent class threading.Thread.__init__(self) # give this thread a name self.name = 'moteProbe@' + self.serialport try: self.serial = serial.Serial(self.serialport, '115200') except Exception as err: print err # start myself self.start() print "serial thread: " + self.name + " started successfully" #======================== thread ========================================== def run(self): while self.goOn: # read bytes from serial port try: self.rxByte = self.serial.read(1) if not self.rxByte: continue #print binascii.hexlify(self.rxByte) if (int(binascii.hexlify(self.rxByte), 16) == 0x7e) and not self.busyReceiving: self.busyReceiving = True self.prevByte = self.rxByte continue except Exception as err: print err break else: if self.busyReceiving and (int(binascii.hexlify(self.prevByte), 16) == 0x7e): #Converting string to integer to make comparison easier self.framelength = int(binascii.hexlify(self.rxByte), 16) self.inputBuf += self.rxByte self.prevByte = self.rxByte continue else: self.inputBuf += self.rxByte if len(self.inputBuf) >= self.framelength: self.busyReceiving = False self._process_inputbuf() def _process_inputbuf(self): if self.inputBuf[1].upper() == 'P': print "received packet: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf) data = [int(binascii.hexlify(x), 16) for x in self.inputBuf] data_tuple = self.parser_data.parseInput(data[2:]) (result, data) = self.routing_instance.meshToLbr_notify(data_tuple) if not result: if self.prev_pkt == data[4:]: print "Duplicate packet" self.inputBuf = '' return curr_packet_time = int(round(time.time() * 1000)) print "received data: " + ':'.join( str(hex(i)) for i in data[4:]) + " , Packet Latency: " + str( curr_packet_time - self.prev_packet_time) print "received data len : " + str(len(data[4:])) x = curr_packet_time - self.prev_packet_time self.latency[1] = self.latency[1] + 1.0 if self.latency[1] > 1.0: x = curr_packet_time - self.prev_packet_time self.running_mean(x) print "average latency: " + str(self.latency[0]) self.prev_packet_time = curr_packet_time self.prev_pkt = data[4:] elif self.inputBuf[1] == 'D': print "debug msg: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) measured_data.append(int(binascii.hexlify(self.inputBuf[2]), 16)) #if(len(measured_data[str(payload_length)]) == 50): if (len(measured_data) == 50): print(json.dumps(measured_data)) f = open('measurement_radio_rcv_to_nxt_slot_127.json', 'w') f.write(json.dumps(measured_data)) f.close() payload_length = -1 self.close() elif self.inputBuf[1] == 'R': print "command response: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) elif self.inputBuf[1] == 'E': print "------------------------------------------------------------------------" print "error msg: " + ":".join("{:02x}".format(ord(c)) for c in self.inputBuf[2:]) print "------------------------------------------------------------------------" elif self.inputBuf[1] == 'S': #Sending commands to mote #Here I am using global variables #curr_packet_time = int(round(time.time() * 1000)) #print "request frame: " + str(curr_packet_time-self.rframe_latency) #self.rframe_latency = curr_packet_time global outputBuf global outputBufLock #global latency if (len(outputBuf) > 0) and not outputBufLock: outputBufLock = True dataToWrite = outputBuf.pop(0) outputBufLock = False #print int(round(time.time() * 1000)) - latency self.serial.write(dataToWrite) #print len(dataToWrite) print "injecting: " + ":".join("{:02x}".format(ord(c)) for c in dataToWrite) self.inputBuf = '' def _process_packet(self, packet): print "process_packet_func" def _fill_outputbuf(self): #When user wants to send some data, data will be pushed to this buffer print __name__ def close(self): self.goOn = False def prepare_UDP_packet(self, payload): print "prepare_UDP_packet" #Running mean implementation by storing only one element, and sample count def running_mean(self, x): #I have used 300 to make sure that first outlier is rejected, while calculating the average tmp = self.latency[0] * max(self.latency[1] - 1, 1) + x self.latency[0] = tmp / self.latency[1]