View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Searching Buffers and comparing to characters NOT IN RAM |
Posted: Wed Aug 12, 2009 11:26 am |
|
|
hello all,
I'm working on a gsm project but I am having some issues on how to
get data from a buffer that gets filled on my ISR.
Its for a GSM project but not a GSM question, I got that part DOWN! haha.
The problem is something like this:
Buffer[50]={'a','b','c',.....'s','t','r','i','n','g',....'z'}
//once i get all my data my buffer is something like that...
I want to check if "string" is somewhere inside my buffer.
I can check for individual characters with a for loop and one thousand nested "IF" statements... since I am looking for the characters "string" in that precise order but that sucks. Doesn't seem too efficient
and I'm not going to be able to reuse my search function to get other strings.
Wanted to see if anyone had a nice method of doing this. My ideal method and additional issue are as follows.
ALSO, since I have a little RAM issue, I was wondering if there was a
way have a list of characters on the program code, NOT IN RAM, and
be able to compare that list or lists to my buffer...
It would be nice if this list (more like a table) could be searched using a counter.
So that I could feed my buffer and the list/table to a for loop
with the appropriate counter values and only compare a set of characters
one by one.
So I would have something like this:
(pseudo / semi real / imaginary code)...
Code: |
Buffer[50]={'a','b','c',.....'s','t','r','i','n','g',....'z'} //data in ram from ISR
Table{'s','t','r','i','g','n',} // my table FIXED ON PROGRAM MEMORY AT PROGRAMING TIME
for(counter=25;counter<32;counter++) //string is 6 chars.. starting at position 25 in the buffer...
{
if(buffer[counter]!=Table[counter-25])
ErrorFlag=true,
}
|
In my particular case, I know before searching my buffer the exact position where my "string" starts.... thus my strange counter values.
What I am asking seems to me very familiar... from my c++ days.
Any light on this issue would be greatly apreciated...
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 11:52 am |
|
|
You can use the strstr() function which comes with <string.h>. The function returns a pointer to the first occurence of the string. From Barnett and Cox's book an example code snippet looks like:
Code: |
char stra[] = "Red Green Blue";
char strb[] = "Green";
char* ptr;
void main(void)
{
ptr = strstr(stra,strb)// looks for the string "Green"
//returns "Green Blue"
}
|
|
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Wed Aug 12, 2009 11:58 am |
|
|
This is already available. Use strstr(), or if more complicated, strtok().
Note that CCS probably requires both to be in RAM, not ROM.
Mark S. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Aug 12, 2009 12:03 pm |
|
|
mkuang:
that is sweet, but as far as i can see,
i have to have the string im looking for on another string/array
in RAM....which is what i am trying to avoid...
i liked your suggestion... i think if all else fails, ill take this one...
seems pretty good, exept for the RAM part...
(im not trying to shoot down your suggestion because its not exactly what im looking for.... i do like it, and probably will have to use it... just wanted to clarify that im not being an ungratefull Bi..h*)
thanks for your help...
g _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 12:09 pm |
|
|
In my code snippet if you declare:
Code: |
const char strb[] = "Green";
|
then strb will be in ROM and not RAM. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Aug 12, 2009 12:20 pm |
|
|
Mkuang,
hold the phone. hahaha... if you declare something as
Const char str[]="ABC",
its on rom, as in the program memory? SWEET... (is it?)
can i search through this const array using a counter
kinda like :
Const char str[]="ABC";
if(str[1]=='B';
do somthing....
... if the above is true... my problem is solved...
is it?
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 12:27 pm |
|
|
Unfortunately your buffer[50] has to be in RAM since the values it is holding can change. What is NOT changing presumably are the characters that you know you are looking for. i think you stored these values in the array called Table. These are the characters you can place in ROM. If you look at the hex file you actually see these constants in there if you ever make a test program. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Aug 12, 2009 12:32 pm |
|
|
yeah yeah i know that my buffer[50] will be on ram..
i need it there....
the issue is with the strings that i want to look for...
as long as i can keep those in ROM and can search the "table" using a counter i am set...
is my example on my previous post true?
g _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 12:35 pm |
|
|
Gabriel wrote: | the issue is with the strings that i want to look for...
as long as i can keep those in ROM and can search the "table" using a counter i am set...
is my example on my previous post true?
g |
Yes that is true. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Aug 12, 2009 12:38 pm |
|
|
mkuang... i think i love you...
hahahaha
thank you
g _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Wed Aug 12, 2009 1:02 pm |
|
|
Sorry, this will fail. CCS will not allow the argument to be a pointer to constant. You will need to copy to RAM. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Aug 12, 2009 1:04 pm |
|
|
...damn
so close.....
further suggestions?
g _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 1:22 pm |
|
|
mskala wrote: | Sorry, this will fail. CCS will not allow the argument to be a pointer to constant. You will need to copy to RAM. |
Dammit I forgot about that ! Strstr returns a pointer...
Actually I just checked and all the functions in string.h only accept strings in RAM as inputs (i.e. the inputs are of the *ptr type). So I think you are screwed.
Last edited by mkuang on Wed Aug 12, 2009 1:39 pm; edited 1 time in total |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Aug 12, 2009 1:34 pm |
|
|
wait wait wait
but will this work:
const char strA[]="ABC";
if(strA[1]=='B')
do somthing? _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Aug 12, 2009 1:36 pm |
|
|
In the end, all i've ever seen has been..
keep them all in ROM.
copy ROM entry for item you want to RAM.
compare in RAM. |
|
|
|