Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Programming problem

Status
Not open for further replies.
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.

Mike.
 
Hi nigel, how about maing a timer , start it before gets and check if time > 2 sec ??
also when i write my own i will wait for input ...
 
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.
 
Last edited:
It would also help if ahmedragia21 told us which uC and which compiler he was using so we could suggest another function to use. :rolleyes:
 
@Sceadwian:
The problem is the Cell phone doesnt give an indication if there's no msg in the box , no AT command for that .

@kchriste : sorry i forgot , i use KeilC51 , with 89S52 .
 
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.
 
Sceadwian, good idea ! but still we didnt solve the main problem how i will extract the retrived msg without GETS ??

AT+CMGL="ALL" <-- list all msgs
// gets the retrived msg
AT // do AT
 
Last edited:
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.
So you could add a time out feature to _getkey, so it returns a \r\n which will make gets exit, then it won't hang beyond a certain time period.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top