3

I have a char array in which reads data from some EEPROM device, in hardware, and if there is no data in there, its value can be anything (garbage).

I would like to check if his value is not garbage and have some valid chars.

                       for(int k=address;k<address+MEM_MAX_LEN;k++)
                       {
                           charBuf[k-address]= EEPROM.read(k);
                           if(charBuf[k-address]=='*')
                         {
                              charBuf[k-address]='\0';
                              break;
                         }

When using strlen>1 I don't get the desired respond (obviously).

How can I check it?

Curnelious
  • 1
  • 16
  • 76
  • 150
  • 1
    Will the string be NUL-terminated? Can it contain non-printable characters? – Thomas Padron-McCarthy Oct 26 '15 at 13:21
  • 2
    A byte is a byte, so there is no way of discering between a valid one and a wrong one. What about a CRC at the end of the data (if fixed lenght) and two STX ETX markers of your choice + CRC (if variable lenght)? – Francis Straccia Oct 26 '15 at 13:21
  • yes, if the eprom data is not valid it can be non printable chars , but if its valid , i get only valid chars. is there a simple way to check for valid chars? – Curnelious Oct 26 '15 at 13:22
  • Either the eeprom data is 100% correct or it is 100% incorrect, so I don't see how these requirements make any sense. In order to tell if something is valid, you must first define what is valid and what is not. Also note that the value 0 is one possible garbage value. The _proper_ way to check if an eeprom is valid is otherwise to program a checksum after the data. – Lundin Oct 26 '15 at 13:29
  • Getting back valid characters doesn't necessarily mean it's not garbage. You need a clear, concise definition of what constitutes valid data. – dbush Oct 26 '15 at 13:30
  • ok, the definition of valid is all abc-xyz , and all numbers 0-9. (for example abc456efg89) – Curnelious Oct 26 '15 at 13:32
  • "If the eprom data is not valid it can be non printable chars" is a double double-edged sword. "Non printable" depends on your own personal interpretation: is the binary code `A7` non-printable because it's beyond the regular ASCII set, or is it correct because it happens to display `§`? And the reverse: even if your buffer contains only printable characters (by any standard) you cannot be sure it is *therefore* valid data. – Jongware Oct 26 '15 at 13:33

3 Answers3

7

You can never be sure, since garbage can look like valid text, but you can make reasonable guesses. Something like this, assuming that a valid string should be NUL-terminated and contain only printable characters:

#include <ctype.h> // For isprint

// ....

int length = 0;
while (length < MEM_MAX_LEN && charBuf[length] != '\0')
    ++length;

if (length == 0) {
    printf("Empty string. Probably garbage!\n");
    return 0;
}

if (length == MEM_MAX_LEN) {
    printf("No NUL byte found. Probably garbage!\n");
    return 0;
}

for (int i = 0; i < length; ++i) {
    if (!isprint((unsigned char)charBuf[i])) {
        printf("Unprintable char (code %d) at position %d. Probably garbage!\n",
               (unsigned char)charBuf[i], i);
        return 0;
    }
}

printf("String consists of %d printable characters. Probably not garbage!\n", length);
return 1;
Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
1

Given that "garbage values" might include anything including the value 0, aka the null terminator, and given the definition of "valid" posted in comments:

the definition of valid is all abc-xyz , and all numbers 0-9. (for example abc456efg89)

Then simply write a function such as this:

#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>

bool is_valid (const uint8_t* data, size_t size)
{
  for(size_t i=0; i<size; i++)
  {
    if( !isalnum(data[i]) )
      return false;
  }

  return true;
}

Keep in mind that this will return false for spaces and the null terminator. You will not be able to assume that a string is properly null terminated, nor that the value 0 always means an end of a string.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

Set it to something non-random before using it and test to see if it has changed?

Stephen
  • 1,607
  • 2
  • 18
  • 40
  • 1
    its not working because even if it was something , after it gets the eprom value it becomes garbaged – Curnelious Oct 26 '15 at 13:18
  • and another problem this approach have is that if lets say its "***" and i check for *** , so in case the eprom data was valid, i will also get false for the check . i will actually always get false. – Curnelious Oct 26 '15 at 13:21
  • If you use a sufficiently long something the odds of garbage randomly generating your something is akin to a million monkeys on a million typewriters. – Stephen Oct 26 '15 at 18:44