Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Problem With Extracting number from a string , Codevision AVR , sscanf

Status
Not open for further replies.

nimaaa

Newbie level 6
Joined
Jul 25, 2014
Messages
14
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Location
Tehran, Iran, Iran
Activity points
122
Hi

I have the string below and I want to extract the credit from it .

"+CUSD: 0,Credit: 16122 Rial"

Here is my code :

Code:
#include <stdio.h>

int useless_value = 0 , credit_value = 0 ,scan = 0 ;  

char response[27] = "+CUSD: 0,Credit: 16122 Rial"

void main(void)

{

scan = sscanf(response,"%*[^0123456789]%d%*[^0123456789]%d",&useless_value,&credit_value);

printf("%d%d",useless_value,credit_value);

sprintf(LCD_buffer,"%d",credit_value);

}

I checked it in DEV C++ , and it works correctly (I got 16122) .
but when I program it to the microcontroller ( using Codevison AVR ), the output result is always 0 ( I display it on a LCD ) .

Please help me to solve this proplem.

Thanks In Advance
 
Last edited:

What is the code you posted ? Is it CodeVisionAVR code or DEV C++ code ? If CodeVisionAVr then are you using something like Regular Expressions ? If not, then zip and post the complete CodeVisionAVR project and I will modify the code to extract the Credit value from the string.

Edit:

This is the code I wrote.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
char response[] = "+CUSD: 16842,Credit: 16122 Rial"
char credit[20];
 
void Extract_Credit(char *text, char *result) {
 
    
    while((*text) && (*text ! = ' '))
        *text++;
 
    *text++;
 
    while((*text) && (*text != ','))
        *result++ = *text++;
 
    *result = '\0';
}
 
 
void main() {
 
 
    memset(credit, '\0', sizeof(credit);
 
    while(1) {
        Extract_Credit(response, credit);
        //Print credit on LCD       
    }
}

 
Last edited:

printf and scanf group of functions are too heavy for mC and may not be implemented as expected. Regular expression is probably not implemented in your library.
Try simple string comparison/searching in stead:

Code:
int extract(const char *responce){
    const char *p = strstr(responce,"Credit:");
    return (p) ? atoi(p+7) : -1;
}

- - - Updated - - -

This is the code I wrote.


Code C - [expand]
1
2
3
4
5
6
7
8
9
char response[] = "+CUSD: 16842,Credit: 16122 Rial"
... while((*text) && (*text ! = ' '))
        *text++;
 
    *text++;
 
[B] while((*text) && (*text != ','))
        *result++ = *text++;
[/B]


you take the first (not important) value. The second numeric value is the "credit" required.
You could check for the second ':' or use an other way...
 
Sorry. I thought the 0 or numeric value that comes infront of the first comma (,) as the credit value. I have fixed the code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
char response[] = "+CUSD: 16842,Credit: 16122 Rial"
char credit[20];
 
void Extract_Credit(char *text, char *result) {
 
    
    while((*text) && (*text ! = ' '))
        *text++;
 
     *text++;
 
    while((*text) && (*text ! = ' '))
        *text++;
 
     *text++;
  
    while((*text) && (*text != ' '))
        *result++ = *text++;
 
    *result = '\0';
}
 
 
void main() {
 
 
    memset(credit, '\0', sizeof(credit);
 
    while(1) {
        Extract_Credit(response, credit);
        //Print credit on LCD       
    }
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top