struct RFSENSOR {
uint8_t code[4];
char name[18];
};
struct RFSENSOR RFsensor[4];
char line[36];
char FFile[66] = {"0xc1,0x8d,0x81,0,Zaplava koupelna\n"};
uint8_t LineNo = 0;
//------------------------------------------------------------------------------
// Check for extra characters in field or find minus sign.
char* skipSpace(char* str) {
while (isspace(*str)) str++;
return str;
}
//------------------------------------------------------------------------------
bool parseLine(char* str) {
char* ptr;
Serial.print("Line No: ");
Serial.println(LineNo);
// Set strtok start of line.
str = strtok(str, ",");
// strtoul accepts a leading minus with unexpected results.
if (*skipSpace(str) == '-') return false;
// Convert string to unsigned int 8b.
RFsensor[LineNo].code[0] = strtoul(str, &ptr, 0);
if (str == ptr || *skipSpace(ptr)) return false;
Serial.println(RFsensor[LineNo].code[0]);
str = strtok(nullptr, ",");
if (!str) return false;
RFsensor[LineNo].code[1] = strtoul(str, &ptr, 0);
if (str == ptr || *skipSpace(ptr)) return false;
Serial.println(RFsensor[LineNo].code[1]);
// Subsequent calls to strtok expects a null pointer.
str = strtok(nullptr, ",");
if (!str) return false;
// strtoul accepts a leading minus with unexpected results.
if (*skipSpace(str) == '-') return false;
// Convert string to unsigned int 8b.
RFsensor[LineNo].code[2] = strtoul(str, &ptr, 0);
if (str == ptr || *skipSpace(ptr)) return false;
Serial.println(RFsensor[LineNo].code[2]);
str = strtok(nullptr, ",");
if (!str) return false;
// strtoul accepts a leading minus with unexpected results.
if (*skipSpace(str) == '-') return false;
// Convert string to unsigned int 8b.
RFsensor[LineNo].code[3] = strtoul(str, &ptr, 0);
if (str == ptr || *skipSpace(ptr)) return false;
Serial.println(RFsensor[LineNo].code[3]);
str = strtok(nullptr, "\n");
if (!str) return false;
strlcpy(RFsensor[LineNo].name, str, 19);
Serial.println(str);
// Check for extra fields.
return strtok(nullptr, ",") == nullptr;
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
// Wait for USB Serial
while (!Serial) {
yield();
}
if (!parseLine(FFile)) {
Serial.print("parseLine failed");
}
Serial.println();
LineNo++;
for (byte i = 0; i < LineNo; i++) {
Serial.print(RFsensor[i].code[0]);
Serial.print(" ");
Serial.print(RFsensor[i].code[1]);
Serial.print(" ");
Serial.print(RFsensor[i].code[2]);
Serial.print(" ");
Serial.print(RFsensor[i].code[3]);
Serial.print(" >");
Serial.print(RFsensor[i].name);
Serial.println("<");
}
Serial.println(F("\nDone"));
}
void loop() {}