daozui
April 28, 2017, 3:21pm
1
int count=0;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial3.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{
Serial.println();
Serial.print("Number of Bytes:");
Serial.println(Serial.available());
while(Serial.available())
{
Serial.read();
}
Serial1.write("+CMTI: ""SM"",4");
Serial1.flush();
count++;
Serial.print("Count:");
Serial.println(count);
delay(100);
}
if(Serial3.available()>0)
{
while(Serial3.available())
{
Serial.write(Serial3.read());
}
Serial.println();
}
}
Hi.. Can anyone explain why after running the loop below, there are still some data in the Serial port.
I want loop inside the while loop for once.
while(Serial.available())
{
Serial.read();
}
Serial1.write("+CMTI: ""SM"",4");
Serial1.flush();
pylon
April 28, 2017, 4:13pm
2
Can anyone explain why after running the loop below, there are still some data in the Serial port.
Probably there's not still data in the buffer but again . The write on the Serial1 object need some time (more than 10 Milliseconds) and during that time some data may have arrived.
Robin2
April 28, 2017, 5:03pm
3
daozui:
Hi.. Can anyone explain why after running the loop below, there are still some data in the Serial port.
I want loop inside the while loop for once.
I'm not sure what you are trying to achieve but I suspect the problem is that the
while(Serial.available())
empties the serial input buffer long before the full message has time to arrive. The Arduino is very much faster than serial data - even at high baud rates.
Have a look at Serial Input Basics
...R
daozui
April 29, 2017, 7:10am
4
Thanks.. Putting a delay before goes into the while loop solved the problem.
daozui:
Putting a delay before goes into the while loop solved the problem.
No, it only hides the problem.
Serial1.write("+CMTI: ""SM"",4");
You know that the resulting string (+CMTI: SM,4) will not contain any " ?
daozui
April 29, 2017, 10:38am
7
Whandall:
No, it only hides the problem.
Serial1.write("+CMTI: ""SM"",4");
You know that the resulting string (+CMTI: SM,4) will not contain any " ?
You meant no " would be written into the serial1?
void setup() {
Serial.begin(250000);
Serial.print(F("Your code produces \""));
Serial.write("+CMTI: ""SM"",4");
Serial.print(F("\" but you probably wanted \""));
Serial.write("+CMTI: \"SM\",4");
Serial.println(F("\""));
}
void loop() {}
Your code produces "+CMTI: SM,4" but you probably wanted "+CMTI: "SM",4"
daozui
April 30, 2017, 7:46am
9
Whandall:
void setup() {
Serial.begin(250000);
Serial.print(F("Your code produces ""));
Serial.write("+CMTI: ""SM"",4");
Serial.print(F("" but you probably wanted ""));
Serial.write("+CMTI: "SM",4");
Serial.println(F("""));
}
void loop() {}
Your code produces "+CMTI: SM,4" but you probably wanted "+CMTI: "SM",4"
I got what you meant now. I really have trouble on that. Thanks for pointing it out.