Hello.
Trying to convert code from a project that uses a Due to the 3.6.
On the 3.6 I seem to be running into issues with how I implemented my serial parse function. I've put code below. It looks like rec is not carrying over a value from CheckSerialPort to ParseUserInput when using the 3.6, whereas on the Due it does. When using the code below on the Due, when entering ParseUserInput I can see the value stored in rec and the values that are parsed. When on the Teensy I see neither.
New to Teensies so insight is very much appreciated.
Thanks for your time.
Trying to convert code from a project that uses a Due to the 3.6.
On the 3.6 I seem to be running into issues with how I implemented my serial parse function. I've put code below. It looks like rec is not carrying over a value from CheckSerialPort to ParseUserInput when using the 3.6, whereas on the Due it does. When using the code below on the Due, when entering ParseUserInput I can see the value stored in rec and the values that are parsed. When on the Teensy I see neither.
New to Teensies so insight is very much appreciated.
Thanks for your time.
Code:
const byte numChars = 128;
char user_input[numChars] = {0};
char tempUser_input[numChars] = {0};
char activityBuf[numChars] = {0};
char senBuf[numChars] = {0};
char rec;
char start = '<';
char end = '>';
bool newData = false;
int a;
int b;
int c;
int d;
int e;
int f;
int g;
void setup()
{
Serial.setTimeout(1000);
Serial.begin(115200);
}
void loop()
{
CheckSerialPort();
ParseUserInput();
}
//Checks the serial port to rx data. Non-blocking & memory safe vs Serial.readToString.
void CheckSerialPort()
{
static byte index = 0;
static boolean isReceiving = false;
while (Serial.available() > 0 && newData == false)
{
rec = Serial.read();
if (isReceiving == true)
{
if (rec != end)
{
user_input[index] = rec;
index++;
if (index >= numChars)
{
index = numChars - 1;
}
}
else
{
user_input[index] = '\0'; //terminate the string
isReceiving = false;
index = 0;
newData = true;
}
}
else if (rec == start)
{
isReceiving = true; //set flag to begin grabbing the serial data
}
if (newData == true)
{
strcpy(tempUser_input, user_input);
newData = !newData;
}
}
}
//Parses user input.
void ParseUserInput()
{
Serial.println(rec); //debug test
if (rec == end)
{
char *strtokIndex; //index for strtok()
strtokIndex = strtok(tempUser_input, "\n"); //tokenize the char array, replaces 2nd arg. w/ \0
strcpy(activityBuf, strtokIndex); //find which activity to enter
if (strcmp(activityBuf, "blahBlah") == 0)
{
strtokIndex = strtok(NULL, "\n"); //this continues from the last \n which was replaced w/ \0
a = atoi(strtokIndex);
strtokIndex = strtok(NULL, "\n");
b = atoi(strtokIndex);
strtokIndex = strtok(NULL, "\n");
c = atoi(strtokIndex);
strtokIndex = strtok(NULL, "\n");
d = atoi(strtokIndex);
strtokIndex = strtok(NULL, "\n");
e = atoi(strtokIndex);
strtokIndex = strtok(NULL, "\n");
f = atoi(strtokIndex);
strtokIndex = strtok(NULL, "\n");
g = atoi(strtokIndex);
Serial.println(a); //debug test
}
}
}