通过SerialPort发送的字节在Netduino上通过蓝牙接收时不一样

时间:2014-10-25 19:34:59

标签: c# bluetooth serial-port .net-micro-framework netduino

我正在开发一个项目,我使用this website作为参考,让我的Netduino与我的PC进行通信。

我已经购买了this Bluetooth个收发器。它似乎是原始帖子使用的版本的更新版本。在他的网站上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上没有数据接收。

1 个答案:

答案 0 :(得分:1)

所以......我终于明白了。代码没有变化。答案似乎很简单,但没有人费心去解释它;我只需要切换TXD和RXD引脚。

Netduino的COM1表示PIN0是RX引脚,PIN1是TX引脚。它希望在PIN0上接收数据并在PIN1上发送。蓝牙组件将在其TX上发送数据,Netduino应在RX上接收数据;蓝牙TX(发送数据)应连接到Netduino的RX(接收数据),蓝牙的RX(接收数据)应连接到Netduino的TX引脚(发送数据)。

相关问题