Pages: [1] 2 3 ... 5   Go Down
Author Topic: Problem in receiving the data from Bluetooth module  (Read 4498 times)
0 Members and 1 Guest are viewing this topic.
Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using  a Bluelink Bluetooth module to detect for devices and return the MAC addresses. This the code I've written.
Code:
char buffer[1000];
int i;
int j;
void setup()
{
  Serial.begin(9600);
  Serial.println("Initializing");

}
void readprint()
{
  while (Serial.available()>0)
  {
    delay(10);
    for(i=0;i<1000;i++)
   {
      buffer[i]=Serial.read();
   }
   for(j=0;j<1000;j++)
   {if(buffer[j]>0)
     Serial.print(buffer[j]);
   } 
  Serial.print("\n");
}
}
void ReadAtinq()
{
  while (Serial.available())
  {
    delay(2000);
    for(i=0;i<1000;i++)
   {
      buffer[i]=Serial.read();
   }
  }
}
 void PrintAtinq()
{
   for(j=0;j<1000;j++)
   {if(buffer[j]>0)
     Serial.print(buffer[j]);
   } 
  Serial.print("\n");
}

void loop()
{Serial.write("===");
 readprint();
 delay(500);
 Serial.write("LLL");
 readprint();
 delay(500);
 Serial.write("\r\nAT+INQ\r\n");
 ReadAtinq();
 delay(25000);
 PrintAtinq();
 delay(5000);
 
  }

The output for the AT+INQ command should have the device name and MAC address...If only one device is present I have no problem in getting the output. If there are say two devices, the address and name of the 1st devices gets printed but the address of the second one gets cut off in the middle. It looks as though there is a limit in the number of characters it would print.

Kindly help me solve this problem. TIA
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  while (Serial.available()>0)
  {
    delay(10);
    for(i=0;i<1000;i++)
   {
      buffer[i]=Serial.read();
   }
If there is at least 1 byte available to read, wait 10 milliseconds, then read all 1000 of them.

No. Reading serial data doesn't work that way.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So if I just program it like whenever data is available.
Code:
Serial.available()

Or can you please tell me, what way I should do these..??
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Typically, you need to figure out what constitutes a packet, and write code to buffer incoming data until the end of packet marker arrives.

So, you would use:
Code:
while(Serial.available() > 0)
{
  // read and buffer any non-end-of-packet marker characters
  // if a character read IS an end of packet marker, set a flag and break out of the while loop
}

// If the end-of-packet marker arrived flag is set, the buffer contains usable data.

You would need to just print any available data (in hex, perhaps) to see what, if anything, is used as the end of packet marker, or to see if there is a packet size value sent.

Reading random serial data is not a trivial task.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have also used the altered program that USES SOP and EOP. But my problem still exists...!!!

Code:
#define SOP '\n'
#define EOP '\r'

boolean started = false;
boolean ended = false;

char inData[200];
byte index;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}
void readprocess()
{
   // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
   
  }
}
void printprocess()
{
  for (int i=0;i<200;i++)
  {
    if(inData[i]>0)
     Serial.println(inData[i]);
   } 
  Serial.print("\n");
}

