View previous topic :: View next topic |
Author |
Message |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
Comparing strings from eeprom |
Posted: Wed Apr 14, 2010 9:59 am |
|
|
Fellow programmers,
Hopefully I can describe what I'm attempting well enough for all to understand.
I have a circuit that reads ASCII characters from an external eeprom on various boards. My circuit connects to these boards, one at a time, via a rather unreliable connector (not my choice, OEM designed). I read these characters, from the eeprom one at a time, and stuff them into one string variable, terminated with a NULL. If the connection is good the strings will be displayed on an LCD screen properly. If the connection is not good the LCD wiggs out due to reading all zeros(pull down resistors on the bus), which stuffs NULL characters in the entire string variable.
I would like to test this string variable for all NULLs and copy a predetermined string into it so the LCD screen remains stable. I've tried to use strcmp() to test if the string contains all zeros, strcmp(string, 0); but all I ever get is a -1 result from strcmp(). What technique would you advise to test for this NULL condition?
Many thanks!
Ronald |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 14, 2010 11:42 am |
|
|
memcmp() will compare two arrays without stopping at a 0x00 byte.
You have to specify the byte count so it knows when to stop.
However, this will require an array of 0x00 bytes that is equal in size
to the array that you want to test. It might be easier to write a small
routine to check if any non-zero elements exist in your array. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Apr 15, 2010 2:39 am |
|
|
It is only the last char of a sequence of characters which should be null to represent a string. You char array is usually longer than this but it doesn't matter what comes after the null in the array as it should be ignored. Based on that I assume you have a problem with your display routine as a normal display routine will read the first char as null and return, therefor not displaying anything.
if your display routine is causing the pic to hang when presented with a string which has a null in any position other than the last all you need to do is do a strlen check, this will return the number of chars upto the first null. If the length is less than what you expect then just output your error string.
It is more likely that you are getting garbage chars and not nulls in your string. |
|
|
sjb
Joined: 13 Apr 2010 Posts: 34 Location: UK
|
|
Posted: Thu Apr 15, 2010 12:04 pm |
|
|
I was thinking along the same lines as Wayne above, but if you still need to check for all NULLs - which implies you know the length of the string from some other method than using strlen() - then why not OR all the characters in the string.
Something like...
Code: |
int len = LENGTH_OF_STRING;
char x = 0;
while(len--)
x |= string[len];
|
Now x will be zero only if all the characters are NULL. |
|
|
|