繁体   English   中英

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

[英]C# console application talking to Arduino via Bluetooth

除此以外,这里没有太多要说的,而且我也不知道为什么。

Arduino上的串行输出什么都没有。 C#代码的输出下降到等待响应,然后什么都没有。

当我启动C#程序时,Arduino上的Bluetooth卡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()));
            }
        }
    }

好了,有一些问题很难从上面看到,所以我将尝试解释。

首先,蓝牙卡上的RX和TX不能与arduino上的相等。 他们走到对立面。

例:

蓝牙卡RX-> Arduino上的TX

蓝牙卡TX-> Arduino上的RX

因此,如果您在上面的Arduino照片中看,应该很清楚地看到插针7和8是不正确的位置。

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

在上述配置中,电源和地似乎都不错。 但是,RX和TX连接需要焊接。否则,连接会变差。

一旦做出更改,上面的代码就可以正常工作。

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

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

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

经过Arduino Uno,USB蓝牙加密狗和蓝牙模块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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM