We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › void serialEvent in class!
Page Index Toggle Pages: 1
void serialEvent in class! (Read 2241 times)
void serialEvent in class!
Jan 26th, 2010, 6:56am
 
Can i make :

void serialEvent in a class? like this ?
But it didn't work, this machine said nothing when void serialEvent is in class, and it work when void serialEvent is in the main code....

It is possible to place serialEvent in class?

/////////////////MAIN
import processing.serial.*;
import processing.opengl.*;

Plotter myPlotter;

void setup()
{
 size(420*2, 297*2,OPENGL);
 frameRate(100);
 myPlotter = new Plotter(this,0);
}

void draw() {
 myPlotter.draw();

}

////////////////Class

import processing.serial.*;

class Plotter extends PApplet{
 
 Serial myPort;
 byte fin = 3;
 PApplet obj;

 int pointerX=0;
 int pointerY;
 float coef=0.1;
 boolean occupe=true;
 int lf = 10;

 Plotter(PApplet obj,int NumSerial){
   String portName = Serial.list()[NumSerial];
   myPort = new Serial(obj, portName, 9600);
 }

 void serialEvent(Serial p) {
   String inString = p.readString();
   if(inString!=null){
     println("Machine said = "+inString);
   }
 }

//////////////////////////////////////FIN DE CLASS  
}
Re: void serialEvent in class!
Reply #1 - Jan 27th, 2010, 1:17am
 
What does not work? And how was it implemented when it worked? From this code (I'm not too familiar with how extend works though) it seems like you never call your serialEvent, but I guess that is supposed to be triggered by the event itself ... If I should guess, I would say it is because the serialEvent is being hidden inside the Plotter class, and thus will not be triggered through the main code. Since the 'normal' way to call that function/method would be to say myPlotter.serialEvent() ... which is not correct ... So, I don't know ... but why not just keep it in main?
Re: void serialEvent in class!
Reply #2 - Jan 27th, 2010, 4:58pm
 
Usually, when you make
void serialEvent(){}
in the main it work perfectly, it's i kind of thread who read perfectly data of serial. and you don't need to call serialEvent on the draw(){}.
But when a call it on class, i need to call it on the draw and it work very very badly....
Exemple : the data is by exemple : "23$22$26$YT";
when i make it on the main it said :
"23$22$26$YT";
"23$22$26$YT";
ok perfect.

But when i make it on class i need to call it on the draw of class and it said "23$22"
"$26$YT"
"23$"
"22$26"
"$YT"

in bref, is completly unorganised and chaotic....

And i can't use it on the main....
Re: void serialEvent in class!
Reply #3 - Jan 27th, 2010, 11:54pm
 
I see. But why can't you use it in main?
Re: void serialEvent in class!
Reply #4 - Jan 28th, 2010, 1:16am
 
Because this class work with a old school graphical machine, some of my student wan't to draw generative shape in real time, and i make the class to communicate easly with the machine, and i put all in the class, and i translate the function of the machine, ( HPGL ). and after we wan't to use not just 1 but 3,4 machines, and we don't need to make dirty things in the main.

And that the story...Impossible to use the main.

Thank
Re: void serialEvent in class!
Reply #5 - Jan 28th, 2010, 4:40am
 
But this is just your printing statements not 'lined up'? What happens if you say print() instead of println()? What if you use 'readStringUntil()' instead of just readString()? I'm no expert in this, but it seems to me that you have it working, you just need to tweak it a little bit ...
Page Index Toggle Pages: 1