Bluetooth communication with Arduino‏ (C#)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Bluetooth communication with Arduino‏ (C#)

Omar Morando
This post has NOT been accepted by the mailing list yet.
Hello,

I'm dev a MonoDroid app that receives data from an Arduino module via bluetooth. I'm able to establish communication with Arduino and receive data using "await Stream.ReadAsync". My problem, however, is the correct reading of the data, I'll explain:

- Arduino continuously sends a data packet with this structure "X: xx.x, Y: yy.y, Z: zz.z;\r\n" (where xx.x, yy.y and zz.z are the coordinates read from a gyroscope)

- Android app reads the information with the function ReadAsync but the problem is that I can not synchronize the reading to get a full string (sometimes read from mid-on, other times the entire string plus a piece of a new...)

How can I get a clean string which can then parse? Something like SerialPort.ReadLine () on the OnDataReceiver method on .NET PC...

Here a piece of code:

private async void ReadAsyncStream () {
        while (true) {
                int nbyte = await inBTStream.ReadAsync(buffer, 0, buffer.Length);
                ParseArduinoString (ByteToString (buffer, buffer.Length));
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: Bluetooth communication with Arduino‏ (C#)

asciiman
This post has NOT been accepted by the mailing list yet.
hi i am using netduino and write this code private static System.Collections.ArrayList _SerialInBuffer; private static void xSerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { var xSerialPort = sender as System.IO.Ports.SerialPort; byte[] xReceivedBytes = new byte[xSerialPort.BytesToRead]; xSerialPort.Read(xReceivedBytes, 0, xReceivedBytes.Length); foreach (byte xReceivedByte in xReceivedBytes) if (xReceivedByte == STX) { _SerialInBuffer.Clear(); _SerialInBuffer.Add(xReceivedByte); } else if (xReceivedByte != EOT) { _SerialInBuffer.Add(xReceivedByte); } else // EOT { byte[] tmpBytes = new byte[_SerialInBuffer.Count]; _SerialInBuffer.CopyTo(tmpBytes); _SerialInBuffer.Clear(); string xSerialCommand = new string(System.Text.Encoding.UTF8.GetChars(tmpBytes)) + EOT; // command complated you can use xSerialCommand it include stx and eot character } } STX is start of text transmission character, EOT is end of transmission character. ///  /// Start of Text Transmission (STX) character /// Hexadecimal: 0x02 ~ Decimal: 2 /// public const char STX = '\u0002'; ///  /// End of Transmission (EOT) character /// Hexadecimal: 0x04 ~ Decimal: 4 /// public const char EOT = '\u0004'; And you must send data like this. string xTransmitData = STX + "this text is content" + EOT ; byte[] xBytes = System.Text.Encoding.UTF8.GetBytes(xTransmitData); _SerialPort.Write(xBytes, 0, xBytes.Length);
Reply | Threaded
Open this post in threaded view
|

Re: Bluetooth communication with Arduino‏ (C#)

asciiman
This post has NOT been accepted by the mailing list yet.
hi this post is not good

sen me mail
asciiman_@msn.com