For my personal project, I am trying to communicate between my PC and Netduino, through Bluetooth. Hence I have attached BlueSMiRF Gold to Netduino, and a USB bluetooth dongle which I found laying around in my house, and which I attached to my PC.
When I came to implement the code, I realized that I have no understanding of how this works. I wish you to have some patience and explain to me what I am doing wrong and what I should be doing.
For my bluetooth testing this is what I am doing:
I have deployed this into my netduino:
public class Program { static SerialPort serial; public static void Main() { // initialize the serial port for COM1 (using D0 & D1) serial = new SerialPort(SerialPorts.COM1, 115200, Parity.None, 8, StopBits.One); // open the serial-port, so we can send & receive data serial.Open(); // add an event-handler for handling incoming data serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); // wait forever... Thread.Sleep(Timeout.Infinite); } static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { // create a single byte array byte[] bytes = new byte[32]; // as long as there is data waiting to be read while (serial.BytesToRead > 0) { // read a single byte serial.Read(bytes, 0, bytes.Length); // send the same byte back serial.Write(bytes, 0, bytes.Length); } } }
With this code from above, I am expecting that any data received from the BlueSMiRF is returned back.
I have opened a Windows Application to send data from PC to Netduino through Bluetooth, and this application also receives the data back. This is the code:
public partial class Form1 : Form { public SerialPort serial; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // initialize the serial port for COM1 (using D0 & D1) serial = new SerialPort("COM6", 115200, Parity.None, 8, StopBits.One); // open the serial-port, so we can send & receive data serial.Open(); serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); } void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { // create a single byte array byte[] bytes = new byte[32]; // as long as there is data waiting to be read while (serial.BytesToRead > 0) { // read a single byte serial.Read(bytes, 0, bytes.Length); lblRecievedData.Text = bytes.ToString(); } } private void btnSend_Click(object sender, EventArgs e) { System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] buffer = encoding.GetBytes(txtSendData.Text); serial.Write(buffer, 0, buffer.Length); } }
In a summary, this code lets the user fill a Textbox and click Send, to send data to Netduino. In return, Netduino should send the data back and this should be printed in the Label Control.
When I run the above code, the 'Connect' LED on the BlueSMiRF light up Green, so I am guessing that the connection is set up right. Data seems to be sent fine, since on debug, the byte[] buffer is filled appropriately. But the DataReceived Event is never accessed.
Please help me out on this. I searched on the internet, but I only found various ways to code Netduino to Send and Receive data through bluetooth. But not what to code to send data to Netduino from PC. Moreover, I am new to Netduino and hardware coding and I am feeling totally lost now.