C#控制台应用程序通过蓝牙与Arduino通信

4
这里除了这个不起作用,我也不知道为什么。Arduino上的串行输出没有任何反应。C#代码的输出只是等待响应,然后就没有任何反应了。当我启动C#程序时,Arduino上的蓝牙卡LED变绿,说明已经建立了连接。但是其他什么也没有发生。

Arduino 代码

#include <SoftwareSerial.h> //Software Serial Port
#define RxD 8 // This is the pin that the Bluetooth (BT_TX) will transmit to the Arduino (RxD)
#define TxD 7 // This is the pin that the Bluetooth (BT_RX) will receive from the Arduino (TxD)

SoftwareSerial blueToothSerial(RxD,TxD);
boolean light = false;
void setup(){
  Serial.begin(9600); // Allow Serial communication via USB cable to computer (if required)
  pinMode(RxD, INPUT); // Setup the Arduino to receive INPUT from the bluetooth shield on Digital Pin 6
  pinMode(TxD, OUTPUT); // Setup the Arduino to send data (OUTPUT) to the bluetooth shield on Digital Pin 7
  pinMode(13,OUTPUT); // Use onboard LED if required.
}

void loop(){
   delay(1000);
   if(light){
      light = false;
      digitalWrite(13,LOW);
   }
   else{
     light = true;
     digitalWrite(13,HIGH);
   }
   //Serial.println(blueToothSerial.available());
   blueToothSerial.write("Im alive");
   if(blueToothSerial.read()>0){
     Serial.write(blueToothSerial.read());
   }
}

核心C#代码

static void Main(string[] args)
        {
        byte[] Command = Encoding.ASCII.GetBytes("It works");//{0x00,0x01,0x88};

        SerialPort BlueToothConnection = new SerialPort();
        BlueToothConnection.BaudRate = (9600);

        BlueToothConnection.PortName = "COM4";
        BlueToothConnection.Open();
        if (BlueToothConnection.IsOpen)
        {

            BlueToothConnection.ErrorReceived += new SerialErrorReceivedEventHandler(BlueToothConnection_ErrorReceived);

            // Declare a 2 bytes vector to store the message length header
            Byte[] MessageLength = { 0x00, 0x00 };

            //set the LSB to the length of the message
            MessageLength[0] = (byte)Command.Length;


            //send the 2 bytes header
            BlueToothConnection.Write(MessageLength, 0, MessageLength.Length);

            // send the message itself

            System.Threading.Thread.Sleep(2000);
            BlueToothConnection.Write(Command, 0, Command.Length);

            Messages(5, ""); //This is the last thing that prints before it just waits for response.

            int length = BlueToothConnection.ReadByte();
            Messages(6, "");
            // retrieve the reply data
            for (int i = 0; i < length; i++)
            {
                Messages(7, (BlueToothConnection.ReadByte().ToString()));
            }
        }
    }
2个回答

4

好的,有几个问题从上面很难看出来,所以我尝试解释一下。

首先,蓝牙卡上的RX和TX不要连接到Arduino上相应的引脚,而是要连接到它们的对应引脚。

例如:

蓝牙卡 RX -> Arduino TX

蓝牙卡 TX -> Arduino RX

因此,如果你看看上面的Arduino照片,你就会明显地发现7号和8号引脚错了。

我遇到的另一个问题是连接不稳定。

在上述配置中,电源和地线似乎都正常。然而,RX和TX连接需要焊接。否则会得到一个不稳定的连接。

一旦我做出这些改变,上面的代码就完美地工作了。

希望对其他遇到这些问题的人有所帮助!


2
以下是简化后用于测试PC <-> Arduino BT通信的类似代码。在下面的代码中,PC向Arduino发送文本,然后Arduino将其回显到PC上。PC将传入的文本写入控制台。
Windows设置您的BT串行端口(示例中为COM5)。
使用Arduino Uno、USB-蓝牙dongle和蓝牙模块HC-05进行测试。在将Arduino的PIN1(TX)电压馈送到模块的RX引脚之前,将其从5V分压至2.5V。
就这样。很容易!
Arduino:
void setup() {
    Serial.begin(9600); 
    delay(50);
}

void loop() {
    if (Serial.available())
    Serial.write(Serial.read()); // echo everything
}

C#:
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

public class SerialTest
{
    public static void Main()
    {
        SerialPort serialPort = new SerialPort();
        serialPort.BaudRate = 9600;
        serialPort.PortName = "COM5"; // Set in Windows
        serialPort.Open();
        int counter = 0;
        while (serialPort.IsOpen)
        {
            // WRITE THE INCOMING BUFFER TO CONSOLE
            while (serialPort.BytesToRead > 0)
            {
                Console.Write(Convert.ToChar(serialPort.ReadChar()));
            }
            // SEND
            serialPort.WriteLine("PC counter: " + (counter++));
            Thread.Sleep(500);
        }
    }
}

非常感谢您的帮助,对我来说非常完美地与HC-06蓝牙设备和Windows 10笔记本电脑配合使用。我不得不将thread.Sleep更改为1000,我不确定为什么。 - Alex Horlock

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接