import sys import serial import time import rss import numpy as np # Open port to listen node serial_filename = rss.serialFileName() sys.stderr.write('Using USB port file: ' + serial_filename + '\n') ser = serial.Serial(serial_filename, 38400) # Set number of values for txs and channels tx = 10 channels = 8 # Variables to be used later on current_tx = 0 current_ch = 0 pseudo_current_tx = 0 pseudo_current_ch = 0 last_tx = 0 last_ch = 0 print_tx = 0 print_ch = 0 # Create matrices to hole the values of the counter, channels, tx ids, RSS values, and Timestamps channel_list = [26, 18, 24, 16, 22, 14, 20, 12] rss_mat = np.zeros([tx, tx]) timestamp = 0 timestamp_skipped = 0 # Set boolean for setup of program for the start of data collection first_line = True # Exports hexadecimal numbers from raw input of listener node to run through other data packet design rawFileName = "rawdata.txt" # Added 5 April 2019 by Neal fout = open(rawFileName, "w") # Added 5 April 2019 by Neal currentLine = [] while 1: # Read in from the listener node tempInt = ser.read().encode('hex') currentLine.append(tempInt) # If at end of line, if currentLine[-2:] == ['ef', 'be']: # Write the line to a file # Added 5 April 2019 by Neal fout.write(str(currentLine)) # Added 5 April 2019 by Neal # For the first line put into the algorithm if first_line: last_tx = (rss.hex2signedint(currentLine[2]) - 2) % tx first_line = False # Record current time and current tx and ch numbers current_tx = rss.hex2signedint(currentLine[2]) - 1 current_ch = rss.hex2signedint(currentLine[tx + 3]) last_ch = channel_list[(channel_list.index(current_ch)-1) % channels] timestamp = time.time() fout.write(", {0:f}\n".format(timestamp)) # Determine if packets were skipped if (current_tx - last_tx) != 1 and (current_tx - last_tx) != -9: # For skipped packets that occurred over the span of a single channel if (current_tx - last_tx) > 1: while (current_tx - last_tx) > 1: pseudo_current_tx = last_tx + 1 for j in range(0, tx): rss_mat[pseudo_current_tx, j] = 127 # Print out the tx corresponding the pseudo tx + 1 print_tx = pseudo_current_tx + 1 print_ch = last_ch # Make a string of RSS values for the node we want to print out datastring = "" for j in range(0, 9): datastring = datastring + str(int(rss_mat[j, print_tx])) + " " datastring = datastring + str(int(rss_mat[9, print_tx])) # Print out all relevant information in the order we desire # Only works for Python 2.7 print "{0}, {1}, {2}, {3:f}".format(str(print_ch), str(print_tx+1), datastring, timestamp) last_tx += 1 # For skipped packets that occurred over the span of two channels if (current_tx - last_tx) < 1: # For the values of the packets on the first of the two channels while((current_tx - last_tx) < 0): # Once at the end of a set of links, set back to 0 if (last_tx == 9): pseudo_current_tx = 0 else: pseudo_current_tx = last_tx + 1 for j in range(0, tx): rss_mat[pseudo_current_tx, j] = 127 if pseudo_current_tx == 9: print_tx = 0 print_ch = last_ch else: print_tx = pseudo_current_tx + 1 # Ensure that correct channel is being printed along with values if pseudo_current_tx == 0: print_ch = last_ch else: print_ch = channel_list[(channel_list.index(last_ch)-1) % channels] # Make a string of RSS values for the node we want to print out datastring = "" for j in range(0, 9): datastring = datastring + str(int(rss_mat[j, print_tx])) + " " datastring = datastring + str(int(rss_mat[9, print_tx])) # Print out all relevant information in the order we desire # Only works for Python 2.7 print "{0}, {1}, {2}, {3:f}".format(str(print_ch), str(print_tx+1), datastring, timestamp) if (last_tx == 9): last_tx = 0 else: last_tx += 1 # For the values of the packets on the most current channel while (current_tx - last_tx) > 1: pseudo_current_tx = last_tx + 1 for j in range(0, tx): rss_mat[pseudo_current_tx, j] = 127 # Print out the tx corresponding the pseudo tx + 1 print_tx = pseudo_current_tx + 1 print_ch = last_ch # Make a string of RSS values for the node we want to print out datastring = "" for j in range(0, 9): datastring = datastring + str(int(rss_mat[j, print_tx])) + " " datastring = datastring + str(int(rss_mat[9, print_tx])) # Print out all relevant information in the order we desire # Only works for Python 2.7 print "{0}, {1}, {2}, {3:f}".format(str(print_ch), str(print_tx+1), datastring, timestamp) last_tx += 1 # Iterate through RSS known locations and input into rolling matrix to be transposed for i in range(3, tx + 3): rss_mat[current_tx, i - 3] = rss.hex2signedint(currentLine[i]) # Always print values and skip lines with bad data in algorithm # After just filling the values for the n tx, we can finally print out the n+1 tx's received values if current_tx == 9: print_tx = 0 print_ch = current_ch else: print_tx = current_tx + 1 print_ch = last_ch # Make a string of RSS values for the node we want to print out datastring = "" for j in range(0, 9): datastring = datastring + str(int(rss_mat[j, print_tx])) + " " datastring = datastring + str(int(rss_mat[9, print_tx])) # Print out all relevant information in the order we desire # Only works for Python 2.7 print "{0}, {1}, {2}, {3:f}".format(str(print_ch), str(print_tx+1), datastring, timestamp) # Set the last_counter to the current counter value for the next loop and blank out the input read-in line last_tx = current_tx currentLine = []