Hello,
I have some serial data from a PSOC micro-controller in this format :
printf("z %i \n x %i \n e%i \n y %i \n",z_value,moving_mean_z,error,out).
I can change the format if that's easier
Now I want to read them from an ESP32 (Arduino IDE) so I can write them to a web app.
I want the parameters separated (variable for z, variable for x, variable for e and variable for y).
I tried to do that with this code but I get some errors :
error:'z' was not declared in this scope
error: 'x' was not declared in this scope
error: 'e' was not declared in this scope
error: 'y' was not declared in this scope
error: expected '}' at end of input
I know that they are not declared but they are chars from the Serial.read.
How can I fix this? Or is there an easier way to separate the parameters?
const unsigned int MAX_MESSAGE_LENGTH = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop(){
//const char parameter = Serial.read;
switch (Serial.read())
{
case z:
// doe iets
while (Serial.available() > 0) { //check serial buffer
static char z_value[MAX_MESSAGE_LENGTH]; // create place to store incomming message
static unsigned int z_value_pos = 0;
char inByte = Serial.read();
if(inByte != '\n' && (z_value_pos < MAX_MESSAGE_LENGTH -1)){
z_value[z_value_pos] = inByte;
z_value_pos++;
}
else{
z_value[z_value_pos] = '\0';
Serial.println(z_value);
z_value_pos = 0;
}
break;
case x:
// doe iets
while (Serial.available() > 0) { //check serial buffer
static char moving_mean[MAX_MESSAGE_LENGTH]; // create place to store incomming message
static unsigned int moving_mean_pos = 0;
char inByte = Serial.read();
if(inByte != '\n' && (moving_mean_pos < MAX_MESSAGE_LENGTH -1)){
moving_mean[moving_mean_pos] = inByte;
moving_mean_pos++;
}
else{
moving_mean[moving_mean_pos] = '\0';
Serial.println(moving_mean);
moving_mean_pos = 0;
}
break;
case e:
//doe iets
while (Serial.available() > 0) { //check serial buffer
static char error[MAX_MESSAGE_LENGTH]; // create place to store incomming message
static unsigned int error_pos = 0;
char inByte = Serial.read();
if(inByte != '\n' && (error_pos < MAX_MESSAGE_LENGTH -1)){
error[error_pos] = inByte;
error_pos++;
}
else{
error[error_pos] = '\0';
Serial.println(error);
error_pos = 0;
}
break;
case y:
// doe iets
while (Serial.available() > 0) { //check serial buffer
static char out[MAX_MESSAGE_LENGTH]; // create place to store incomming message
static unsigned int out_pos = 0;
char inByte = Serial.read();
if(inByte != '\n' && (out_pos < MAX_MESSAGE_LENGTH -1)){
out[out_pos] = inByte;
out_pos++;
}
else{
out[out_pos] = '\0';
Serial.println(out);
out_pos = 0;
}
break;
}
}
Then I get this:
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino: In function 'void loop()':
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:30:10: error: jump to case label [-fpermissive]
case 'x':
^~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:18:10: note: crosses initialization of 'char inByte'
char inByte = Serial.read();
^~~~~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:47:10: error: jump to case label [-fpermissive]
case 'e':
^~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:35:10: note: crosses initialization of 'char inByte'
char inByte = Serial.read();
^~~~~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:18:10: note: crosses initialization of 'char inByte'
char inByte = Serial.read();
^~~~~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:64:10: error: jump to case label [-fpermissive]
case 'y':
^~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:52:10: note: crosses initialization of 'char inByte'
char inByte = Serial.read();
^~~~~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:35:10: note: crosses initialization of 'char inByte'
char inByte = Serial.read();
^~~~~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:18:10: note: crosses initialization of 'char inByte'
char inByte = Serial.read();
^~~~~~
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:84:1: error: expected '}' at end of input
}
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:32:36: note: to match this '{'
while (Serial.available() > 0) { //check serial buffer
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:84:1: error: expected '}' at end of input
}
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:15:36: note: to match this '{'
while (Serial.available() > 0) { //check serial buffer
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:84:1: error: expected '}' at end of input
}
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:12:3: note: to match this '{'
{
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:84:1: error: expected '}' at end of input
}
^
C:\Users\siebe\Documents\Arduino\sketch_nov28a\sketch_nov28a.ino:8:12: note: to match this '{'
void loop(){
^
exit status 1
Compilation error: jump to case label [-fpermissive]
And it might be better to see first if there is actually a character waiting for you to read.
Your code works OK if it works because you have a switch/case with no case matching -1, the value you are probably getting most of the time: no character available, boss.
You can use Serial.available() to see if reading makes any sense.
Look at just the 'z' case. First you haven't checked to see if there is a byte available before executing the switch statement. But even if you did, your while loop will likely process just one character before exiting (because another won't be available on the next iteration), leaving any other characters unread. This means the next execution of loop() will result in reading the next character with the switch statement and it will not match any of the cases.
You need to restructure your logic.
switch (Serial.read())
{
case 'z':
// doe iets
while (Serial.available() > 0) { //check serial buffer
static char z_value[MAX_MESSAGE_LENGTH]; // create place to store incomming message
static unsigned int z_value_pos = 0;
char inByte = Serial.read();
if (inByte != '\n' && (z_value_pos < MAX_MESSAGE_LENGTH - 1)) {
z_value[z_value_pos] = inByte;
z_value_pos++;
}
else {
z_value[z_value_pos] = '\0';
Serial.println(z_value);
z_value_pos = 0;
}
}
break;
So what you mean is that he will leave the while loop as soon as the inByte changes.
How do you think I can store all the characters of 1 parameter? I have to keep reading till the \n comes. for just 1 parameter it's easy but this was the only thing I could mention for multiple parameters.
I think it would be better to read the serial until a '\n' is encountered. Then test the first character to see which data value it is. The code would be shorter and easier to 'sanity check'.