C# 控制台应用程序通过蓝牙与 Arduino 对话 [英] C# console application talking to Arduino via Bluetooth

查看:28
本文介绍了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 对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