View previous topic :: View next topic |
Author |
Message |
herby handcock Guest
|
Clearing contents of an array |
Posted: Wed Aug 11, 2004 4:14 am |
|
|
What is the easiest way of clearing an array used to buffer characters received from RS232. When I fill array with characters I search for a substring then if it is present I want to set array to contain nothing. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Aug 11, 2004 5:58 am |
|
|
memset() would do that for you. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Aug 11, 2004 11:55 am |
|
|
Code: |
void clear_array(int Buffer_NAME, int Buffer_SIZE)
{
int n;
for(n = 0; n <= Buffer_SIZE; n++)
{
Buffer_NAME[n] = 0;
}
}
|
Best wishes,
Humberto |
|
|
Tom-H-PIC
Joined: 08 Sep 2003 Posts: 105 Location: New Castle, DE
|
Ring Buffer |
Posted: Wed Aug 11, 2004 1:26 pm |
|
|
Why don,t you use a ring buffer?
Then you don't have to clear it just get the data out of the buffer.
I use them on all of my serial PIC projects.
Look at CCS examples EX_SISR.C.
Tom |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Aug 11, 2004 4:46 pm |
|
|
herby handcock asked
Quote: |
I want to set array to contain nothing.
|
Ooops, pls change
Code: |
void clear_array(int Buffer_NAME, int Buffer_SIZE)
|
To:
Code: |
void clear_array(int *Buffer_NAME, int Buffer_SIZE)
|
Humberto
. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 12, 2004 8:38 am |
|
|
Tom-H-PIC: could you post your ring buffer code into the code library? I have used them in the past,... but I want to find out if I'm doing it in the most efficient way. I'll post mine also.
tjr |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Aug 12, 2004 2:48 pm |
|
|
My perference is for linear buffers using an index. When moving data that has a predefined pattern the data can be accessed in the buffer directly rather than indirectly. That makes the code smaller and faster. On the other hand if you are sending or recieving data in a continous stream ring buffers are a better choice. Either way you go you only have to clear the index values not the actual buffer. |
|
|
|