Formatting a return value from a serial device

2024/10/9 22:20:56

I am reading a value from a device over serial, and the return value has the format:

[Theoretical position in mm, Encoder position in mm], for example, b'\r#-0.001504,-0.001516\n'

I would like to format this return value and save the second value into an array. Does anyone have an idea how I might do this? Any suggestions are much appreciated.

Thanks.

New edit: This is my code and output. At this point I only need to extract the float value from this string.

    ser.write(b'1POS?\r') #command to query positionpos = ser.readline() #read the positionpos_str = str(pos)pos_splt = pos_str.split(",") enc_pos = pos_splt[1]print(enc_pos)

Output: "-0.028970\n'"

Answer

Ok, sorry, I've just realised what your: error "a bytes-like object is required, not 'str'" means.

Its because what you are trying to split is a bytes array, which, although it has a split() method, its requires a bytes array as the parameter.

Try this now:

pos = b'\r#-0.001504,-0.001516\n'   # Sample data from serial portpos_splt = pos.strip().split(b",")  # the param b"," is the bytes version of ","
enc_pos = pos_splt[1]
print(float(enc_pos))

Output

-0.001516
https://en.xdnf.cn/q/118528.html

Related Q&A

if Else statement inside for loop is not working [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.am using following code to check certain conditionsfor myarg in my…

Sending data back and forth from android server to python client

I have posted this few days back but now i ran into another problem after solving that one. DESCRIPTION: working on an android app written in kotlin that behaves as a server side and Python program tha…

Loop to run 4 times to try run a SQL procedure, after 4 attempts then stop

I have attempted to write some code in python to do a loop 4 times. It will fail as spTest doesnt exist. So I want to try loop again (repeated 4 times total) if it still cant find it, I want to break o…

Loops in Python 3.4.3

I apologize ahead of time for my ignorance but I have trying to code something in python that requires a question to be asked to the user and the user responds. Dependent on that response, the program …

Adding userdata on create VM operation with Python SDK for Azure

I am using Python sdk for azure creation virtual machine operation. I want some script to be executed whenever the VM starts. So, I have tried adding the custom-data while creating VM with Python. My d…

python obtain the self variable in another class which already has a self function

I want to use the self variables in one class and use them in another class which already has its own self variables how to do I do this. Some code here to help.class A():self.health = 5 class B(): # T…

Cannot pip install package in virtualenv on EC2

Im seeing this weird issue on ec2. Im trying to install lsm-db package inside my virtualenv, it says its successfully installed but when trying to import the package or do pip list its not there.I crea…

Python: string to integer as a key

Im trying to convert a string column in a dataframe to int. The strings should be replaced with an integer as a key value.Data:user_id site_id 100 url1.com 100 url2.com 100 url1.com 101…

Data manipulation, kind of downsampling

I have a large csv file, example of the data below. I will use an example of eight teams to illustrate.home_team away_team home_score away_score year belgium france 2…

Chrome Native Messaging throwing error when sending a base64 string to client

Using Chrome Native Messaging sample app as a template am able make a system call to bashos.system("<bash command>")The requirement is to return a base64 string from the python scriptos…