我正在做一个项目,我使用本网站作为参考,让我的Netduino与我的PC进行通信。
我买了这个蓝牙收发器。这似乎是一个更新的版本所使用的原来的帖子。1.06对1.04在他的网站上。
我将蓝牙的TXD设置为Pin0,RXD设置为Pin1,VCC设置为5V。
这是我在Netduino上的代码:
static SerialPort Bluetooth;
public static void Main()
{
Bluetooth = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
Bluetooth.DataReceived += new SerialDataReceivedEventHandler(Bluetooth_DataReceived);
Bluetooth.Open();
Thread.Sleep(Timeout.Infinite);
}
static void Bluetooth_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] bytes = new byte[1];
while(Bluetooth.BytesToRead > 0)
{
Bluetooth.Read(bytes, 0, bytes.Length);
Debug.Print(bytes[0].ToString());
}
}这是我笔记本电脑上的代码:(这是一个WPF应用程序)
SerialPort serialBT;
private void Connect()
{
// COM7 is the outgoing port that corresponds to the Bluetooth transceiver
serialBT = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One);
serialBT.Open();
serialBT.Write(new byte[] { 23, 24, 25, 26 }, 0, 4);
Debug.Print("Values sent");
}在Netduino上,当我发送23、24、25和26字节数组(只是为了测试目的)时,DataReceived事件会触发。但是,它在调试窗口中接收并打印出来的值是6、0、0和248,而不是应该是23、24、25和26。
我发送的其他值也同样神秘地被转换成完全不同的值。
我已经检查了合适的蓝牙收发器的COM设置,这些都是正确的设置。我已经翻转了TXD和RXD引脚,因为最初的Arduino期望TXD是Pin1,RXD是Pin0,但这不会导致在Netduino上接收任何数据。
发布于 2014-11-07 03:43:33
所以..。我终于让它起作用了。代码没有变化。答案似乎如此简单,但从来没有人真正解释过;我只是需要切换TXD和RXD引脚。
Netduino的COM1意味着PIN0是RX引脚,PIN1是TX引脚。它希望在PIN0上接收数据,并在PIN1上发送数据。蓝牙组件将在其TX上发送数据,Netduino应该在RX上接收数据;蓝牙TX (发送数据)应该连接到Netduino的RX (接收数据),蓝牙的RX(接收数据)应该连接到Netduino的TX引脚(发送数据)。
https://stackoverflow.com/questions/26566436
复制相似问题