Output cut off

Hello,

I do not understand how the output works. I modified a Bluetooth sample program to work with my HM-19 Bluetooth module:

#include <SoftwareSerial.h>
#define rxPin 10 // yellow
#define txPin 11 // green

SoftwareSerial BTSerial(rxPin, txPin); // RX, TX

void setup() {

  Serial.begin(9600);
  BTSerial.begin(9600);
  while(!Serial);

  BTSerial.println("AT+DISI?");

}

void loop() {

  // read from HM-19 and print in the Serial
  if(BTSerial.available())
  Serial.write(BTSerial.read());

  //read from the Serial and print to the HM-19
  if(Serial.available())
  BTSerial.write(Serial.read());
}

The output is:

OK+DISCS
OK+DIS1:00000000:00000000000000000000000000000000:0000000000:F33321aabbcc:-059
OK+DIS1:00000000:00000000000000000000000000000000:0000000000:F333B2aabbcc:-057
OK+DIS1:00000000:00000000000000000000000000000000:0000000000:6BA0C7aabbcc:-037
OK+DISCE

Now I replace BTSerial.println("AT+DISI?");

with

BTSerial.println("AT+DISI?");
delay(1000);
BTSerial.println("AT+DISI?");

and all I get is this:

OK+DISCS
OK+DIS1:00000000:00000000000000000000000000000000:000OK+DISCE

What did I do wrong that I got this cutoff output instead of getting the pre-modification output twice?

My first guess would be that you are running out of input buffer. Instead of starting to empty the buffer almost immediately you are waiting for a full second and then asking for more input before even starting to pull characters from the buffer.

johnwasser:
My first guess would be that you are running out of input buffer. Instead of starting to empty the buffer almost immediately you are waiting for a full second and then asking for more input before even starting to pull characters from the buffer.

Something like that was my first guess, too. But the other way round: It was the reason for me to add the delay, because

  Serial.begin(9600);
  BTSerial.begin(9600);
  while(!Serial);
  Serial.println("1");
  BTSerial.println("AT+DISI?");
  Serial.println("2");
  BTSerial.println("AT+DISI?");

only gives me

1
2

I added the 1, 2 to see if it's working at all.

My guess is that the module receiving the second command before the first command has completed was causing both to fail.
You should probably only send a command when you are immediately ready to receive the response and not send another command until the first response is complete.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

johnwasser:
My guess is that the module receiving the second command before the first command has completed was causing both to fail.
You should probably only send a command when you are immediately ready to receive the response and not send another command until the first response is complete.

My guess exactly. That's why I put the delay in there.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

I'll have look, thank you.