serial connection opened in a parallel thread

edited March 2017 in Library Questions

Hi all, i would like to have a serial port declared into an object running a parallel thread (to feed a drawing machine with gcode).

Now, if if i try to open it like this:

myPort = new Serial(this, Serial.list()[0], 57600);

As i would do usually, it don't works, it say: The constructor Serial(PLOT.Controller, String, int) is undefined

So if i try:

myPort = new Serial(new PApplet(), Serial.list()[0], 57600);

I can send command over it.

But if i try to declare a void calle serialEvent() in my class, it does not receive nothing. And if i try:

@Override  void serialEvent(Serial p) { 
  String inString = p.readString(); 
  println(inString);
} 

It thell me: The method serialEvent(Serial) of type PLOT.Controller must override or implement a supertype method

What i'm doing is having a method like this, called at any thread cycle:

 void serialCheck(){
        while(serialPort.available() > 0){
           String incoming  = serialPort.readStringUntil('\n');
           if(incoming != null)print(incoming);
        }
      }

And looks like working, but i don't know if will be stable or there are better ways. Any suggestions?

Thanks

Answers

  • edited March 2017

    Class Serial already got 2 concurrent callbacks: serialEvent() & serialAvailable().

    However, those methods need to belong to an active PApplet instance. =;

    If you do insist, you may attempt to hack Serial's Method fields serialEventMethod or serialAvailableMethod to point to another class' methods. :ar!

  • edited March 2017

    Hmmm, i see, but if my second thread is under the same PApplet, (i guess it is?) why "this" is not accepted so?

  • edited March 2017

    Keyword this is always of the datatype of the current scoped class. ~O)

  • Why don't you post your code, or some sort of sample to let us know what you've achieved so far?

  • edited March 2017

    Here the nested class for the serial connection, than in my code need to work together an instance running it's own thread (here what is around is just an example):

    class Controller implements Runnable{
    
      Feeder feeder;
      boolean loop = true;
      public Controller()
      {
        feeder = new Feeder();
        new Thread(feeder).start();
        feeder.tryConnection();
      }
    
      //@Override 
       public void run(){
        while(loop){
    
          if(feeder.isConnected()){
    
            // do other stuffs
    
          }
          delay(10);
        }
    
      }
    
      class Feeder implements Runnable{
    
        boolean loop = true;
        boolean tryConnection = false;
        boolean isConnected = false;
        Serial serialPort;
    
        //@Override 
         public void run(){
          while(looping){    
            if (tryConnection()){
              connectionAttempt();
              tryConnection = false;
            }
            if(isConnected) serialCheck();  
          }
        }
    
        void connectionAttempt() {
          try{
            printArray(Serial.list());
            serialPort = new Serial(new PApplet(), Serial.list()[0], 57200);
            serialPort.bufferUntil('\n');
            isConnected = true;
            println("connected");
            }catch (Exception e) {
              println("error:", e);
              println("connection attempt failed");
              isConnected = false;
            }
          }
    
          public synchronized void serialWrite(String input){
    
            serialPort.write(input + "\n");
          }
    
          void serialCheck(){
            while(serialPort.available() > 0){
              String incoming  = serialPort.readStringUntil('\n');
              if(incoming != null)print(incoming);
            }
          }
    
          ///////////tryConnection
          public synchronized void tryConnection(boolean input) {tryConnection = input;}
          public synchronized boolean tryConnection() {return tryConnection;}
    
          ///////////isConnected
          public synchronized void isConnected(boolean input) {isConnected = input;}
          public synchronized boolean isConnected() {return isConnected;}
    
        }
      }
    
  • I have to tell that, so far, seems to work correctly. Still i did not tried with more than one serial port.

Sign In or Register to comment.