Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: Split string read from serial port 1

Status
Not open for further replies.

RoyceyBaby

IS-IT--Management
Mar 23, 2001
22
0
0
I am an extreme newbie to C.

I am trying to write a program that reads/write data from/to a serial port.

I have mananged to get a small program that reads data in from the serial port using the select statement.

What I read in is whatever is in the buffer of the serial port. As such I get fragments of information.

What I read in could be something like this

First Select Call "Command 1\nCommand 2\nCom"
Second Select Call "mand 3\n"

I append all the data to one string

BufferString="Command 1\nCommand 2\nCommand 3\n"

How do I pull off each command. I think it is something to do with pointers, a kick in the general direction and I would be very grateful.

Many thanks,

Royce
 
Sorry for the confusing question.

"Pull off each command" I am referering to the data I receive from my serial port, they are commands/text.

I suppose the simplest question would be.

How do I split a string into seperate strings, using \n as the cut point.

Hope that is clearer.

Royce
 
Ok, that's what I *thought* you meant, but didn't want to post a possibly long answer in case you meant something entirely different ;-)

strtok() is one way to do it. Here's a sample:

int main(void)
{
char str[]="Command 1\nCommand 2\nCommand 3\n";
char *fld;

for (fld=strtok(str,"\n");fld!=NULL;fld=strtok(NULL,"\n")) {
/* fld points to each substring here */
}
return 0;
}

If you want to save each substring and then execute them after the loop you can store them in an array:

char *arr[SOME_SUITABLE_SIZE];
int i=0;

for (fld=strtok(str,"\n");fld!=NULL;fld=strtok(NULL,"\n")) {
arr[i++]=fld;
}

This approach only works if

1. You don't modify str between the time that you pass it to strtok() and you're done using the commands stored in arr.

2. You don't need your array of commands to persist beyond the scope of str. Since each element ("command") is a pointer to a place in str, the pointers will become invalid once str goes out of scope. If this is the case you can use malloc() to make a separate copy of each string:

#include <stdlib.h>
#include <string.h>

/* ... */

char *arr[SOME_SUITABLE_SIZE];
int i=0;

for (fld=strtok(str,&quot;\n&quot;);fld!=NULL;fld=strtok(NULL,&quot;\n&quot;)) {
arr=malloc(strlen(fld)+1);
if (arr!=NULL) {
strcpy(arr,fld);
} else {
/* error! */
}
}

One thing to be wary of, as implied above, is that strtok() modifies its first argument. It works by replacing all occurences its second argument with NUL characters. Above, fld is just a pointer into the original string. If you need to retain the value of the original string, don't use strtok() or copy the original string somewhere else before passing it to strtok().

At the end of the loop above, this statement puts(str); will output &quot;Command 1&quot; since all instances of '\n' were replaced by the NUL character.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top