Hi I am staring out and have cracked sending multiple bits of information from Python to my Arduino. I now would like to send information from my Arduino to python I have looked at may examples but they all send to show only one line of information being sent.
his is what i am stuck with.The Arduino is sending a start number 255 then an ID then the information it looks like this on the terminal window.
255 start chr
1 ch ID
99 Value
I want to split these back into there individual arrays in python.
thanks for any tips.
2 Answers 2
The easiest way is to use a dedicated separator character (e.g. a comma) and split the strings accordingly. That is: write the Arduino side of your code in such a way that parsing on the Python side becomes easy.
2 Comments
print or println? That code would produce 255199, which is impossible to parse.Print the Arduino items into a line with a delimiter such as a comma like so:
Serial.print(VALUE 1);
Serial.print((" , "));
Serial.print(VALUE 2);
Serial.print((" , "));
Serial.println(LAST VALUE);
Using println for the last value will combine all previous print into one line. With Arduino Serial Monitor results something like this:
VALUE 1 , VALUE 2 , LAST VALUE
You can easily split the values in python and assign a variable to each value like taking values from an array:
SERIALDATA= sensorData.readline() #Read line of text from Arduino
DATASPLIT= SERIALDATA.split(' , ') #Splits the line of text into array of strings composed of each individual sensor data
pyVALUE1= DATASPLIT [0]
pyVALUE2= DATASPLIT [1]
pyFINALVAL= DATASPLIT [2]
Comments
Explore related questions
See similar questions with these tags.
" start chr"? Are there two newlines between values? Or do you receive255199?