DF Robot蓝牙模块如何连接到Arduino,使Arduino可以与之通信。
我使用了本教程。
我设法让模块上的灯闪烁,它似乎能够很好地配对,但是当我运行串行监视器并发送一个应该打开灯的字母(比如“H”)时,我得到一个 Java 错误:
java.io.IOException: Bad file descriptor in nativeDrain
at gnu.io.RXTXPort.nativeDrain(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.flush(RXTXPort.java:1201)
at processing.app.Serial.write(Serial.java:470)
at processing.app.Serial.write(Serial.java:492)
at processing.app.SerialMonitor.send(SerialMonitor.java:128)
at processing.app.SerialMonitor.access$100(SerialMonitor.java:29)
at processing.app.SerialMonitor$4.actionPerformed(SerialMonitor.java:82)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我检查了接线,我将 RXD 连接到板上的 RX(引脚 0),将 TXD 连接到 TX(引脚 1),其余的连接起来,但还是没有运气。可能是什么问题?
这是我在 Arduino 上的代码:
int ledpin = 13;
char val;
void setup() {
pinMode(ledpin, OUTPUT); // pin 48 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 9600bps
}
void loop() {
if( Serial.available() ) { // if data is available to read
val = Serial.read(); // read it and store it in 'val'
}
if( val == 'H' ) { // if 'H' was received
digitalWrite(ledpin, HIGH); // turn ON the LED
} else {
digitalWrite(ledpin, LOW); // otherwise turn it OFF
}
delay(100); // wait 100ms for next reading
}