• C

How to select which string to output

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

liyulinmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
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
liyulinmAuthor Commented:
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
sunnycoderCommented:
>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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

liyulinmAuthor Commented:
Oh i see. Thanks. I'd need to try it out back in the office, so I'll keep u updated tomorrow.
0
liyulinmAuthor Commented:
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
sunnycoderCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
liyulinmAuthor Commented:
Yes, I understand now. I shouldn't have gone 1 big round to do this! Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.