Can I reconnect a serial Port without re-Starting a sketch?

I am building a robot ran by an Arduino and I am using the Processing as a GUI.

Sometimes I loose my m BT connection and is a pain to restart the sketch

I am using a GUI button trying to re initialize the connection, but It does not work.

public void button14_click1(GButton source, GEvent event) { //CODE:button14:731462:
BTPort = new Serial(this, “/dev/tty.BD1HC-05-DevB”, 112500);

It seems to work only in the setup.

Any advice is appreciated.
Thanks

1 Like

Hi @laptophead,

I have projects talking serial to Arduino that recover from disconnect and reconnect, but I’m using Windows and standard Arduino COM port.

I use a count value from the Arduino to show comms is good. (There may be port properties or exceptions that would do the same). If that stops:

  • Close the comms with myPort.stop
  • Wait 5 seconds for Windows to sort itself out.
  • If the com port is in the list of com ports then reconnect

(It doesn’t recover from a very quick reconnect of the Arduino, before Windows has made the usb-device-gone sound.)

Hope this helps.

@laptophead
As Richard wrote - periodically check your connection.
If it’s lost - close the port with BTPort.stop().
Reopening should work then.

Good luck.

PS.: My solution to work with the Arduino and it’s IDE is like:

int maxFps = 60;
int portIsOpenFlag = 0;

void CheckAppWindow() {
  
  if (focused == false) {
    if (portIsOpenFlag == 1) { 
      myPort.clear();
      myPort.stop(); 
      portIsOpenFlag = 0;
    } // if
    frameRate(3); 
  } // if


  if (focused == true) { 
    if (portIsOpenFlag == 0) { 
      myPort = new Serial(this, portName, 9600); 
      portIsOpenFlag = 1; 
      myPort.clear(); 
    } // if
    frameRate(maxFps); 
  } // if  
  
} // end

It checks if the app is active or not.
Inactive app - the serial connection will be closed.
Active app - the serial connection will be reopened.
It also drops the cpu load when inactive.

Thanks everyone, I had no idea of the serial “stop”

My connection is based on bursts of data every 400Ms from arduino

So here is what I did

while (BTPort.available() > 0) {
    IncomingStr = BTPort.readStringUntil(lf);
    
    if (IncomingStr != null) {
      First = IncomingStr.charAt(0);
      Trans_Stamp= millis();
}

Than

 if (millis() - Trans_Stamp >1000)
{tts.speak("RECONNECTING ");
  delay(1000);
 fill(p, 100); // Red
    rect(20, 20, width-40, height-40, 7);
   
  // println ("Lost");
   
}

Then Here is my button

public void button14_click1(GButton source, GEvent event) { //_CODE_:button14:731462:
   tts.speak("RECONNECT RESET");
      BTPort.clear();
      BTPort.stop(); 
      
      BTPort = new Serial(this, "/dev/tty.BD1HC-05-DevB", 112500);
     
    
  BTPort.write("R" + '\n' ); 
} //_CODE_:button14:731462:

I reconnect manually , and that is OK

Is there a better way to do it?

Thanks

Your manual reconnect code is works well? assume yes - so develop something that runs it at the right time. Add a ‘int counter’, declared globally at the top. In your receiving comms code set that counter to zero every time. In the draw() increment it. Now you have a measure of how long it is since good comms. Suppose the frameRate is 20, this would attempt a reconnect every 10 sec since loss of comms.

if (counter > 0 && counter % 200 == 0){reconnect();}