PhippsJ
Posts: 4
Joined: Thu Jan 18, 2018 10:46 pm

Extracting bytes from a byte list

Thu Jan 18, 2018 11:30 pm

I have the following code which is reading a usb port for a block of data.

import serial

# Open Serial Port is OK
ser=serial.Serial("/dev/ttyUSB0", 19200)

# assign LIst
tedData = []

# Request data from remote unit
stat = ser.write(b'\xAA')

tedData.append(ser.read(282))
# read in the 282 byte length data packet

# get length of list
dataLen = int(len(tedData))

# Prints length of 282 each time
print(dataLen)

# prints b'\x10\x01\x00 --- etc for 282 bytes.
print(tedData[0])

# List index out of range error.
print(tedData[10]}

I need to be able to select certain groups of bytes in the list and convert to integers, floats or ?
I am having trouble indexing tedData to extract a particular byte or bytes.
Running Raspian PI 2 with Python 3.
Any help would be appreceated
Jerry

User avatar
OutoftheBOTS
Posts: 659
Joined: Tue Aug 01, 2017 10:06 am

Re: Extracting bytes from a byte list

Fri Jan 19, 2018 6:33 am

using the strut library is the easiest way see https://docs.python.org/2/library/struct.html

I just typed this out without testing but it should work

Code: Select all

import serial
import struct

# Open Serial Port is OK
ser=serial.Serial("/dev/ttyUSB0", 19200)

# Request data from remote unit
stat = ser.write(b'\xAA')

# read in the 282 byte length data packet
tedData = bytearray(ser.read(282), 'utf-8') 

# get length of list
dataLen = int(len(tedData))

# Prints length of 282 each time
print(dataLen)

#unpack the first 4 bytes as a signed interger in little indian format
unpacked_int = struct.unpack"<i", tedData[0:5])

print (unpacked_int)

PhippsJ
Posts: 4
Joined: Thu Jan 18, 2018 10:46 pm

Re: Extracting bytes from a byte list

Fri Jan 19, 2018 8:33 pm

I tried the struct.unpack command to extract the integer from the data block but get this error
TypeError: a bytes-like object is required , not 'list'

Look's like the list data is considered string instead of bytes. although when I print the block out "print(tedData) is print out as hex bytes \x00 etc.
Any other suggestions anybody? (working with Python3)

Code: Select all

#!/usr/bin/env python3

import serial
import struct

ser = serial.Serial("/dev/ttyUSB0", 19200)
tedData = []

# requirest packet
ser.write(b'\AA')    

# read in block
tedData.append(ser.read(282))         

#unpack 2 bytes to short integer
kw = struct.unpack('<s', tedData[249:250]) 
print("KWH Today =  " + kw)                           


User avatar
Paeryn
Posts: 2141
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Extracting bytes from a byte list

Sat Jan 20, 2018 1:16 am

PhippsJ wrote:
Fri Jan 19, 2018 8:33 pm
I tried the struct.unpack command to extract the integer from the data block but get this error
TypeError: a bytes-like object is required , not 'list'

Look's like the list data is considered string instead of bytes. although when I print the block out "print(tedData) is print out as hex bytes \x00 etc.
Any other suggestions anybody? (working with Python3)

Code: Select all

#!/usr/bin/env python3

import serial
import struct

ser = serial.Serial("/dev/ttyUSB0", 19200)
tedData = []

# requirest packet
ser.write(b'\AA')    

# read in block
tedData.append(ser.read(282))         

#unpack 2 bytes to short integer
kw = struct.unpack('<s', tedData[249:250]) 
print("KWH Today =  " + kw)                           

You created tedData as an empty list and appended a bytearray to it so tedData would now be a list with its first (and only) element containing a bytearray of 282 bytes, you get the error because you provided a list (and an empty one at that).

Also your unpack format string is wrong, s is a string of length 1 character not a two byte integer, that would be h. And slices are from the start index up to but not including the end index, tedData[249:250] is one item not two.

Oh, and struct.unpack() always returns a tuple even if there is only one item.

Code: Select all

#!/usr/bin/env python3

import serial
import struct

ser = serial.Serial("/dev/ttyUSB0", 19200)

# requirest packet
ser.write(b'\AA')    

# read in block
tedData = ser.read(282)

#unpack 2 bytes to short integer
kw = struct.unpack('<h', tedData[249:251])[0]
print("KWH Today =  " + kw)
She who travels light — forgot something.

PhippsJ
Posts: 4
Joined: Thu Jan 18, 2018 10:46 pm

Re: Extracting bytes from a byte list

Sat Jan 20, 2018 2:42 am

Thanks
Everything working now.
Jerry

Return to “Python”

Who is online

Users browsing this forum: paddyg and 31 guests