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

**URL:** <https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 10, 2020, 1:46am UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697 "2020-05-10T01:46:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [May 10, 2020, 1:46am UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697/1 "2020-05-10T01:46:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [May 10, 2020, 8:49am UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697/2 "2020-05-10T08:49:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Putzlicht](https://avatars.discourse-cdn.com/v4/letter/p/848f3c/32.png) [@Putzlicht](https://discourse.processing.org/u/Putzlicht)\
**Post date:** [May 10, 2020, 10:31am UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697/3 "2020-05-10T10:31:57Z")

</div>

@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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [May 11, 2020, 6:38pm UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697/4 "2020-05-11T18:38:38Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [May 13, 2020, 8:18pm UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697/5 "2020-05-13T20:18:24Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![gerardjensen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gerardjensen/32/16782_2.png) [@gerardjensen](https://discourse.processing.org/u/gerardjensen)\
**Post date:** [August 10, 2022, 1:44pm UTC](https://discourse.processing.org/t/can-i-reconnect-a-serial-port-without-re-starting-a-sketch/20697/6 "2022-08-10T13:44:25Z")

</div>

> [@laptophead](#):
>
> `BTPort = new Serial(this, "/dev/tty.BD1HC-05-DevB", 112500);`

Well actually this would create a Memory Leak. I recomend using `System.gc();` after assigning the new BTPort value.
