C # console application talking to Arduino via Bluetooth

There is not much to say except this, it does not work, and I have no idea why.

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

The LED of the Bluetooth card on the Arduino lights up green when I run the C # program, so the connection is made. Just nothing.

Enter image description hereEnter image description here

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

C # kernel 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())); } } } 
+1
source share
2 answers

Ok, so there were a few problems that are hard to see from above, so I will try to explain.

First of all, RX and TX on a Bluetooth card do not equal them on an arduino. They go to their opposites.

Example:

Bluetooth RX → TX on Arduino

Bluetooth TX → RX on Arduino

So, if you look at the Arduino photo above, it should be very obvious that pins 7 and 8 are the wrong location.

The next question I had was a poor connection.

In the above configuration, power and earth seem beautiful. However, you must connect the RX and TX connections. Otherwise, you will get a bad connection.

As soon as I did this, the above code worked flawlessly.

Hope this helps someone else to have these problems!

+1
source

Here, a similar code is simplified for testing PC-to-PC communication - Arduino BT. In the code below, the PC sends text to the Arduino, which then echoes it to the PC. The PC writes the incoming text to the console.

Install your BT serial port (COM5 in the example) from Windows .

Tested with Arduino Uno, USB-Bluetooth dongle and bluetooth module HC-05 . The Arduino PIN1 (TX) voltage was separated from 5 V to 2.5 V before applying it to the output of the RX module).

What is it. Easy!

Arduino:

 void setup() { Serial.begin(9600); delay(50); } void loop() { if (Serial.available()) Serial.write(Serial.read()); // echo everything } 

WITH#:

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

Source: https://habr.com/ru/post/952275/


All Articles