Problem in receiving the data from Bluetooth module

I'm using a Bluelink Bluetooth module to detect for devices and return the MAC addresses. This the code I've written.

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

  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.

So if I just program it like whenever data is available.

Serial.available()

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

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:

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.

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

#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);
  }

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.

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:-

===
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

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 .

===
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
“
  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().

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.

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?
inData[index] = '\0'; This is the one that deletes the stuff from receive buffer right?

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.

This is the one that deletes the stuff from receive buffer right?

Yes.

I changes the SOP and EOP as per my requirement and the code that i used is this

#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

[color=red]===[/color]
OK

[color=red]LLL[/color]
OK

[color=red]AT+INQ[/color]

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.
inData[index] = '\0'; and then,I tried printing the inData array elements after detecting the EOP char in this way,

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)

#define SOP '\r\n'

Which SINGLE character is that?

The format of my result codes are
RESULT CODE

and for the devices it is
device 1 device2

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.

[color=red]===[/color]
OK

[color=red]LLL[/color]
OK

AT+INQ
[color=orange]ERROR[/color]
OK
[color=orange]ERROR[/color]
+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.

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.

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?
device 1 device2

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

I see some repeating data in that stream of data:
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”

I see that between each repeating pattern of data that there is a delimiter, the comma. That will make parsing easy.

I see that there is non-repeating data:
\r\n +INQ: (at the start)
\r\n (at the end)

So, what I would see as the start of the packet is the + (SOP), and the \r is the end of the packet (EOP). After the start of the packet, then, there is a token that can be ignored (INQ:) and after the end of the packet, before the start of the next, there is some other stuff that can be ignored (\n, \r, \n) (assuming that the next packet looks just like this one).

void readatinq()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP1)
    {  Serial.println("SOP detected");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP1)
    { Serial.println("EOP1 detected");
      count=count+1;
      if(count>=3)
      {
       Serial.print("EOP2 detected");
       ended = true;
       break;
    }}
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        Serial.print(inData[index]);
        index++;
        inData[index] = '\0';
      }
    }
  }

  if(started && ended)
  { 
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
   }
}

here SOP is + and the EOP i used is comma ,
I did not use \r because the code begins with a \r the code is not processed at all. Since there are more than one commas I used a variable "count" to and entered a code to exit the loop when "count" value is greater than 3. I've used the "EOP1 detected" and "EOP2 detected" to find out which part of the code is being executed. Even now my output is

[color=red]===[/color]
OK
[color=red]LLL[/color]
OK
[color=red]AT+INQ[/color]
OK
SOP detected
INQ:00:12:39:00:34:35 EOP1 DETECTED
”RLMobile”
00:13:45:46:99:23 EOP1 DETECTED
”Je

2nd iteration continues

I still don't understand why I am not able to receive the complete output! Please help

I still don't understand why I am not able to receive the complete output

The key is right here:

here SOP is + and the EOP i used is comma ,

So, the code starts storing the message when the + arrives, which is good, and stops when the comma arrives. Since the comma is NOT the end of packet, storing terminates prematurely.

I did not use \r because the code begins with a \r the code is not processed at all.

This doesn't make sense. The stream of data may well contain a \r before the +, but that doesn't matter. Nothing happens with the bytes in the stream until the + arrives. 47 \r could arrive before the + does, and they would all be ignored.

Like my advice. You can continue to ignore it, or not. Your choice.

Oh no. I never meant to ignore your inputs sir. I did try out + as the SOP and \r as the EOP. I still did not get an output. I'll post the entire code here, I guess i made a mistake in the read and print process for I did not quite understand your instructions on them, a few posts back. Kindly check the code for me.

#define SOP '+'
#define EOP '\r'
#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++;
        inData[index] = '\0';
      }
    }
  }

   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 readatinq()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       Serial.print("SOP detected");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {  Serial.print("EOP detected");
       ended = true;
       break;
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        Serial.print(inData[index]);
        index++;
        inData[index] = '\0';
      }
    }
  }

   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);
  readatinq();
  delay(2000);
  wdt_enable(WDTO_15MS);
  wdt_reset();
}

The output I got is this

[color=red]===[/color]
OK
[color=red]LLL[/color]
OK
[color=red]AT+INQ[/color]
OK
EOP DETECTED

2nd iteration continues

Even now my output is

===
OK
LLL
OK
AT+INQ
OK
SOP detected
INQ:00:12:39:00:34:35 EOP1 DETECTED
”RLMobile”
00:13:45:46:99:23 EOP1 DETECTED
”Je

was what you were getting, when SOP was + and EOP was ,.

You said that you changed just the EOP value, and now all you get is this:

The output I got is this

OK
LLL
OK
AT+INQ
OK
EOP DETECTED

I find it a little difficult to believe that you changed just one character, unless something happened to your phone.