Commit 81c66e85 authored by Evan Raskob's avatar Evan Raskob

merged example with other serial strings example and rationalised name

parent e903d5ce
......@@ -7,15 +7,14 @@
* Then, what data do you want from it? And how often?
*/
const char stopChar = '\n'; //new line character
const char stopChar = '\n'; //new line character
const long timeoutMs = 400; // timeout if message took too long
const long timeoutMs = 400; // timeout if message took too long
// if matching an exact message:
// if matching an exact message:
const char LEDONCommand[] = "ledon";
const char LEDOFFCommand[] = "ledoff";
long bytesReceived = 1;
/**
......@@ -37,23 +36,27 @@ void handleMessage(char *message)
{
// handle the command.. blink or something?
if (strcmp(message,LEDONCommand) == 0) {
digitalWrite(LED_BUILTIN,HIGH);
if (strcmp(message, LEDONCommand) == 0)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else if (strcmp(message,LEDOFFCommand) == 0) {
digitalWrite(LED_BUILTIN,LOW);
else if (strcmp(message, LEDOFFCommand) == 0)
{
digitalWrite(LED_BUILTIN, LOW);
}
else {
for (int i=0; i<4; i++)
else
{
// flash error lights
for (int i = 0; i < 4; i++)
{
blink(LED_BUILTIN,80);
blink(LED_BUILTIN, 30);
}
}
////////////////////////////////////////////////
// useful for debugging - echo back what we got
Serial.print('[');
int msgLen = strlen(message);
for (int i=0; i< msgLen; i++)
for (int i = 0; i < msgLen; i++)
{
Serial.print(message[i]);
}
......@@ -68,99 +71,95 @@ void setup()
//
char cmdTest[] = {'l', 'e', 'd', 'o', 'n', '\0'};
// test:
if (strcmp(cmdTest, LEDONCommand) == 0)
{
for (int i=0; i<4; i++)
for (int i = 0; i < 4; i++)
{
blink(LED_BUILTIN,400);
blink(LED_BUILTIN, 400);
}
}
// set this as high as you can reliably run on your platform
Serial.begin(38400);
Serial.begin(38400);
// wait for serial port to be ready
while (!Serial);
while (!Serial)
;
}
const unsigned int bufferSize = 256; // number of bytes we can read
char buffer[bufferSize];
int charsRead = 0; // amount of bytes we've read
void loop()
{
// this returns the number of bytes available to read
const int totalBytesToRead = Serial.available();
const long startTime = millis(); // time we started checking for serial
if (totalBytesToRead > 0)
// we need this while loop to fully empty the serial buffer
while (Serial.available())
{
Serial.print("bytes left:");
Serial.println(totalBytesToRead);
int bytesToRead = totalBytesToRead;
const long startTime = millis(); // time we started checking for serial
// stop if we've taken too much time!
if (millis() - startTime > timeoutMs)
{
bytesToRead = 0;
// create empty message, leave room for 1 extra byte (string terminator) just in case
char messageReceived[totalBytesToRead+1];
// flash error lights
for (int i = 0; i < 4; i++)
{
blink(LED_BUILTIN, 30);
}
break;
}
boolean error = true; // might not get a full message...
char charRead = (char)Serial.read();
while (bytesToRead > 0)
// be careful not to go over the buffer size (with room
// for string terminator character)!
// note that this doesn't stop the loop - we ignore
// extra bytes
if (charsRead < bufferSize)
{
// read the current serial values into the buffer, in order
// this starts at 0 and ends at the length of our buffer - 1
int index = totalBytesToRead - bytesToRead;
if (charRead != stopChar)
{
buffer[charsRead] = charRead;
charsRead = charsRead + 1;
// for testing:
// Serial.print(charsRead);
}
else
{
Serial.println("process command[");
Serial.print(charsRead);
Serial.print("]\n");
// null terminator to make a proper string:
// https://www.tutorialspoint.com/cprogramming/c_strings.htm
bytesReceived++; // debugging - keep track of chars read
buffer[charsRead] = '\0'; // terminate string
char newChar = (char)Serial.read();
//we've consumed one byte from the serial buffer this loop:
bytesToRead = bytesToRead - 1;
// process results
Serial.print(':');
Serial.print(buffer);
Serial.println(':');
// stop if we've taken too much time!
if (millis() - startTime > timeoutMs){
bytesToRead = 0;
break;
}
// now handle bytes read
handleMessage(messageReceived);
// check if we've received the stop character
if (newChar == stopChar)
{
// echo it back
// char echoStr[index+2];
// strcpy(echoStr, messageReceived);
// echoStr[index+1] = '\0';
// Serial.println(echoStr);
// handle it
error = false;
// change end character to string terminator
messageReceived[index] = '\0'; // null terminator to make a proper string: https://www.tutorialspoint.com/cprogramming/c_strings.htm
bytesToRead = 0;
break;
}
// otherwise, just add it to the buffer
else {
messageReceived[index] = newChar;
charsRead = 0; // go back to beginning of buffer for next command
}
// finally, keep going.
}
if (error)
// otherwise too many bytes!
else
{
for (int i=0; i<4; i++)
Serial.print("looped");
// loop back around, so we drain all serial data waiting
charsRead = 0;
// flash error lights
for (int i = 0; i < 4; i++)
{
blink(LED_BUILTIN,30);
blink(LED_BUILTIN, 30);
}
}
// success! do something:
else {
// note: we might want to do this in two stages, parse first then handle
handleMessage(messageReceived);
// debugging - optional
Serial.println(bytesReceived);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment