1

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
  • Is the arduino actually outputting " start chr"? Are there two newlines between values? Or do you receive 255199? Commented Feb 23, 2013 at 9:55
  • it just outputs 255 1 99 on separate lines. i would like to get them into data0 data1 data2. Commented Feb 23, 2013 at 13:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

don't know what you mean but my arduino Serial.print(data0) Serial.print(data1) Serial.print(data2)
@jamesjordan: print or println? That code would produce 255199, which is impossible to parse.
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.