hello,
I need help with SIM900 and arduino Mega. Ido not receive the complet message that Isend from my phone.(arduino mega _ sim900 _ Alimentation 9V/2A)
while (Serial2.available()) {
Char += (char)Serial2.read();
}
Reading whatever data has arrived since you last read data, and then pretending that the data you read is a complete packet, and nothing but a complete packet, is NEVER going to work.
PaulS:
Reading whatever data has arrived since you last read data, and then pretending that the data you read is a complete packet, and nothing but a complete packet, is NEVER going to work.
Agreed - but OP should be getting a second line printed (after all the buffer is read only once every second) if the message is incomplete. Most of the time the message will have arrived within that second.
More likely the problem lies with the Serial2 buffer being full, as it's read only once a second, and extra characters getting dropped. There are 12 characters received every time, I don't know the size of the FIFO. Try this:
void loop() {
if (Serial2.available()) {
char c = Serial2.read();
Serial.print(c);
}
}
Maybe you need to use Serial.write() instead, but for a char type it should print the ASCII character.
This code checks the buffer constantly, and prints out characters as they arrive, so you won't have problems with the Serial2 buffer overflowing.
PaulS:
Reading whatever data has arrived since you last read data, and then pretending that the data you read is a complete packet, and nothing but a complete packet, is NEVER going to work.
wvmarle:
Maybe you need to use Serial.write() instead, but for a char type it should print the ASCII character.
This code checks the buffer constantly, and prints out characters as they arrive, so you won't have problems with the Serial2 buffer overflowing.
My goal is to save the complet message in a String so that i may manipulate it later => to get the phone nuber and the message. that's why I think that it's not really helpfull to use Serial.write().
late_id:
Why not since we may put any thing in a string?
Well, yes, that's one of the horrors of String and why it's so easy to abuse.
No matter: you don't check whether your message is complete, so you can not be sure it is. Do understand Serial communication if you want to use it - read the Serial input basics tutorial on how to properly read a complete message.
wvmarle:
No matter: you don't check whether your message is complete, so you can not be sure it is. Do understand Serial communication if you want to use it - read the Serial input basics tutorial on how to properly read a complete message.
Do understand Serial communication if you want to use it - read the Serial input basics tutorial on how to properly read a complete message.
The only problem with that tutorial is that it is not applicable.
That tutorial expects that the user has control over the sender and the receiver.
For some completely unfathomable reason, the AT command set does NOT require any kind of indication that the sender is done sending data. So, the problem with reading a complete text message is that there is no definitive way to know that the complete message has been read.
If only the AT command specification weren't so lame...
PaulS:
If only the AT command specification weren't so lame...
hhhh
Do you have any idea how I may delete the old char in Serial buffer. I tried the use Serial2.flush() after adding the contenent of Serial2 to a Sting .. but it doesn't seem to work
if (Serial2.available()) {
s += (char) Serial2.read();
Serial2.flush();
}
I tried the use Serial2.flush() after adding the contenent of Serial2 to a Sting .. but it doesn't seem to work
It certainly does. You just need to understand that flushing is like making the sh*t in a toilet go away. The OUTGOING plumbing gets a workout. The INCOMING plumbing just supplies water to refill the toilet. flush() affects the outgoing serial buffer, not the incoming buffer.
The ONLY way to empty the incoming buffer is to read all the data. Why you would want to read and dispose of incoming data without concern as to what that data is is a mystery, though.
Surely many parts are applicable still. Such as reading data and validating it, and using start/end tags in data so you can tell a message is complete.
A major part of Serial communication is trying to read characters as soon as possible, as they come in, and store them in a local buffer (that's big enough to hold the largest message - plus some logic to not overflow your buffer) until the message is complete, after which you can start processing it.
wvmarle:
Probably because you don't read it properly... buffer full or so? (see also previous messages)
I do not think that the problem is in Serial buffer cause when I try to read the message without saving it in a variable it shows the complete message and not just a part of it.
Key difference is the lack of that delay(1000). That's most likely the problem as I said already: characters getting lost as they don't fit in the buffer due to you reading it very infrequently.
Now where you write the received character to the console, that's the place where you should store it into your string. And no delay() call needed. Use some kind of end tag, a special character or so, maybe simply a \n or \r. There's got to be something for you to identify a message is complete.
char buffer[51]; // As small as possible, but big enough to hold a complete message plus null terminator
byte counter = 0;
void loop() {
if (Serial2.available()) {
char c = Serial2.read();
buffer[counter] = c;
if (c == yourEndTag) {
// string complete
buffer[counter + 1] = '\0'; // null terminate the string.
stringComplete = true;
}
counter++;
if (counter == 50) {
// Buffer full!
}
}
if (stringComplete) {
// Deal with it!
// When string is processed get ready for a new message:
stringComplete = false;
counter = 0;
}
}
And do note that I used string (c-string, char array) rather than String.
That's a lot more than you posted before.
So you basically see two lines here.
Start of the message is the '+' followed by the phone number and a timestamp.
End of the first line is probably a '\n', after which you expect a '@'.
Then the message itself, terminated with another '@'.
OK, now you know what to look for.
To really see all characters and know what they are, change the loop() in your code to the following:
void loop() {
if (Serial2.available()) {
char c = Serial2.read();
Serial.print((uint8_t) c);
Serial.print(" ");
Serial.println(c);
}
}
This prints one line per character: the ASCII code and the character itself. This way you can also see the unprintable characters such as \n and \r and maybe others, and then you really know exactly what the message looks like, so you can start parsing it properly.