Getting values from bytestrings

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Daft_vagabond
 
Posts: 92
Joined: Thu May 28, 2020 2:53 pm

Getting values from bytestrings

Post by Daft_vagabond »

I have this generic prop-board with a motion sensor. I'm communicating with it via uart. I'm trying to nail-down the sensor's feedback so I can map things to it.
I've really been trying to figure this out on my own, reading into the re and struct modules, but I can't seem to figure out how to format the data the way I want.

It's been through a few iterations, but heres the code I currently have:

Code: Select all

prop_board_in = uart.readline() 
    
if prop_board_in is not None:
   # convert bytearray to string
   prop_board_in_string = ''.join([chr(b) for b in prop_board_in ])
   print(prop_board_in_string, end="")
And the output of that looks like this:

Code: Select all

motion "2920 -118 7526 -29 -13 -3"

motion "2923 -105 7552 -28 -15 -2"

motion "2925 -106 7548 -27 -9 -1"

motion "2915 -95 7528 -27 -11 2"
or like this, if I just read it as a bytearray:

Code: Select all

b'motion "2905 -149 7525 -29 -11 3"\n'
Ideally, when that comes in, I'd like to discard the "motion" and quotes, then take the six values and save and/or print them as:

Code: Select all

x1 = 2905  
x2 = -149 
y1 = 7525  
y2 = 29  
z1 = -11 
z1 = 3
Or something like that. I just want each incoming set of those six values to individually assigned to a variable whenever I ask the board to update me on the values, so not a constant update, just whenever I press a button or something.

I feel like I could do something with .split(), or maybe there's some format module that could take out the quotes and spaces. I imagine I'd have to populate an array, then iterate through it, adding each value to a different variable. I'd probably want to convert the values it int, too.

Either way, I'd definitely appreciate advice with this one.

User avatar
Daft_vagabond
 
Posts: 92
Joined: Thu May 28, 2020 2:53 pm

Re: Getting values from bytestrings

Post by Daft_vagabond »

I wanted to give an update. I figured out how to do it, and it seems to work consistently.

Code: Select all

for i in range (5):
        # with how the data comes in, this makes it so I only read exactly one line and that it's a full, complete line
        # When I used readline, I would often get line fragments
        board_in = uart.readline()
    
    if board_in is not None:
        
        board_in_string = ''.join([chr(b) for b in board_in ]).strip('motion').replace('"',"")
        # converts the bytearray to a string, removes the word "motion" and the quotes on the ends
        board_in_list = list(map(int,board_in_string .split()))
        # Takes the new string of just numbers and spaces, splits them into a list at the spaces, maps them to integers in a list
        print(board_in_list )
        # so I can see and compare my result to the original list,
        x1, x2, y1, y2, z1, z2 = [board_in_list [i] for i in range(len(board_in_list ))]
        # takes each value in the list and assigns it to a variable, in order
        print("x1 is:",x1,"x2 is:",x2,"y1 is:",y1,"y2 is:",y2,"z1 is:",z1,"z2 is:",z2)
        # prints each variable in a format that makes them easier to identify

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”