good afternoon. THIS CODE WORKS UNO, MEGA.
tell me where to look for the problem, how to fix it? IDE 1.8.11
Thank You
parsing.txt (7.55 KB)
parsing.ino (2.84 KB)
good afternoon. THIS CODE WORKS UNO, MEGA.
tell me where to look for the problem, how to fix it? IDE 1.8.11
Thank You
parsing.txt (7.55 KB)
parsing.ino (2.84 KB)
I don't usually download files but I was bored. (Please read "How to use this forum" posted as a sticky on nearly every part of the forum).
OPs Code
/*
Пример текстового интерфейса через монитор порта. Парсинг осуществляется
через switch и встроенные методы работы со строками.
Принятие в порт НЕ блокируещее!!! Читаем посимвольно
Пример отправки: keyword 5; - ключевое слово keyword, значение 5
Между ключевым словом и значением стоит divider (можно настроить)
Посылка завершается символом ending (можно настроить)
Парсинг работает со включенным и выключенным концом строки, т.е. универсально
*/
char divider = ' ';
char ending = ';';
const char *headers[] = {
"asd1", // 0
"asd2", // 1
"asd3", // 2
"asd4", // 3
"qw1", // 4
"qw2", // 5
};
enum names {
MOTOR1, // 0
MOTOR2, // 1
MOTOR3, // 2
MOTOR4, // 3
SETT1, // 4
SETT2, // 5
};
names thisName;
byte headers_am = sizeof(headers) / 2;
uint32_t prsTimer;
String prsValue = "";
String prsHeader = "";
enum stages {WAIT, HEADER, GOT_HEADER, VALUE, SUCCESS};
stages parseStage = WAIT;
boolean recievedFlag;
void setup() {
Serial.begin(9600);
}
void loop() {
parsingSeparate();
if (recievedFlag) {
recievedFlag = false;
switch (thisName) {
case MOTOR1: Serial.print("motor1 "); Serial.println(prsValue);
break;
case MOTOR2: Serial.print("motor2 "); Serial.println(prsValue);
break;
case MOTOR3: Serial.print("motor3 "); Serial.println(prsValue);
break;
case MOTOR4: Serial.print("motor4 "); Serial.println(prsValue);
break;
case SETT1: Serial.print("sett1 "); Serial.println(prsValue);
break;
case SETT2: Serial.print("sett2 "); Serial.println(prsValue);
break;
}
}
}
void parsingSeparate() {
if (Serial.available() > 0) {
if (parseStage == WAIT) {
parseStage = HEADER;
prsHeader = "";
prsValue = "";
}
if (parseStage == GOT_HEADER)
parseStage = VALUE;
char incoming = (char)Serial.read();
if (incoming == divider) {
parseStage = GOT_HEADER;
}
else if (incoming == ending) {
parseStage = SUCCESS;
}
if (parseStage == HEADER)
prsHeader += incoming;
else if (parseStage == VALUE)
prsValue += incoming;
prsTimer = millis();
}
if (parseStage == SUCCESS) {
for (byte i = 0; i < headers_am; i++) {
if (prsHeader == headers[i]) {
thisName = i;
}
} recievedFlag = true; parseStage = WAIT;
} if ((millis() - prsTimer > 10) && (parseStage != WAIT)) { // таймаут
parseStage = WAIT;
}
}
You have a line after enum names that is "names thisName;" Changing that to "int thisName;" does away with your error.
I think the problem is here:
const char *headers[] = {...};
byte headers_am = sizeof(headers) / 2;
This assumes that a character pointer is always 2 bytes long. If the DUE uses 32-bit pointers then 'headers_am' will be two times too large and the loop using "byte i" will go beyond the range of the 'names' enum.
Change it to:
byte headers_am = sizeof headers / sizeof headers[0];
DangerToMyself:
You have a line after enum names that is "names thisName;" Changing that to "int thisName;" does away with your error.
It does away with the error message but does not fix the actual error.
Yes, two patches, and everything compiles
thank you very much, good luck, long life
johnwasser:
It does away with the error message but does not fix the actual error.
Interesting. Can't say I would have caught that. This - names thisName; - just looked really odd to me. Can't say I've ever seen something like that. What's the purpose? Or was it an error as well?
"nam
DangerToMyself:
Interesting. Can't say I would have caught that. This - names thisName; - just looked really odd to me. Can't say I've ever seen something like that. What's the purpose? Or was it an error as well?
enum names {
MOTOR1, // 0
MOTOR2, // 1
MOTOR3, // 2
MOTOR4, // 3
SETT1, // 4
SETT2, // 5
};
An 'enumeration' (enum) is a handy way to assign names to a small set of unique numbers (defaulting to starting at 0 and running sequentially). When you declare "thisName" to be of type "names" the compiler knows that it should contain a value from 0 to 5. When you try to assign it a value greater than 5 (like when your loop goes to 11 instead of 5) it can tell you that something is wrong with your code.
Another good use for an enum is the argument of a switch statement. That way the compiler can tell you if you try to put in a case for a value that doesn't match any member of the enumeration and warn you if there is a member for which you didn't define a case.
johnwasser:
"namenum names {
MOTOR1, // 0
MOTOR2, // 1
MOTOR3, // 2
MOTOR4, // 3
SETT1, // 4
SETT2, // 5
};
An 'enumeration' (enum) is a handy way to assign names to a small set of unique numbers (defaulting to starting at 0 and running sequentially). When you declare "thisName" to be of type "names" the compiler knows that it should contain a value from 0 to 5. When you try to assign it a value greater than 5 (like when your loop goes to 11 instead of 5) it can tell you that something is wrong with your code. Another good use for an enum is the argument of a switch statement. That way the compiler can tell you if you try to put in a case for a value that doesn't match any member of the enumeration and warn you if there is a member for which you didn't define a case.
I appreciate the information!! I read up on enum several months back and have used it to "update" some of my practice/learning codes. However, I was completely unaware that one could tie a variable to the enum as was used in the manner we're discussing. That may come in handy one day. Thanks again for the info!