Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Bluetooth Communication


  • Please log in to reply
1 reply to this topic

#1 xikky

xikky

    Member

  • Members
  • PipPip
  • 20 posts

Posted 21 March 2012 - 02:45 PM

Hello Netduino Community!

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.

#2 Bainesbunch

Bainesbunch

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts
  • LocationFrance

Posted 21 March 2012 - 08:40 PM

Hello,

I am using a BT serial adapter connected to my Netduino and talking to my PC through a BT dongle.

Pairing was my first issue. Once i hade the BT to serial paired with my PC then I simply connected the TX and RX lines of the BT to Serial together to make a loopback. I then used a terminal emulator program (http://www.compuphas...are_termite.htm) on my PC to send characters through the serial BT to see if they were echoed back.

Once this was working I then set about writing the code to receive the data on the Netduino and echo it back.

It all worked as expected once I had gone through these steps. One gotchya however it the TX/RX they seem to differ from one manufacturer to another. So it might be worth switching them around if you are not receiving any data at the Netduino end.

Sometimes it is good to set some debug.prints to show what is coming in on the port.

I did notice however that you are trying to read 32 bytes into a buffer. The read will not return until it has read 32 bytes or timed out. Try setting the buffer length to 1 byte. This will echo every byte one at a time and not wait for the buffer of 32 bytes to be read.


Hope this helps ... and good luck.

Cheers Pete.
I was going to change the world, then I discovered Netduino.
The world will have to wait.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.