nathan
Reged: Oct 16 2003
Posts: 18
|
|
how do i make a string?
say i receive a bunch of chars from the serial port for example, it is not user driven, how do i get those chars into a string
say i get n, a, t, h, a, n from the serial port. i want that in a string "nathan" so i can use the strstr functions, for example, on it.
should not be too hard?
thanks
nathan
|
Paul Nijdam
Reged: Oct 16 2003
Posts: 444
|
|
Hi,
See mails on this in the archive:
Fri May24 18:33
Here all questions & example code can be found,
Good luck!
>how do i make a string?
>
>say i receive a bunch of chars from the serial port for example, it is not user driven, how do i get those chars into a string
>
>say i get n, a, t, h, a, n from the serial port. i want that in a string "nathan" so i can use the strstr functions, for example, on it.
>
>should not be too hard?
>
>thanks
>
>nathan
|
nathan
Reged: Oct 16 2003
Posts: 18
|
|
thank you paul
i got the routines and it works great
the only thing that i don't quite understand about them is the following
the msg buffer mess i declare as
#define SIZE_OF_MESS 5
char mess[SIZE_OF_MESS] = 0;
so a string containing 4 chars and the terminating zero should be all that it can hold
but i made it look for a command "led1ON" as soon as i press using the strstr cmd
led1ON is 6 chars, but strstr still picks it up correctly.
using the following code snippet, should'nt Rcindex just increment till it reaches SIZE_OF_MESS - 1?
if (Rcindex * SIZE_OF_MESS-1) Rcindex++;
so i'm a bit confused as to how this works
if you can shed some light on this for me
thanks for the help so far
|
Paul Nijdam
Reged: Oct 16 2003
Posts: 444
|
|
Hi Nathan,
indeed the
"
using the following code snippet, should'nt Rcindex just increment till it reaches SIZE_OF_MESS - 1?
"
is correct. You may be confused by the fact that the 'smaller than' operator cannot be displayed on this forum (will disappear)...
With certain escape codes things are possible but I always forget that!
Also note that if you do not comply to the string boundary, things may still seem to work but with the danger that other RAM variables are overwritten...
Good luck!
>thank you paul
>
>i got the routines and it works great
>
>the only thing that i don't quite understand about them is the following
>
>the msg buffer mess i declare as
>
>#define SIZE_OF_MESS 5
>char mess[SIZE_OF_MESS] = 0;
>
>so a string containing 4 chars and the terminating zero should be all that it can hold
>
>but i made it look for a command "led1ON" as soon as i press using the strstr cmd
>
>led1ON is 6 chars, but strstr still picks it up correctly.
>
>using the following code snippet, should'nt Rcindex just increment till it reaches SIZE_OF_MESS - 1?
>
>if (Rcindex * SIZE_OF_MESS-1) Rcindex++;
>
>so i'm a bit confused as to how this works
>
>if you can shed some light on this for me
>
>thanks for the help so far
|
nathan
Reged: Oct 16 2003
Posts: 18
|
|
i've found another weird thing concerning this strings that i don't quite understand
i send an at command to the modem
the response is
+CCLK: “00/06/09,17:34:23”
this I put in my string.
but it doesn't keep that value.
sometimes it displays like +,CLK....
other times just +,
If I display that string, just sending it through the serial port to see what it's value is, it displays as it should: +CCLK: “00/06/09,17:34:23”
But say I want to do that again, the value of the string is not the same anymore.
I don't know what I'm doing wrong. I've declared the buffer which should hold the string as
#define SIZE_OF_MESS 30
char mess[SIZE_OF_MESS] = 0;
I don't see why the value of mess whould not stay as it is.
Hopy somebody can help
Thanks
|
nathan
Reged: Oct 16 2003
Posts: 18
|
|
i know what my problem is, but i'm not quite sure what my solution will be
i get the reply from the modem as follows
+CCLK: “00/06/09,17:34:23”
so i get the CR & LF before and after the actual string i want.
so i think that i overwrite the string before i can read it?
because without the modem, if i send a command through the serial string, ending it with the CR, by pressing enter, everything is fine.
but in the modem string, there's a lot than one CR and not just at the end of the string
so how would i extract my string then?
hope i'm explaining this ok
|
Paul Nijdam
Reged: Oct 16 2003
Posts: 444
|
|
I think I get yuor problem: you get an CR/LF at the beginning of your string and on the end...
your code was something like this:
if ((Rcdummy == '\n') || (Rcdummy== '\r'))
{
mess[Rcindex]=0;
command=1;
Rcindex =0;
}else
{
mess[Rcindex]=Rcdummy;
if (Rcindex SIZE_OF_MESS-1) Rcindex++;
};
this may be changed into:
if ((Rcindex>3) && ((Rcdummy == '\n') || (Rcdummy== '\r')))
{
mess[Rcindex]=0;
command=1;
Rcindex =0;
}else
{
if (!((Rcdummy == '\n') || (Rcdummy== '\r')))
{
mess[Rcindex]=Rcdummy;
if (Rcindex SIZE_OF_MESS-1) Rcindex++;
}
};
not the most sexy program snipped, but it easy to grasp!
>i know what my problem is, but i'm not quite sure what my solution will be
>
>i get the reply from the modem as follows
>
> +CCLK: “00/06/09,17:34:23”
>
>so i get the CR & LF before and after the actual string i want.
>
>so i think that i overwrite the string before i can read it?
>
>because without the modem, if i send a command through the serial string, ending it with the CR, by pressing enter, everything is fine.
>
>but in the modem string, there's a lot than one CR and not just at the end of the string
>
>so how would i extract my string then?
>
>hope i'm explaining this ok
>
>
>
|
nathan
Reged: Oct 16 2003
Posts: 18
|
|
thank you, that should help.
should have thought of that actually...oh well
sorry that the questions keep on coming like this, but there is something about this topic i still want to know
say i have my string all nice and tidy now in mess
and it looks like this
+CCLK: “00/06/09,17:34:23”
say i only want:
00/06/09,17:34:23
from my string
how do i copy only that to another string?
thanks for the help so far
ps i'm going to post this at the top of the forum as well, so more people can see my question
|
Paul Nijdam
Reged: Oct 16 2003
Posts: 444
|
|
You can use the string strip/copy/ etc. functions, but to save code space and RAM you also may modify:
if ((Rcindex>3) && ((Rcdummy == '\n') || (Rcdummy== '\r')))
into
if ((Rcindex>8) && (Rcindex[15) && ((Rcdummy == '\n') || (Rcdummy== '\r')))
(the [ is 'smaller than')
Here the start index is 8, the stop index is 15 (if I count well...)
>thank you, that should help.
>
>should have thought of that actually...oh well
>
>sorry that the questions keep on coming like this, but there is something about this topic i still want to know
>
>say i have my string all nice and tidy now in mess
>and it looks like this
>
>+CCLK: “00/06/09,17:34:23”
>
>say i only want:
>
>00/06/09,17:34:23
>
>from my string
>
>how do i copy only that to another string?
>
>thanks for the help so far
>
>ps i'm going to post this at the top of the forum as well, so more people can see my question
|