We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm working on a sketch that is receiving network events from an external program (specifically, an OpenFrameworks sketch), using the processing.net library.
Inside the draw method, I have the following code to parse the incoming data, and assign it appropriately to display a value of text in a text label:
void draw()
{
// check for incoming data
Client client = server.available();
if (client != null) {
// check for a full line of incoming data
String line = client.readStringUntil('\n');
if (line != null) {
//println(line);
int val = int(trim(line)); // extract the predicted class
//println(val);
if (val == 1) {
messageText = "EVENT 1";
} else if (val == 2) {
messageText = "EVENT 2";
} else if (val == 3) {
messageText = "EVENT 3";
}
}
}
// draw
background(0);
textFont(f,64);
fill(255);
textAlign(CENTER);
text(messageText, width/2, height/2);
}
Through logging, I have verified that the data is being received properly
However, I'm experiencing a very annoying bug - the text of my messageText
label is VERY slow to update...after a new event has occurred (and is shown as such through logging), the messageText will still display the value of the last event for several seconds.
Anyone have any pointers on how to speed up performance here?
Thanks!
Answers
On a side note:
can all go in setup.
Related to receiving data... are you sending data to the same machine?
Through logging, I have verified that the data is being received properly
You are using a third party SW to do this? What SW is this? What rates are you receiving?
Kf
@kkfrajer Thank you for the side note!
I may have misspoke - I was using
println
to verify that the data was being received properly. The data is being sent from an OpenFrameworks sketch to this Processing sketch, on the same machine. I'm not sure of the rate at which I'm receiving - can that be configured withServer
somehow?I have no experience with OFW.... I have experience using the server-client examples (https://processing.org/reference/libraries/net/index.html) on the same machine without any problems. I can say that oscP5 works as well out of the box.
Can you provide more details about your setup? Even sharing your second code or a minimum runnable version just to see your approach?
This next would be my first attempt to understand what I am receiving:
Kf
Hey @kfrajer, I almost figured out the problem - in an extremely hacky sort of way.
Basically, after changing the framerate to 600, and the renderer to P3D, everything worked perfectly - the issue was that the network code inside of
draw()
was not being updated fast enough!As I said though, this is extremely hacky.
I then realized that the Network library has
clientEvent
andserverEvent
methods!So, I tried implementing the
clientEvent
method as such. However, I think I may be misunderstanding something...even though my original, hacky code seems to work OK now, my new code below using this delegate method doesn't seem to work at all. Basically, I have to run my sketch first, which creates a server, that my external program connects to. That program then sends out data that's received by my Processing sketch.Here's what my full sketch looks like - anyone know where my misunderstanding may be coming from?
Method readStringUntil() is useless within clientEvent(), b/c the latter is triggered for each single
byte
received, so you don't get more than 1byte
anyways.Instead, use readChar() there, gathering each
char
returned from it, concatenating+=
them into 1 global String variable.Let's name that String as received.
Also declare another global String called message.
Now check whether the current
char
received is an ENTER, that is an\n
.If
true
, assign received to message. And then clear received afterwards, assigning it to""
.