[英]Reconnect raspberry pi's bluetooth to an HC-06 in a processing sketch
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.
此草图将 Pi 的板载蓝牙连接到 HC-06。 The Pi also sets up a serial connection to an arduino nano through a USB cable.
Pi 还通过 USB 电缆与 arduino nano 建立串行连接。 The processing sketch acts as a relay.
处理草图充当中继。 It relays bytes from the arduino to the Hc-06 and vice versa.
它将字节从 arduino 中继到 Hc-06,反之亦然。
The device with the HC-06 is an arduino nano.带有 HC-06 的设备是 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.
该设备发出握手信号,因此 Pi 端的 arduino 知道它已连接并发送响应。
This all works like a charm but on one condition.这一切都像一个魅力,但在一个条件下。 The Hc-06 needs to be 'on' before the processing sketch boots.
在处理草图启动之前,Hc-06 需要“开启”。 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.
如果我打开 HC-06 太晚或者如果我打开/关闭它,我将无法重新连接并且处理草图将重新启动。
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我通过 shell 脚本开始处理草图
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. rfcomm bind 命令仅在引导时运行一次。
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?是否有可能在此草图中终止与 rfcomm0 的串行连接然后重新启动它?
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.我对 java 不是很熟悉。 How can I destroy the serial object?
如何销毁串行 object? And can I do 'new' a 2nd time from out a function?
我可以从 function 中第二次“新建”吗?
Kind regards,亲切的问候,
Bas巴斯
You can use the Serial's stop()
method to close the serial connection.您可以使用 Serial 的
stop()
方法关闭串行连接。 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).请记住,这需要进行测试/调整:我不是 100% 传递的引用会轻松更新(否则新的
Serial
object 可能需要由方法返回并重新分配给原始引用)。
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作为纯命令行选项的快速替代方案,您可以查看 Python 和pyserial 模块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.