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

除了这个不起作用之外,在这里说的不多,我不知道为什么。

Arduino上的串行输出什么都没有。 C#代码的输出结果是等待响应然后什么都没有。

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

在此输入图像描述在此输入图像描述

Arduino代码

#include  //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上等于它们。 他们走向他们的对立面。

例:

蓝牙卡RX – > Arduino上的TX

蓝牙卡TX – > Arduino上的RX

因此,如果您查看上面的Arduino照片,那么第7和第8针是不正确的放置应该是非常明显的。

我遇到的下一个问题是连接性差。

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

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

希望这可以帮助其他人解决这些问题!!

以下是用于测试 PC < - > Arduino BT通信的类似代码。 在下面的代码中,PC将文本发送到Arduino,然后将其回送到PC。 PC将传入的文本写入控制台。

从Windows设置BT串行端口(示例中为COM5)。

使用Arduino Uno,USB蓝牙适配器和蓝牙模块HC-05进行测试 。 Arduino的PIN1(TX)电压在5V到2.5V之间分压,然后再送到模块的RX引脚。

而已。 简单!

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