Reputation: 397
I have a working setup in which a raspberry pi runs a headless processing sketch upon booting. This sketch connects the Pi's onboard bluetooth to an HC-06. The Pi also sets up a serial connection to an arduino nano through a USB cable. The processing sketch acts as a relay. It relays bytes from the arduino to the Hc-06 and vice versa.
The device with the HC-06 is an arduino nano. This device sends out a handshaking signal so the arduino on the Pi's end knows it's connected and sends a respons.
This all works like a charm but on one condition. The Hc-06 needs to be 'on' before the processing sketch boots. If I turn on the HC-06 too late or if I turn it on/off I cannot reconnect and the processing sketch is to be rebooted.
I want to program a more advanced hand shaking protocol with an time-out feature. So both device will be aware that the connection is severed.
I start the processing sketch via a shell script
sudo rfcomm bind hci0 20:14:04:15:23:75
sudo killall java
xvfb-run processing-java --sketch=/home/pi/Documents/bluetooth --run # runs headless
The rfcomm bind command is only yo be run once upon booting.
And the bluetooth script:
import processing.serial.*;
Serial handController;
Serial central;
byte mode;
void setup()
{
printArray(Serial.list());
size(200,200);
background(0); // black
central = new Serial( this, Serial.list()[3], 115200);
handController = new Serial( this , Serial.list()[0] , 115200 );
}
long prev;
byte tgl = 0;
void draw()
{
if(handController.available() > 0) {
int c = handController.read();
println(" handcontroller:\t" + (char) c + "\t" + c); // as well char as dec value
central.write(c);
}
....
Is it possible that from within this sketch that I terminate the serial connection to rfcomm0 and then restart it?
It seems that this line sets up the bluetooth connection.
handController = new Serial( this , Serial.list()[0] , 115200 ); // rfcomm0
I am not extremely familiar with java. How can I destroy the serial object? And can I do 'new' a 2nd time from out a function?
Kind regards,
Bas
Upvotes: 3
Views: 238
Reputation: 51837
You can use the Serial's stop()
method to close the serial connection.
You can then re-initialise the port as required.
Here's a rough(untested example):
void restartSerialPort(Serial reference,String portName, int baudRate){
// pause rendering (draw loop)
noLoop();
// stop previous connection
if(reference != null){
reference.stop();
reference = null;
}
// start connection anew
try{
reference = new Serial( this, portName, baudRate);
}catch(Exception e){
println("error opening serial port: " + portName);
e.printStackTrace();
}
// resume rendering
loop();
}
Bare in mind this needs to be tested/tweaked: I'm not 100% the passed reference will update that easily (otherwise the new Serial
object probably needs to be returned by the method and re-assigned to the original reference).
Also not that Processing requires a windowing environment, so it's not quite fully headless.
As a quick alternative to a pure commandline option you can look at Python and the pyserial module
Upvotes: 3