本文介绍了C# 控制台应用程序通过蓝牙与 Arduino 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了这行不通之外,没有什么可说的了,我也不知道为什么.

Not a whole lot to say here other than this doesn't work, and I have no idea why.

Arduino 上的串行输出什么都不是.C# 代码的输出归结为等待响应,然后什么都不做.

The serial output on the Arduino is nothing. The output on the C# code goes down to waiting for a response and then nothing.

当我启动 C# 程序时,Arduino 上的蓝牙卡 LED 变为绿色,因此正在建立连接.没有别的.

The Bluetooth card LED on the Arduino goes green when I start the C# program so there is a connection being made. Just nothing else.

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

推荐答案

好的,所以有一些问题很难从上面看到,所以我会尝试解释.

Okay so there were a few issues that are hard to see from above so I will attempt to explain.

首先,蓝牙卡上的 RX 和 TX 在 arduino 上不相等.他们走向对立面.

First, RX and TX on the bluetooth card DO NOT go to their equals on the arduino. They go to their opposites.

示例:

蓝牙卡RX ->Arduino 上的 TX

Bluetooth Card RX -> TX on Arduino

蓝牙卡TX ->Arduino 上的 RX

Bluetooth Card TX -> RX on Arduino

因此,如果您查看上面的 Arduino 照片,很明显引脚 7 和 8 的位置不正确.

So if you look in the photo of the Arduino above it should be painfully obvious that pins 7 and 8 are the incorrect placement.

我遇到的下一个问题是连接不良.

The next issue that I was having was poor connectivity.

在上述配置中,电源和地似乎没问题.但是,RX 和 TX 连接需要焊接.否则连接不良.

The power and ground seem to be fine in the above configuration. However, the RX and TX connections need to be soldered in. Otherwise you get a poor connection.

一旦我做了这个更改,上面的代码就完美无缺.

Once I made this changes the above code worked flawlessly.

希望这能帮助其他遇到这些问题的人!!

Hope this helps anyone else having these issues!!

这篇关于C# 控制台应用程序通过蓝牙与 Arduino 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 02:46