Hi
(Originaly posted on SO)
In App Inventor, how do I make sure that the whole part of string is received?
I'm continously sending text commands between arduino and an android app. I use a function on arduino, which makes sure that the whole command arrived before processing it further:
(Credit: @Robin2)
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(38400);
}
void loop(){
read_serial();
}
void read_serial() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}else{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
char * strtokIndx;
strtokIndx = strtok(receivedChars,":");
int section = atoi(strtokIndx);
strtokIndx = strtok(NULL, ":");
int action = atoi(strtokIndx);
strtokIndx = strtok(NULL, ":");
int value = atoi(strtokIndx);
do_something_with(section, action, value);
}
}else if (rc == startMarker) {
recvInProgress = true;
}
}
}
Now, how to replicate similar functionality in App Inventor, to make sure that the Clock timer will not split my text commands into parts?
App Inventor (clock at 100ms):
Example:
Arduino (this part fires often):
my_function(){
Serial.print(NUMBER);
Serial.print(":");
Serial.print(ANOTHER_NUMBER);
Serial.print(":");
Serial.println(YET_ANOTHER_NUMBER);
}
And the test output in my android app:
-
- 1:3:150
-
- 1:3:150
-
- 1:3:150
-
- 1:3:1
-
- 50
-
- 1:3:150
-
- ...
As you can see, the 4'th command has been split into parts. How do I prevent this?
Kindly,
Artur.