Skip to main content
Arduino

Return to Question

Fixed syntax highlighting, capitalization, sentence structure.
Source Link
VE7JRO
  • 2.5k
  • 19
  • 27
  • 29

Unpacking arduinoArduino binary data on a python sketch

asAs 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 arduinoArduino and unpack them from the buffer into two separate csv files, one of the csvcvs 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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum

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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum

Unpacking arduino binary data on a python sketch

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 csv 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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum

Unpacking Arduino binary data on a python sketch

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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum
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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum
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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum
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)[0] << 8) | struct.unpack('B',malcolm)[0]
 #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[0] ..+ str(sensor_array[9]))
 #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)[10]<<8)|struct.unpack('B',malcolm)[10]
 sensor_array.append(lingo)
 if len(sensor_array) > 19:
 self.flick.write(str(sensor_array[10]..+ str(sensor_array[19]))
 
 The Packet data structure for the first and second sensor is:
 Packet1 from sensor 1:
 data_array[0] = headerbyte('#')
 data_array[1] = acceleration_xAxis
 data_array[2] = acceleration_yAxis
 data_array[3] = acceleration_zAxis
 data_array[4] = temperature
 data_array[5] = gyro_xAxis
 data_array[6] = gyro_yAxis
 data_array[7] = gyroz_xAxis
 data_array[8] = checksum
 Packet2 from sensor 2:
 data_array[9] = headerbyte('$')
 .
 .
 .
 data_array[17] = checksum
replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

as a follow up to my earlier post Simultaneously reading data from multiple serial ports 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 csv 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

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 csv 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

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 csv 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

added an example of the data structure
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading
Post Undeleted by dada
Post Deleted by dada
adjusted the code format, added a screenshot of the data packet and added some comments
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading
adjusted the code format and added some comments
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading
adjusted the code format and added some comments
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading
adjusted the code format and added some comments
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading
adjusted code formats
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading
Source Link
dada
  • 127
  • 3
  • 6
  • 17
Loading

AltStyle によって変換されたページ (->オリジナル) /