As a follow up to my earlier post: Simultaneously reading data from multiple serial ports, I am stuck trying to develop a python sketch that can read the incoming serial data from the com port on my Arduino and unpack them from the buffer into two separate cvs files.
One of the csv files would contain the data from the first sensor, while the other would contain the data from the second sensor. I have been able to develop the sketch to a point where on receiving the first header byte the first sensor data is unpacked. I am finding it hard to adjust the sketch to allow for the second byte header to be called to unpack the second sensor data from the data packet. Below is my abbreviated sketch thus far. Thanks all.
def run(self): #Creating a csv file to read data from the first sensor self.click = open("ball_Data.csv", "wb") #Creating a csv file to read data from the second sensor self.flick = open("rugby_Data.csv", "wb") #creating the serial port object self.push = serial.Serial('COM6', 9600) while True: #If the incoming byte is equivalent to '$' if node == b'$': # the byte object is equivalent to the available bytes node = self.push.read() #an array to hold the sensor data sensor_array =  i = 0 #This while loop initiates a count from 0 to 9 #through the sensor array while i < 10: #The available byte present malcolm = self.push.read() #The next byte turnbull = self.push.read() #The incoming uint_16 data is unpacked into the variable lingo lingo= (struct.unpack('b', turnbull) << 8) | struct.unpack('B',malcolm) #The variable lingo is then appended to the sensor array sensor_array.append(lingo) #If the length of the sensor array is equivalent to 9 #write the data through each count into the sensor array if len(sensor_array) == 9: self.click.write(str(sensor_array ..+ str(sensor_array)) #My idea is that this else statement was supposed to trigger #the next header for the second sensor which would be an '#' #sign before unpacking the data else: oldde = self.push.read() sensor_array =  i = 10 while i < 20: #The available byte present malcolm = self.push.read() #The next byte turnbull = self.push.read() lingo= (struct.unpack('b', turnbull)<<8)|struct.unpack('B',malcolm) sensor_array.append(lingo) if len(sensor_array) > 19: self.flick.write(str(sensor_array..+ str(sensor_array)) The Packet data structure for the first and second sensor is: Packet1 from sensor 1: data_array = headerbyte('#') data_array = acceleration_xAxis data_array = acceleration_yAxis data_array = acceleration_zAxis data_array = temperature data_array = gyro_xAxis data_array = gyro_yAxis data_array = gyroz_xAxis data_array = checksum Packet2 from sensor 2: data_array = headerbyte('$') . . . data_array = checksum