We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Maybe use
arduinoPort.bufferUntil(ENTER);
within setup(): https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.htmlThen replace readStringUntil() w/ readString():
https://Processing.org/reference/libraries/serial/Serial_readString_.html
And serialEvent() is more precise than available() methinks:
https://Processing.org/reference/libraries/serial/serialEvent_.html
And almost forgetting: trim() ( or strip() ) is our best friend to clean up readings from Serial:
https://Processing.org/reference/trim_.html
http://py.Processing.org/reference/string_strip.html
I was looking at serialEvent() and I don't understand its use. I am assuming in the Java mode it gets called automatically. But in the Python mode I have to call serialEvent() after defining it which makes me think that it acts as just another function. I will look into bufferUntil() and using readString(). Hopefully, it'll help me :)