Pages: [1]   Go Down
Author Topic: Help with serial port Bluetooth module  (Read 1267 times)
0 Members and 1 Guest are viewing this topic.
Sao Paulo, Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi folks,

I have bluetooth module attached to arduino and connected (via bluetooth) to my android phone.

But the messages are intermittent splitted.

Arduino:
Code:
void loop(){
   Serial1.write("123456");
   delay(300);
}

Android log:
Code:
Received (6 bytes): 123456
Received (1 bytes): 1
Received (5 bytes): 23456
Received (1 bytes): 1
Received (5 bytes): 23456
and so on...

It's normal?
I should do a packaging method or something else? Maybe set messages with start and end mark..?

Maybe is a dumb question.. sorry, I'm a beginner.

thanks in advance!
Logged

Emerson Moretto

France
Offline Offline
God Member
*****
Karma: 4
Posts: 978
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
On Android side did you test it with some proven terminal like BlueTerm ot SenaBterm ?
Is your Android version known for good serial port profile handling ?
If you stay under 57600 bauds no reason loose bytes in such short messages if Android appication is good.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.write() is meant for sending individual bytes, not strings of characters. With nothing to indicate what constitutes a packet within a stream of data, the receiver is just guessing.

In this case, the Android application guessed wrong.
Logged

Sao Paulo, Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS and al1fch.
After a 1 minute thinking.. I've found a solution.

I have a thread to listen bluetooth stream... but this thread don't have sleep time. It's so fast to listen arduino.  smiley-red

Maybe isn't a perfect solution, because still data are broken, however much less (~10%)

I'm using 9600 baud rate. My Android 2.2.
I've tested with BlueTerm too with same results.

Soon I will post here the source code.

thanks

Logged

Emerson Moretto

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Adding a sleep to the listener is a bandaid, and a poor one at that. Notice that when we communicate we use punctuation to tell when a sentence (a packet) ends and spaces to tell when a word ends (another kind of packet.

WhatissohardaboutdoingthesamethinginyourserialcommunicationItcompletelyeliminatestheconfusionorambiguityaboutwhatconstitutesapacket.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26621
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial1.write("123456");
I'm really surprised that works at all - I thought "write" took a single "byte" argument.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From Print.h:
Code:
    virtual void write(uint8_t) = 0;
    virtual void write(const char *str);
    virtual void write(const uint8_t *buffer, size_t size);
So, yes, there is an overload that takes a string.
Logged

Pages: [1]   Go Up
Jump to: