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

标签 c# bluetooth serial-port arduino serial-communication

除了这行不通之外,这里没什么好说的,我不知道为什么。

Arduino 上的串行输出什么也没有。 C# 代码的输出变为等待响应,然后什么也没有。

当我启动 C# 程序时,Arduino 上的蓝牙卡 LED 指示灯变绿,表明正在建立连接。仅此而已。

Enter image description here Enter image description here

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()));
            }
        }
    }

最佳答案

好吧,有一些问题很难从上面看出,所以我会尝试解释一下。

首先,蓝牙卡上的 RX 和 TX 不要在 arduino 上达到同等水平。他们走向了对立面。

Example:

Bluetooth Card RX -> TX on Arduino

Bluetooth Card TX -> RX on Arduino

因此,如果您查看上方 Arduino 的照片,您会很明显地发现引脚 7 和 8 的位置不正确。

我遇到的下一个问题是连接不畅。

在上面的配置中,电源和地似乎没问题。但是,需要焊接 RX 和 TX 连接。否则连接会很差。

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

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

关于C# 控制台应用程序通过蓝牙与 Arduino 对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16553997/

相关文章:

c# - 使用 System.Text.Json 一次读取一条记录

android - 多个蓝牙连接

java - Android蓝牙发现未找到设备

c# - .Net Remoting 序列化问题

c# - 排列算法优化

java - 无法在 Android 中扫描或发现 BT 和 BLE 设备

c - 如何在c中通过串口连接编写二进制表示

c - chardev 的主要和次要模式有多稳定?

python - 何时刷新 Pyserial 缓冲区?

c# - 具有无限参数的参数化 SQL 查询