CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Comparing strings from eeprom

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

Comparing strings from eeprom
PostPosted: Wed Apr 14, 2010 9:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 14, 2010 11:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 15, 2010 2:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 15, 2010 12:04 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group