Hello,
In my app I read the results of industrial camera inspections.
I set the format of inspections in a camera dedicated app. For example for testing I put the inspections in that way:
So I have 6 inspections here
I1 , I2 ,I3, I4,I5
where,
I1-I5 are the results which are 0 - correct inspections or '-1' - not correct
I6 is correlation so for example 84 means 84% of correlation.
My part of code which reads that is:
while (ModeInt == 1) {
if (client.available()) {
char d = client.read();
Serial.print(d);
}
}
Now I need to use these results to make something like:
For inspections 1-5
If inspection 1 is correct (0) - i turn on the LED (output)
If inspection 1 is incorrect (-1) I turn off the LED (output)
etc.
For inspection 6
I convert the result to analog output to use the signal later.
So i need to split that results to separate variables which I can convert to int to use them and I have no idea how I can do it.
Add each character to an array of chars as it arrives. When the whole message has been received terminate the array with a '\0' to turn it into a C string (not a String) and split it up using the strtok() function
the first was for reading a string and printing it when a '\n' is received.
i don't know how you tested this when you said "it prints nothing". if you're using the serial monitor, there's a setting on the bottom for selecting the line terminator. you should set it to Newline
the 2nd piece of code demonstrates the use of strtok(). it is a complete program that i tested on my laptop.
i presumed you would study it and integrate substr() into your code.
byte indx = 0;
char d[10];
if (client.available()) {
char c = client.read();
d [indx++] = c;
if ('\n' == c){
Serial.print(d);
indx = 0;}
}
I don't know what size of d should i put at the beggining maybe that is a problem.
Tried to put it to some big value like "50" and it printed nothing then.
For now i just want to print a correct value after i will try with splitting it.
Okay i found the error, I do not know why but \n doesn't work here. I must use \x0d instead
case 'a':
while (incomingByte = 'a') {
if (client.available()) {
char c = client.read();
d [indx++] = c;
if ('\x0d' == c){
Serial.print(d);
indx = 0;}
}
}
break;
Excactly i forgot that it ends with carriage return.
But trying to split it by strtok (d , ','); it gives me
invalid conversion from 'char' to 'const char*' [-fpermissive]
#setup
int indx = 0;
char d[80];
char* toks;
char* s;
int maxToks = 0;
#define MAX_TOKS 10
#loop
case 'a':
while (incomingByte = 'a') {
if (client.available()) {
char c = client.read();
d [indx++] = c;
if ('\x0d' == c) {
Serial.print(d);
// char* wynik=strtok(d,',');
int substr (
char *s,
char *toks [],
int maxToks);
int i = 0;
toks [i] = strtok (s, ',');
for (i++ ; i < maxToks && (toks [i] = strtok (NULL, ',')); i++);
return i;
char *toks [MAX_TOKS];
strcpy (d, "1, 2, 3, 4");
int n = substr((char*) d, toks, MAX_TOKS);
for (int i = 0; i < n; i++)
Serial.print (toks [i]);
indx = 0;
}
}
}
Still the same error:
invalid conversion from 'char' to 'const char*' [-fpermissive]
in line:
for (i++ ; i < maxToks && (toks [i] = strtok (NULL, ',')); i++);
Changed it to "," and :
exit status 1
invalid conversion from 'char*' to 'char' [-fpermissive]
Update:
Okay i changed the : char* wynik=strtok(d,",");
Before it was (d, ',');
Now i will try to sepearte it into 6 "values"
switch (incomingByte) {
case 'a':
while (incomingByte = 'a') {
if (client.available()) {
char c = client.read();
d [indx++] = c;
while ('\x0d' == c) {
Serial.print(d);
char* logs = strtok(d, ",");
char* separator = strchr(d, ",");
if (separator != 0;)
{
//here i need to make for example 6 values
}
}
}
}
break;