void readatinq()
{
   // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
   
  }
}
void printatinq()
{
  for (int i=0;i<200;i++)
  {
    if(inData[i]>0)
     Serial.println(inData[i]);
   } 
  Serial.print("\n");
}
void loop()
{
  Serial.write("===");
 readprocess();
 printprocess();
 delay(500);
 Serial.write("LLL");
 readprocess();
 printprocess();
 delay(500);
  Serial.write("===");
 readprocess();
 printprocess();
 delay(500);
 Serial.println("2nd time");
 Serial.write("LLL");
 readprocess();
 printprocess();
 delay(500);
  Serial.write("\r\nAT+INQ\r\n");
 readatinq();
 delay(25000);
 printatinq();
 delay(5000);
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But my problem still exists...!!!
You get some serial output, but you don't show it to us. Yet, you'd like us to debug your program for you.

It doesn't work that way.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You get some serial output, but you don't show it to us. Yet, you'd like us to debug your program for you.

It doesn't work that way.

My Bad! I'm Sorry!
The Output that I expect to get is this:-
Code:
===
LLL
OK

===
OK
LLL
OK
AT+INQ
OK

+INQ:00:1C:A4:14:7D:AA,"Sony Ericsson"
D4:C1:FC:3A:56:7C,"C1-01"
DONE

OK

For my first program I got this output
Code:
Initializing
===LLL
ERROR

OK


AT+INQ

OK

OK

===
+INQ:D4C1:FC:3A:56:7C,”C1-01”
00:1C:A4:14:7D:AA,”Sony Erics

For the latest program which I altered using your SOP and EOP concept I got a more weird output .
Code:
===
LLL
===O
K

2nd time
LLLK

AT+INQ
O
K
===K

LLLO
K

===K

2ND TIME
LLLO
K

LLLK

===+
I
N
Q
:
0
0
:
1
C

S
O
N
Y

E
R
I
C
S
S
O
N


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.write("===");
I'd suggest a refresher re-read of the Serial page would be in order. The Serial class has print() and write() methods that have different purposes. One is for ASCII data output and one is for binary data output. You want to be using Serial.print() (or, seeing your expected output, Serial.println()) here, instead of Serial.write().

The code I posted, that you adapted, expects to be called in loop. It expects that you will not try to print the data in the array until the end of packet marker has arrived. The modifications you've made no longer buffer (and ignore) data until the end of packet marker arrives.

You should revise loop to not call printprocess() unless ended is true. Of course, you will have a problem even then, since readprocess() clears the ended flag when the end of packet marker has been received, because it (still) expects to process the packet and get ready for the next one.

I'm not sure how you want to go about fixing the issues. If it were me, I'd remove the code from readprocess() that clears the started and ended flags and resets the index and array, and revise loop() to only call printprocess() is started and ended are true. In that if() block, I'd put the stuff I removed from readprocess().
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry, my expected output in my previous post has both my inputs as well. Forgot to differentiate while posting. So the expected output is this.
===
LLL
OK

===
OK
LLL
OK
AT+INQ
OK

+INQ:00:1C:A4:14:7D:AA,"Sony Ericsson"
D4:C1:FC:3A:56:7C,"C1-01"
DONE

OK

 The red ones are the Input commands that I send to the Bluetooth module.So I have to use Serial.write in this case, as I need to send the commands to my module. Please correct me if I'm wrong.
I'm am not well versed with coding, so kindly bear with my doubts.
Code:
while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
In this it reads the SOP and when it is done it deletes that from the receive buffer right?
Code:
inData[index] = '\0';
This is the one that deletes the stuff from receive buffer right?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In this it reads the SOP and when it is done it deletes that from the receive buffer right?
Everything that is read from the serial buffer is removed from the buffer. If you mean that SOP is not placed in the inData array, that is correct.

Quote
This is the one that deletes the stuff from receive buffer right?
Yes.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changes the SOP and EOP as per my requirement and the code that i used is this
Code:
#define SOP '\r\n'
#define EOP '\r\n'
#include <avr/wdt.h>
int count=0;
int i;

bool started = false;
bool ended = false;

char inData[200];
byte index;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}
void readprocess()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        Serial.print(inData[index]);
        index++;
       
      }
 
  }
   
}
   Serial.println('\n'); 

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
   }
}

void loop()
{
 
  Serial.write("===");
  delay(100);
  readprocess();
  delay(2000);
  Serial.write("LLL");
  delay(100);
  readprocess();
   delay(2000);
  Serial.write("\r\nAT+INQ\r\n");
  delay(1000);
  readprocess();
  delay(30000);
  readprocess();
  delay(2000);
  wdt_enable(WDTO_15MS);
  wdt_reset();
}
I get this output
===
OK

LLL
OK

AT+INQ

OK
+INQ:D4C1:FC:3A:56:7C,”C1-01”
00:1C:A4:14:7D:AA,”Sony Erics

if it can print from the buffer all the data detected as a result of the AT+INQ command, I can't understand why it is not able to print beyond the "ERICS" part
 
Before trying out the above program, i tried the following..
I removed the code that removes data from the buffer.
Code:
inData[index] = '\0';
  and then,I tried printing the inData array elements after detecting the EOP char in this way,
Code:
if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
     for(i=0;i<200;i++)
     {
      if(inData[i]>0;)
Serial.print(inData[i]);
}
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
   }


But for this code I dont get any output for my AT+INQ command  (Maybe my modification to the code is wrong)

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define SOP '\r\n'
Which SINGLE character is that?
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The format of my result codes are
<cr><lf>RESULT CODE<cr> <lf>

and for the devices it is
<cr><lf>device 1<cr> <lf>device2<cr> <lf>

I tried all the combinations for (SOP, EOP) like (\r , \r), (\r,\n), (\n,\n), (\n,\r) but I received an "ERROR" code after AT+INQ.
===
OK

LLL
OK

AT+INQ
ERROR
OK
ERROR
+INQ:D4C1:FC:3A:56:7C,”C1-01”
00:1C:A4:14:7D:AA,”Sony Erics


So i just tried \r\n though even I hesitated to include two characters instead of one. But as I did not receive any compilation error and as the "ERROR" in the output got rid off, i did not think much about it.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is not syntactically invalid to define something unrealistic. But there is absolutely no way for the Arduino to receive a multi-byte constant over the serial port, so nothing it ever receives will be equal to that multi-byte constant.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But there is absolutely no way for the Arduino to receive a multi-byte constant over the serial port, so nothing it ever receives will be equal to that multi-byte constant.

OK. In that case what characters could be assigned as the SOP and EOP in the following format of the result code?
 <cr><lf>device 1<cr> <lf>device2<cr> <lf>

E.g
\r\n +INQ:00:12:39:00:34:35,”RLMobile”,00:13:45:46:99:23,”Jeny”, 00:12:39:00:34:35,”RLMobile”,00:13:45:46:99:23,”Jerry”\r\n
« Last Edit: March 03, 2012, 12:01:09 am by elan_arr » Logged

Pages: [1] 2 3 ... 5   Go Up
Jump to: