C#控制台应用程序交谈通过蓝牙Arduino的 [英] C# console application talking to Arduino via Bluetooth

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

问题描述

不是一大堆说这里除了这不工作,我不知道为什么。

在Arduino的串行输出是什么。在C#code的产量下降到等待响应,然后什么都没有。

蓝牙卡上的Arduino的LED走向绿色,当我开始在C#程序所以正在进行的连接。只是仅此而已。


的Arduino code

 的#include< SoftwareSerial.h> //软件串口
#定义的RxD 8 //这是蓝牙(BT_TX)将发送到Arduino的引脚(RXD)
#定义的TxD 7 //这是蓝牙(BT_RX)将接收来自Arduino的管脚(TXD)SoftwareSerial blueToothSerial(RXD,TXD);
布尔光= FALSE;
无效设置(){
  Serial.begin(9600); //允许通过USB线连接到计算机的串行通信(如果需要)
  pinMode(RXD,输入); //设置Arduino的接收来自蓝牙屏蔽输入数字引脚6
  pinMode(TXD,OUTPUT); //设置Arduino的发送数字引脚7数据(OUTPUT)蓝牙盾
  pinMode(13,输出);如果需要,//使用板载LED灯。
}空隙环(){
   延迟(1000);
   如果(光){
      光= FALSE;
      digitalWrite(13,低);
   }
   其他{
     光= TRUE;
     digitalWrite(13,高);
   }
   //Serial.println(blueToothSerial.available());
   blueToothSerial.write(我现在还活着);
   如果(blueToothSerial.read()大于0){
     Serial.write(blueToothSerial.read());
   }
}

核心C#code

 静态无效的主要(字串[] args)
        {
        字节[] =命令Encoding.ASCII.GetBytes(它的工作原理); // {0x00,0x01,0x88};        的SerialPort BlueToothConnection =新的SerialPort();
        BlueToothConnection.BaudRate =(9600);        BlueToothConnection.PortName =COM4;
        BlueToothConnection.Open();
        如果(BlueToothConnection.IsOpen)
        {            BlueToothConnection.ErrorReceived + =新SerialErrorReceivedEventHandler(BlueToothConnection_ErrorReceived);            //声明一个2个字节向量来存储信息的长度头
            字节] MessageLength = {0×00,0×00};            //设置LSB到消息的长度
            MessageLength [0] =(字节)Command.Length;
            //发送2个字节的头
            BlueToothConnection.Write(MessageLength,0,MessageLength.Length);            //发送消息本身            System.Threading.Thread.Sleep(2000);
            BlueToothConnection.Write(指挥,0,Command.Length);            消息(5,); //这是打印,它只是等待响应之前的最后一件事。            INT长度= BlueToothConnection.ReadByte();
            消息(如图6所示,);
            //检索回复数据
            的for(int i = 0; I<长度;我+ +)
            {
                消息(7,(BlueToothConnection.ReadByte()的ToString())。);
            }
        }
    }


解决方案

好了,还有那些难以从上面,所以我会试图解释看到的。几个问题

首先,RX和TX蓝牙卡上不走他们对Arduino的平等。他们去他们的对立面。


  

例如:


  
  

蓝牙卡RX - > TX上的Arduino


  
  

蓝牙卡TX - >上的Arduino RX


因此​​,如果你在阿尔杜伊诺的照片看上述应该痛苦明显,销7和8是不正确的位置。

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

的电源和地似乎是细于上述结构。然而,RX和TX连接需要进行焊接,否则你会得到一个贫穷的连接。

在我做这改变了上述code完美无缺。

希望这有助于任何人有这些问题!

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

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

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.

Arduino Code

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

Core C# Code

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.

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

Example:

Bluetooth Card RX -> TX on Arduino

Bluetooth Card TX -> RX on Arduino

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.

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天全站免登陆