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

Having trouble with Bluetooth (SerialPort service)

Bluetooth SerialPort

  • Please log in to reply
4 replies to this topic

#1 shiv.kumar

shiv.kumar

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 20 May 2014 - 11:40 AM

I'm using Netduino Plus 2 (Firmware 4.3.1)

I'm using the JY-MCU Bluetooth board (v1.06)

 

I've tried using COM1 with Pins 0-1 as well as COM2 with Pins 2-3, same results

 

When using COM2

bluetoothSerialPort = new SerialPort(SerialPorts.COM2);
bluetoothSerialPort.DataReceived += rec_DataReceived;
bluetoothSerialPort.Open();

I have the TXD pin of JY-MCU connected to Pin 2 and the RXD pin of JY-MCU connected to Pin 3

 

I can pair with the device using an app on my phone and/or desktop

I can send data to the device using both as well

The number of bytes received is the same as the number of bytes sent

 

I have two of these boards and I get the same results with both.

 

The problem is that I don't receive the same bytes I send. I do however always receive the same (wrong bytes) for the same sent bytes.

 

For example, if I send "Hello|"

I'll receive (consistently)

0
32
32
56
224
254
 
If I send "abcde|"
I'll receive
64
8
0
0
224
254
 
I get these results if I power the board with 5v. If I power it with 3.3v I get different results and fewer bytes
 
I'm hoping someone can help me figure out what is going on. Or if someone has this combination working they could share what they're doing.


#2 Jack Chidley

Jack Chidley

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 20 May 2014 - 01:48 PM

I have a similar set up to you. It works with both 5V and 3.3V. I've sent data to and from Windows 8.1 and Windows Phone 8.1 without errors. However correct message framing is important - this might be your problem, or the encoding or the "endiness". I'm using windows throughout.

Some notes:
Connect to the Bluetooth device "HC-06" using pin 1234 (either on my phone or my computer).

On the Netduino
Parameters are "COM1", 9600, Parity.None, 8, StopBits.One
D0 to TXD
D1 to RXD

and pretty much what you have above.

This code

https://github.com/j.../SerialMessages

is messy but working. I trust you can read it. My code (currently) works on delimiters.

#3 shiv.kumar

shiv.kumar

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 20 May 2014 - 03:42 PM

Hey Jack,

 

Thanks for your reply and code. I'll go through it and see if I can get it to work for me and if something pop out at me in my code.

 

I don't understand this part in your message

 

 

Connect to the Bluetooth device "HC-06" using pin 1234 (either on my phone or my computer).

 

The "using pin 1234" is where I'm foxed.



#4 Jack Chidley

Jack Chidley

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 20 May 2014 - 05:46 PM

I'm sure you've already paired it.

But once the JY-MCU is powered and opened on the Netduino (bluetoothSerialPort.Open() ;) then you need to pair it with your phone/PC by selecting HC-06 and entering the pin 1234. Then the red status light on the JY-MCU stops flashing and changes to a solid red colour.

Honestly, it would help if you pasted more of your code so that we could see what is happening.

#5 shiv.kumar

shiv.kumar

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 20 May 2014 - 08:07 PM

Ok, "pin" was where I got confused where I thought the physical pin on the Netduino. Yes, I'm paird of course since I do connect and send data :)

 

Here is the code as it stands currently.

    public class Program
    {
        static SerialPort bluetoothSerialPort;
        static byte[] buffer = new byte[1024];
        static int bufferOffset = 0;

        public static void Main()
        {

            bluetoothSerialPort = new SerialPort(SerialPorts.COM1);
            bluetoothSerialPort.DataReceived += DataReceived;
            bluetoothSerialPort.Open();

            Thread.Sleep(Timeout.Infinite);
        }

        private static void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var bytesRead = 0;
            while (bluetoothSerialPort.BytesToRead > 0)
            {
                bytesRead = bluetoothSerialPort.Read(buffer, bufferOffset, buffer.Length - bufferOffset);
                bufferOffset += bytesRead;
            }

            bufferOffset = 0;
            var chars = Encoding.UTF8.GetChars(buffer, 0, bytesRead);
            var data = new string(chars);
        }

        private static void ExecuteCommand(string command)
        {
            throw new NotImplementedException();
        }
    }







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.