# Bluetooth issue - com port busy when restarting a new program

**URL:** <https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 12, 2021, 9:48am UTC](https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297 "2021-04-12T09:48:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neuro503](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@Neuro503](https://discourse.processing.org/u/Neuro503)\
**Post date:** [April 12, 2021, 9:48am UTC](https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297/1 "2021-04-12T09:48:25Z")

</div>

Hi All,

This is essentially a com port busy issue. I am unable to find a solution for this other than resetting the M5stickC. (Basically a ESP32)

The M5stickC is programmed as a bluetooth device. It sends data out constantly. The processing sketch monitors the Bluetooth com port (incoming) and reads data from it.

Data can be send by the device and received by the PC via bluetooth. The program works fine on the first go, turning the device on beofre running the sketch, doing everything I expect it to.

Here is where the problem begins, now, after I stop the processing sketch and I try to Run the code again, the following error appears,  
RuntimeException: Error opening serial port COM10: Port busy

I tried a few bluetooth code examples and this behaviour just repeats itself.

I am assuming that once I closed the program, the com port is still ‘closed’ to new programs, even if a new instance is started. Therefore the error returned that the port is busy.

I’m not sure what is causing this or how it can be resolved. The current solution that I have are

1. power on and off the bluetooth device for each processing Run.
2. For some reason, this does not happen to a serial com port that is connect via USB, To get another ESP32/arduino to read the bluetooth data and stream it via USB.

I am looking for a solution where I do not need another MCU, or having to power cycle the device each time I start the processing sketch.

One of the sample code that I used for processing to read the bluetooth data.

```auto
import processing.serial.*;

Serial myPort; // The serial port

void setup () {
  // set the window size:
  size(300, 100);        

  // List all the available serial ports
  printArray(Serial.list());
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0xff);
}

void draw () {
  //Map and draw the line for new data point
  
  delay(10);
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
  println(inString);
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);    
  }
  
}

```

Sample Arduino sketch that I used

```auto
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;
int i;
void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  i = 0;
}

void loop() {
  SerialBT.print("testing ");
  SerialBT.println(i);
  i++;
  delay(20);
}

```

---

<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:** [April 13, 2021, 9:24am UTC](https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297/2 "2021-04-13T09:24:14Z")

</div>

Hi @Neuro503

I don’t have the exact answer you need, but seeing there’s no avalanche of replies…

I have a few Arduino+Bluetooth(BT) projects running.

I don’t use Windows+PC+BT to connect to them. You might achieve something by running device manager, and disable/enable the com port. I’ve forgotten what the BT config looks like, but there might be ways of unconfiguring and reconfiguring there to get it to reconnect. If it works it can probably be automated with command line, which can be run from Processing.

On my Android phone I have BlueTerm2. It can always connect to the Ard+BT (if not already connected to something else). You could use this to confirm that Ard+BT is fine, problem is in PC.

You could move your project to Android phone. Example in this [topic](https://discourse.processing.org/t/anyone-possible-make-project/25109/25).

You could use a [usb-ttl serial adaptor](https://www.amazon.co.uk/dp/B07R17BMTL/ref=syn_sd_onsite_desktop_90?psc=1&uh_it=64f45e370c5eea47702ac3444ef28e4c_CT&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUFXUDJGN1FXTzROTFYmZW5jcnlwdGVkSWQ9QTAxNTAxMjVLODgzSVpBWkg1Q1QmZW5jcnlwdGVkQWRJZD1BMDYzMzkzMFhDUE05WTZYTU5YMCZ3aWRnZXROYW1lPXNkX29uc2l0ZV9kZXNrdG9wJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==) connected to [HC05](https://www.amazon.co.uk/WINGONEER%C2%AE-Wireless-Bluetooth-Transceiver-Arduino/dp/B01DC9FZ2I/ref=sr_1_3?dchild=1&keywords=hc05&qid=1618305722&s=electronics&sr=1-3). This would connect reliably and give you a normal serial port on the PC. (Prices have gone up lately, CV19?)

Hope this helps,

Richard.

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [April 13, 2021, 12:15pm UTC](https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297/3 "2021-04-13T12:15:12Z")

</div>

Hi,  
i will try to carefully close the connection when you stop the sketch (and hope it will release nicely client bluetooth connection):  
it means you need to handle an exit event as discussed here: [https://forum.processing.org/one/topic/run-code-on-exit.html](https://forum.processing.org/one/topic/run-code-on-exit.html)  
test different way to get the event as it looks to be a little bit os dependant and vary on how you close the sketch  
hope it helps…

---

<div class="post-metadata">

**Author:** ![LuisC2802](https://avatars.discourse-cdn.com/v4/letter/l/9de0a6/32.png) [@LuisC2802](https://discourse.processing.org/u/LuisC2802)\
**Post date:** [April 23, 2021, 11:26am UTC](https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297/4 "2021-04-23T11:26:25Z")

</div>

Hi,  
I have the same problem and i don’t know another solution different of disconnect and connect the device over new.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [April 23, 2021, 6:23pm UTC](https://discourse.processing.org/t/bluetooth-issue-com-port-busy-when-restarting-a-new-program/29297/5 "2021-04-23T18:23:14Z")

</div>

For whatever it’s worth, the GitHub link below is what I use to connect/disconnect my Android to a bluetooth device: [https://github.com/vsquared/Processing\_Android](https://github.com/vsquared/Processing_Android)
