We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I've successfully done this long time ago but for some reason now it's not working. I'm just trying to read data from arduino via serial. I'm sending a string with 3 values (range 0-360), but I'm getting "Error, disabling serialEvent() for COM3 null" in Processing and can't figure out the issue.
Here's my code
import processing.serial.*;
Serial myPort;
void setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
}
void serialEvent(Serial myPort){
String val = myPort.readStringUntil('\n');
val = trim(val);
int gyro[] = int(split(val, ','));
print("X: " + gyro[0]);
print(", Y: " + gyro[1]);
println(", Z: " + gyro[2]);
}
It's pretty much the same code I've used before but no idea what's wrong..help?
here's my arduino printing code
//the vars are integers range 0 -360
Serial.print(GyX);
Serial.print(',');
Serial.print(GyY);
Serial.print(',');
Serial.print(GyZ);
Serial.print('\n');
thanks!
Answers
https://forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1
thanks..it works..but
I still don't understand exactly what's wrong in my previous code..?
it seems I'm not able to access the element of the array separately?
if I do
print(gyro[1]);
it will say that the index is out of bound..but gyro is an array and the split function is supposed to split the incoming string and store each value in a different cell of the array right?
Check that var is no null before splitting it. For debugging add after line 16 in your first post:
Kf
That statement fails b/c gyro[] array is still
null
. @-)It is only initialized for the 1st time when serialEvent() is called back by the Serial's Thread. =;
In order to fix that premature access, just initialize gyro[] array w/ some arbitrary length: *-:)
right :D..now working..just out of curiosity would you be able to tell why my initial code wasn't working? thanks!!
@ your statement
String val = myPort.readStringUntil('\n');
, val can benull
if'\n'
isn't found! :-SThat's why we should always use bufferUntil():
https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html
Doing so, serialEvent() is only called back when the specified character arrives: <:-P
https://Processing.org/reference/libraries/serial/serialEvent_.html
ok..makes sense..bust how's that possible? I was sending it every time
By default, serialEvent() is triggered by every single
byte
arrived! :-&Again, that's why we need to customize it w/ bufferUntil() or buffer(). :-B
@plux
There is no need to initialize the container size. Consider the following example. I commented out the init of the array and I handle two very important situations. This is very simple error handling and one ways to do this.
Kf
@GoToLoop
Why would n1 exchange a single statement like
print(gyro[1], "- ");
for some extra big function like showString()? :-\"While a single
new int[VALUES];
initialization would completely avoid such addendum? :(|)