# Programming problem

Status
Not open for further replies.

#### ahmedragia21

##### Member
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);

#### Pommie

##### Well-Known Member
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.

#### blueroomelectronics

##### Well-Known Member
Plus his GSM phone is USB according to another thread.

#### ahmedragia21

##### Member
blueroom , its not usb ! , its serial , i connect it to hyperterminal very fine and i do AT commands with it !

#### blueroomelectronics

##### Well-Known Member
What is the cable part number. Do you have a photo?

#### ahmedragia21

##### Member
blueroom ,
the problem is with the gets function , it waits for an input , and i cant do any code

#### Nigel Goodwin

##### Super Moderator
blueroom ,
the problem is with the gets function , it waits for an input , and i cant do any code
Then don't use 'gets', it's the wrong function - write your own.

#### ahmedragia21

##### Member
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 ...

##### Banned
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:

#### kchriste

##### New Member
Forum Supporter
It would also help if ahmedragia21 told us which uC and which compiler he was using so we could suggest another function to use.

#### ahmedragia21

##### Member
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 .

##### Banned
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.

#### ahmedragia21

##### Member
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:

#### kchriste

##### New Member
Forum Supporter
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:

#### ahmedragia21

##### Member
Thanks , good solution

Status
Not open for further replies.