Solved

How to select which string to output

Posted on 2003-11-25
7
258 Views
Last Modified: 2010-04-15
hi
I'm helping my company to to code a simple c program that can select a particular string of characters to output to screen from a hardware (a GPS).

I'm using interrupts and have successfully output ALL the different strings from the GPS to the screen, but now I need to output only a user selected one.

The GPS output (strings of characters) is received via COM1/COM2 and stored in a buffer (done)
print the stored buffer to the screen (done)
select (String 1/2/3 etc) to output to screen (help!!!)

Each GPS string always end with a CRLF. The strings look like this:

$GPGGA,001101,0120.1867,N,10354.2943,E,1,04,02.7,00056,M,07,M,,*72     //string 1
$GPVTG,000,T,,M,000.0,N,000.0,K,A*13                                                     //string 2
$GPGGA,001102,0120.1866,N,10354.2943,E,1,04,02.7,00056,M,07,M,,*70     //string 1 again
$GPVTG,000,T,,M,000.0,N,000.0,K,A*13                                                     //string 2 and so on
$GPGGA,001103,0120.1867,N,10354.2943,E,1,04,02.7,00057,M,07,M,,*71
$GPVTG,000,T,,M,000.0,N,000.0,K,A*13

In this case, there are only 2 different strings and I'd like to choose only one of them to output to screen. Please give some expert opinions on this. Thanks!

liyulinm

0
Comment
Question by:liyulinm
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9816393
Hi liyulinm,

assuming that your strings are in buffers string1 and string2
int i;

printf ( "select a string : " );
scanf ( "%d", i );

switch ( i )
{
    case 1:
        printf ( "%s", string1 );
        break;
    case 2:
        printf ( "%s", string2 );
        break;
    default:
        printf ( "invalid option\n" );
        break;
}

Cheers!
Sunny:o)
0
 

Author Comment

by:liyulinm
ID: 9816527
Hi sunnycoder,
No, all the strings are stored in 1 buffer. The strings will look like this all jumbled up together:

$GPGGA,001101,0120.1867,N,10354.2943,E,1,04,02.7,00056,M,07,M,,*72CRLF$GPVTG,000,T,,M,000.
0,N,000.0,K,A*13CRLF$GPGGA,001102,0120.1866,N,10354.2943,E,1,04,02.7,00056,M,07,M,,*70$GPV
TG,000,T,,M,000.0,N,000.0,K,A*13CRLF$GPGGA,001103,0120.1867,N,10354.2943,E,1,04,02.7,00057,
M,07,M,,*71CRLF
         
Do u suggest that I store them seperately? Another problem is that different brands of GPS gives different numbers of string, that's why i store them in 1 buffer 4 convenience sake... or am i wrong?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9816575
>Do u suggest that I store them seperately?
sounds like a good idea ...

>Another problem is that different brands of GPS gives different numbers of string, that's why i store them in 1 buffer 4
>convenience sake... or am i wrong?
two possible approaches
1. store them using an array of char * ... that way it will be easy to select and display string as I posted above

2. If you wish to store them in one string, then you can proceed like this
(assuming that sequence \r\f delimits a string )


int i,j;

char * temp = string;

printf ( "select a string : " );
scanf ( "%d", i );

switch ( i )
{
    case 1:
    case 2:
        for ( j = 0; j < i-1 ; j++ )        /* look for the end of i-1th string */
                temp = strstr ( temp, "\r\f" );      
        if ( *temp == '\r' && *(temp+1) == '\f' ) /*this check will avoid incrementing temp if we are displaying first string*/
                temp = temp + 2;              /* increment to skip over \r\f */      
        while ( *temp != '\r' && *(temp+1) != '\f' ) /*write till the end of string*/
        {
                 putchar((int)*temp);        
                 temp++;
        }
        break;
    default:
        printf ( "invalid option\n" );
        break;
}

In context of this problem, I find separate storage of strings as cleaner ... however, you are dealing with a larger product, so you neeed to view it in context of the overall picture.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:liyulinm
ID: 9816612
Oh i see. Thanks. I'd need to try it out back in the office, so I'll keep u updated tomorrow.
0
 

Author Comment

by:liyulinm
ID: 9831395
Hi sunnycoder

I'm terribly sorry about the late reply, I was not in the office for the last 2 days.
I've tried out your code and it works fine except that the '\f' should be '\n'. But I've decided to go with your 1st suggestion- to store the strings in seperate arrays as I agree it's more manageable.

1) Can you pls explain this "for loop"? I don't quite understand it.
        for ( j = 0; j < i-1 ; j++ )        /* look for the end of i-1th string */
                temp = strstr ( temp, "\r\n" );  

2) How would you suggest I "split" the strings into seperate arrays, say string1[64], string2[64]... until stringX[64]? (as I used to store them all in 1 array)    

Here's my attempt:
main () {
        // some routines to get GPS data
        while (GPS has data to output) {
            int a,b,c=0;
            char *ptr;
            char *temp=buf[0];
            char string1[64], string2[64], string3[64]; // and so on until string64[64]
            buf [c]= GPS data;  //GPS raw data is from the COM port
            c++;        // as more data come in;
            // to get the 1st string, do steps below:
            if ( *temp == '\r' && *(temp+1) == '\n' ) {
                    a=strlen(buf);
                    ptr= &(temp-a);
                    for (b=0; b<a; b++) {
                        string1[b]= *ptr;
                        ptr++;
                    }
             }

I'm trapped with getting the 2nd and subsequent Xth string. Your kind help is very much appreciated 8-)

Thanks

liyulinm

         
             
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 140 total points
ID: 9835840
for ( j = 0; j < i-1 ; j++ )        /* look for the end of i-1th string */
               temp = strstr ( temp, "\r\n" );  

If all your strings are stored in a single buffer/string, then only way of distinguishing one from another is the sequence \r\n
Where does your string 2 start ... after the first \r\n
Where does your string 3 start ... after second \r\n
in general, your ith string will start after i-1 th \r\n

strstr locates a substring in the given substring ... so after first iteration temp will point to first \r\n ... now we have to increment temp to skip over this and again search for next \r\n in the remaining string (starting at temp) until we have found i-1th \r\n sequence... I just notices that I had not included the increment in the loop which is an error .. it infact should look like

for ( j = 0; j < i-1 ; j++ )        /* look for the end of i-1th string */
{
               temp = strstr ( temp, "\r\f" );      
               temp = temp + 2;
}

>2) How would you suggest I "split" the strings into seperate arrays, say string1[64], string2[64]... until stringX[64]?
>(as I used to store them all in 1 array)    
Why take the trouble of splitting them into separate arrays ? Why not read them into separate arrays in the first place ?
There is no benefit in reading them all in the same array and then splitting them into separate ones when you can read them into separate arrays!!!
0
 

Author Comment

by:liyulinm
ID: 9837102
Yes, I understand now. I shouldn't have gone 1 big round to do this! Thanks again!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now