Hi, im trying to interface a uC to a GSM Mobile phone , i wanna read the 1st unread msg from the cell phone , the problem is my codes it waits for the string with \r , the problem is when there is no msg the uC halts at the gets() cuz it needs a string with \r , i dont know how to solve this ..
Code:
puts("AT+CMGL=\"ALL\"\r\n");
// skips 11 strings
// when there is no msg , the uC halts here waiting for any string and it terminates with \r
// i wanna solve this and when there is no msg , it should skip all these gets
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
gets(array,10);
// here is the msg
gets(array,5);
Stop relying on library functions. I have no idea what those functions do. Study the hardware and write your own serial functions and you will be able to make them do whatever you wish.
The problem isn't with the gets function it's with how you're using it.
When there are no messages what is returned from the GSM unit? It has to return SOMETHING. Your code blindly reads strings and tosses them away, this is bad practice. You should compare the first GETS with the message the GSM unit responds with when there are no text messages and break from the loop at that point.
So right after that first GETS function you need to compare that with the 'no messages found' reply, and return from that function then.
ahmed, send two AT command at the same time. The first one to retrieve all the messages and one that will always give a static responce. Perhaps an AT command to display version or retrieve status that always responds with SOME output. Wait for that known responce. If you get that and you haven't retrieved a valid message you know that no message was stored.
Since KeilC51 doesn't implement the equivalent of the M$ C kbhit function, you'll need to poll the UART RX buffer flag with your code to see if any data is available.
If you read about the gets function, you'll see that it calls the _getkey, via calling getchar, which can be modified according to the user guide:
The _getkey function waits for a character to be received from the serial port.
Note
* This routine is implementation-specific and its function may deviate from that described above. Source code is provide in the LIB folder. You may modify the source to customize this function for your particular hardware environment.