Bluetooth issue - com port busy when restarting a new program

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.

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

#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);
}

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.

You could use a usb-ttl serial adaptor connected to HC05. This would connect reliably and give you a normal serial port on the PC. (Prices have gone up lately, CV19?)

Hope this helps,

Richard.

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
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…

1 Like

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

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