Python Forum
Pyserial Readline() conversion to array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyserial Readline() conversion to array
#1
Dear all,

As the title already states I'd like to convert a line , read via the readline() function, I am already Googling all afternoon but cant find a solution.
This is the code I currently have , I would like to convert the variable response into an array of numbers (each byte translated into a number), 27 in total, well maybe without the 27th (<cr>).

This is the code I have :
    ser.open()
    #reset_input_buffer()
    #reset_output_buffer()  
    # Write query command
    logging.info("Writing query command")  
    ser.write(b'QS\r')
    logging.info("Read response")  
    #response = ser.read(27)        # read up to xx bytes 
    response = ser.readline()
    print( response.hex() )
    #print( response(0))

  
response.hex:
236d01206c206d02206c2000205ff920280212d020e7203c20090d
Reply
#2
This would give you an error because it is function call syntax, not array indexing syntax.
print( response(0))
Serial.readline() returns bytes. Bytes are indexable and iterable. If you index a "byte" from bytes, it is an int.
b = b'Hello World'  # Make bytes object
print("bytes", b[1], type(b[1]))
ba = bytearray(b)  # Make bytearray from bytes
print("bytearray", ba[1], type(ba[1]))
bl = list(b)   # Make list of ints from bytes
print("list", bl[1], type(bl[1]))
print("All equal is", b[1] == ba[1] == bl[1])  # Different types, but values are the same
Output:
bytes 101 <class 'int'> bytearray 101 <class 'int'> list 101 <class 'int'> All equal is True
You could not find how to turn bytes into an array of ints because it is already an array of ints.

If you need a mutable array of ints (bytes is immutable, like str) make a bytearray or a list.

The story changes quite a bit if the things you are reading from the serial port arn't bytes. Let's say the 26 bytes you read were a short int and three float doubles on the sending side of the serial link. The way you reconstruct this in Python is tu use the struct library.

https://docs.python.org/3/library/struct.html

This is a short example of packing and unpacking.
values = (1047, 1.23, 4.56, 7.89)
as_bytes = struct.pack("=hddd", *values)
print("Bytes", as_bytes)
as_values = struct.unpack("=hddd", as_bytes)
print("Values", as_values)
Output:
Bytes b'\x17\x04\xaeG\xe1z\x14\xae\xf3?=\n\xd7\xa3p=\x12@\x8f\xc2\xf5(\\\x8f\x1f@' Values (1047, 1.23, 4.56, 7.89)
The "=hddd" is the format string for data to pack/unpack. = is the byte order. After the byte-order are the format characters defining the data. "hddd" is 1 short (h) and three doubles (ddd).

Byte order is very important. You need to know the byte order of the data encode in the bytes. Choices are
@ Native
= Native
< Little Endian
> Big endian
! Network

If you have control of the sender and the receiver, use Network byte order. As long as everyone uses Network byte order the packing and unpacking always works.

I am running on a little endian machine. Look what happens if I set the byte order to Network order (Network order is big endian).
import struct
 
values = (1047, 1.23, 4.56, 7.89)
as_bytes = struct.pack("!hddd", *values)
print("Bytes", as_bytes)
as_values = struct.unpack("!hddd", as_bytes)
print("Values", as_values)
Bytes b'\x04\x17?\xf3\xae\x14z\xe1G\xae@\x12=p\xa3\xd7\n=@\x1f\x8f\\(\xf5\xc2\x8f'
Values (1047, 1.23, 4.56, 7.89)
The values come out just fine even though the bytes are completely different. The values work out if both sides agree. When both sides don't agree you get some crazy numbers.
import struct
 
values = (1047, 1.23, 4.56, 7.89)
as_bytes = struct.pack("<hddd", *values)
print("Bytes", as_bytes)
as_values = struct.unpack(">hddd", as_bytes)
print("Values", as_values)
Output:
Bytes b'\x17\x04\xaeG\xe1z\x14\xae\xf3?=\n\xd7\xa3p=\x12@\x8f\xc2\xf5(\\\x8f\x1f@' Values (5892, -9.60372140561512e-86, 1.19203925053298e-14, -9.539767610302094e-233)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Greek letters with .readline() and tkinter KinkgOfKeks 7 1,757 Mar-24-2023, 05:13 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,110 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,531 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  readline inside and outside functions paul18fr 2 2,050 May-20-2021, 01:15 PM
Last Post: csr
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 10,951 Jun-12-2020, 05:12 PM
Last Post: DreamingInsanity
  problem with readline() schlundreflex 6 4,431 Nov-06-2019, 02:22 PM
Last Post: schlundreflex
  readline() and readlines() rpaskudniak 9 30,125 Nov-21-2017, 07:39 PM
Last Post: metulburr
  Empty variable when using print before readline fstefanov 3 3,673 Oct-23-2017, 02:22 AM
Last Post: fstefanov
  pySerial .readline() help AlexSneedMiller 1 14,503 Jan-23-2017, 01:16 PM
Last Post: j.crater
  How can I use GNU readline when the value of sys.stdin has been changed? thePhysicist8 6 7,109 Dec-30-2016, 10:09 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020