I think you use inString in draw() cycle before a value has been set to it in serialEvent(). Why donât you put print statement in serial event? Then it would be certain that inString has a value and doesnât return null.
I think sampling rate sets how often computer checks the port. With input the serialEvent() called. I donât know if it would be called 115200 times a second. Instead input could be buffered to a larger bunch to reduce call ups.
In my opinion , 115200 means the maximum bytes could the gauge exchange with computer in 1 sec, which should be called baud rate. Baud rate , the sampling rate of the gauge and the processing speed of program effect the readings together.I guess in my code, if I separate readString() and print(), it may miss the value. I removed serialEvent () part then move the code into draw(), now it works well. The value of gauge can be read in a high speed and recorded in a .csv to analysis.
Thanks for your advice! It does help me a lot
import processing.serial.*;
Serial myPort;
String inString = "no inString yet";
String Force = "no Force yet";
int trigger;
boolean data;
void setup()
{
printArray(Serial.list());
myPort = new Serial(this, "COM5", 115200);
delay(1000);
// Comment this line to see what happens!
myPort.bufferUntil('N'); // See reference
trigger = '?'; // This will be cast to an int
frameRate(60); // This is the default.
delay(1000);
}
void draw()
{
background(255);
if (data)
{
Force = inString;
println(Force); // Only prints valid data
data = false;
}
// Request data every 30 frames
if (frameCount%30 == 0)
{
myPort.write(trigger);
}
fill(0);
text(Force, 20, 20); // Displays each frame
}
void serialEvent(Serial myPort)
{
//inString = myPort.readStringUntil('N'); //Aleady buffered!
inString = myPort.readString();
if (inString != null) // It should not be null here. Can add other checks too!
{
println("Event: ", inString);
data = true;
}
}
The important thing when using serialEvent() is to buffer until a character⌠I chose âNâ since this is what I saw in the picture.
There may be other characters (whitespace such as /r and /n) but I canât see them in your picture.
Do you get a line feed when you donât use the timestamp?