Error handling during serial read

I am working with an Arduino and making a small GUI to communicate with it on Processing. I am just printing out float values onto the serial port and reading it back using Processing. In most cases everything goes well and I am able to read the values. However, sometimes the serial read spits out fairly arbitrary values and I am not sure why. For example,

My Arduino code:

 void setup() {
   // put your setup code here, to run once:
 Serial.begin(9600);
 }

 void loop() {
   for (int i =1;i<5;i++)
 {  Serial.println(float(i)+2.0);
 delay(1);
   // put your main code here, to run repeatedly:
 }
 }

The output according to the Serial monitor is 3.0,4.0,5.0,6.0 as expected.

Here's the processing code (Python Mode) that I use to read the data in

 def connect2Arduino(): 
     global arduinoPort
     arduinoPort= Serial(this, 'COM32',9600)

 def setup():
    background(0)
    connect2Arduino()

 def draw():
    global arduinoPort
    if arduinoPort.available()>0:
        dataIn = arduinoPort.readStringUntil(int(10))
        if (dataIn != None or int(dataIn) != 13):
            print dataIn  

The output from processing looks like this

4.00

5.00

6.00

3.00

4.00

5.‚j

6.°3.00

4.00

5.00

6.00

When I try to change the type of dataIn, an error pops up when output is 5.‚j which is as expected.

First of, I have no idea why these errors exist. I would like to know why it's showing up in the first place. Secondly, what's a fix? I could go print instead of println on my arduino code and maybe fix it, but I am looking for something better.

Thank you

Answers

Sign In or Register to comment.