Port get busy after a few times running. Is it because of P3D?

edited October 2017 in Arduino

Hi,

I have come to a problem of having error from using Window 7 with arduino Leonado sending data through Serial port. I tested a code that getting only the data from both Mac and Window and it works fine. Then I make a video to put on a canvas for Spout to project it out to other program. Note that with syphon everything works fine too. I have ran into a post somewhere that posted long ago that a guy had the same problem of mine and he fixed it by changing to openGL but I also learned that openGL now is the same as P3D from a more recent post here in this forum. ** I didn't run anything at the same time of processing.** First time running usually fine, but for the second or third then the error will say that my port is busy! And I will have no choice but restart the computer again. !!!! Is this a bug? Is there a solution?

Here is the code that runs fine on both operation

import processing.serial.*;
Serial myPort ;
int value, wheel;
String myString = null;
int weight = 800;
int lf = 10;

void setup() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.clear();
  myPort.bufferUntil(lf);
  myString = myPort.readStringUntil(lf);
}
void draw() {
println("in draw once");
 println("fsr: "+ value);
    println("wheel position: "+ wheel);
    println("-----------");
}

void serialEvent(Serial p) {
  //println("access Serial port");
  myString = p.readStringUntil('\n');
  if (myString !=null) {
    //println("get data from Serial port");
    String[] fsr =split(myString, 'r');
    //printArray(fsr);
    value = int(fsr[0]);
    String trim = trim(fsr[1]);
    wheel = int(trim);

  }
}

And this is a code that has a problem in Window 7 (x64)

import spout.*;// IMPORT THE SPOUT LIBRARY
import processing.serial.*;//Serial to Arduino
import processing.video.*;//play vdo

int nSenders = 5;
PGraphics[] canvas;
Spout[] senders;

int lf=10;
Serial myPort;
int value, wheel;
String myString = null;
Movie myMovie;
int arduinoValue = 800;

int projectorWidth = 1024;
int projectorHeight = 768;
//int scalar = 3;

//-----swtiches------//
Boolean arduinoSwitch = true;
Boolean sensorSwitch = false;
Boolean spoutSwitch = true;
//-----------//

void settings() {
  size(projectorWidth, projectorHeight,P3D);
  // size is doesn't change the size of canvas in term of sending graphic signal out.
}
void setup() {
  canvas = new PGraphics[nSenders];
  for (int i=0; i<nSenders; i++) {
    canvas[i] = createGraphics(projectorWidth, projectorHeight, P3D);
  }
  printArray(Serial.list());

  // Create Spout senders to send frames out.
  if (spoutSwitch) {
    senders = new Spout[nSenders];
    for (int i = 0; i < nSenders; i++) { 
      senders[i] = new Spout(this);
      String sendername = "Test "+i;
      senders[i].createSender(sendername, projectorWidth, projectorHeight);
    }
  }  
  if (arduinoSwitch) {
    myPort = new Serial(this, Serial.list()[1], 9600);
    myPort.clear();
    myPort.bufferUntil(lf);
    myString = myPort.readStringUntil(lf);
  }
  myMovie = new Movie (this, "C:/Users/dma/Desktop/tuang/test1.mp4");
}

void draw() {
  canvas[0].beginDraw();
  canvas[0].background(255, 0, 100);
  canvas[0].image(myMovie, 0, 0);
  if (spoutSwitch) {
    for (int i = 0; i < nSenders; i++) {
      senders[i].sendTexture(canvas[i]);    //send all separated spout canvases
    }
  }
  if (arduinoSwitch) {
    println("fsr: "+value);
    println("position wheel: "+wheel);
    if (value < arduinoValue) {
      sensorSwitch=true;
      myMovie.stop();
      canvas[0].background(0);
      println("nothing in the house");
    }
    if (sensorSwitch) {
      if (value>=arduinoValue) {
        println("Force sensor is detacted");
        //newRandom=true;
        //randomMovie();
        myMovie.play();
        sensorSwitch=false;
      }
    }
  }
  canvas[0].endDraw();
  image(canvas[0], 1024/2*(0%2), 768/2*(0/2));
}
void movieEvent(Movie m) {
  m.read();
}

void serialEvent(Serial p) {
  if (arduinoSwitch) {
    myString = p.readStringUntil('\n');
    if (myString != null) {
      String[] fsr = split(myString, 'r');
      //printArray(fsr);
      value = int(fsr[0]);
      String trim = trim(fsr[1]);
      wheel = int(trim);
    }
  } else {
    println("Arduino is off.");
  }
}
//keyboard for testing vdo
void keyPressed() {
  switch (key) {
  case 'a' : 
    myMovie.play();
    break;
  case 'r' :
    myMovie.stop();
    break;
  }
}

What should I do to stop the port being too busy ! I didn't even run anything but processing.

Answers

  • I think netstat will show you which ports are in use and there's an option to tell you which process is using them. P3D seems an unlikely candidate to me.

    Have you tried using stop() on your serial port?

  • @koogs hey I didnot know that we can stop the port. Can you tell me where should I put it ?? This happen when I clicked exit from the java window tho.

  • but for the second or third then the error will say that my port is busy

    Does it highlight a particular line in your code?

    To clarify, you don't have any issues when running your first code sample in Win 7?

    Kf

  • edited October 2017

    Hi @kfrajer,

    Yes, I don't have that for the very first time of running.

    Here is what happened from the experiment: Every very first time, when turned on the computer, the program will run fine. Sometimes the second will be okay but sometimes doesn't. My friend pointed out that the error said something about the port is still open somewhere. I found the "java.exe" caused the problem in task manager. Although I close the play window from Processing IDE this computer still doesn't close the window. It mostly ran behind the scene. I have to go to task manager and click "end process" instead. My assumption is that this computer somehow fail to close the program and needed to be told, maybe from command line to change the setting. However, I do not know much about the command line thing so I'm still stuck with this silly issue.

    A little more detail: I moved Serial lines in the setup() to beginning of the function instead of the end and notice that it's slightly better.

  • Quite often I find several java.exe in Task Manager, haven't yet worked out what makes the programs not exit properly. I'm not using P2D or P3D. I usually use 'esc' or alt-space-c to close the window. Almost never stop on the IDE.

  • I did both esc and the exit button from the ide but the alt+space+c. So I will try it tomorrow.

  • Yes, I don't have that for the very first time of running.

    You didn't address my question:

    To clarify, you don't have any issues when running your first code sample in Win 7?

    Please elaborate on this:

    I moved Serial lines in the setup() to beginning of the function instead of the end and notice that it's slightly better.

    What makes you think it is better? Can you run it more times before you get the error?

    Kf

  • Answer ✓

    The serial library has a stop() function, so maybe it's necessary to stop before exit. I'm using the code below and it seems to be exiting and re-running cleanly. It doesn't handle the window being closed. Does Processing have a way of defining an exit handler?

    boolean do_stop  = false;
    
    
    void mousePressed()
    {
        do_stop = true;
    }
    ...
    
    
    
    loop()
    {
    
      // port_exists is set true after connect
    
        if (do_stop) 
        {
            if (port_exists)
            {
                myPort.stop();
                //myPort = null; // made things worse.
                exit();
            }
        }
    }
    
Sign In or Register to comment.