Allow PC to send serial commands to arduino.
- (c) Pieter, GPLv3
- Format {command:data}
- Edit cmds() function, at the end it matches command, and can then execute any function.
- as is will recognize {temp}, {temp:xxxx}, (test2}.
#include <Streaming.h>
//######################
void setup(void)
{
// start serial port
Serial.begin(9600);
blink(); delay(1000); blink(); delay(1000); blink(); delay(1000); blink();
};
//######################
void loop(void)
{
blink();
if (Serial.available() ) { cmds(); }
}
//######################
void blink() {
static boolean blinkLed;
static unsigned long Blinktime;
const int ledPin = 13;
const int delayMsec = 1000;
pinMode(ledPin, OUTPUT);
if ( millis() > Blinktime ) {
if (blinkLed) { digitalWrite(ledPin, HIGH); }
else { digitalWrite(ledPin, LOW); }
blinkLed = !blinkLed;
Blinktime = millis() + delayMsec;
};
}
//######################
void cmds() {
//Reads serial looking for commands in format {cmd:datadata}
char inByte; // for incoming serial data
const int maxLenCmd = 10;
const int maxLenData = 20;
const boolean debug = false;
static char cmd_command[maxLenCmd+1] = "/0"; // {xxx:
static char cmd_data[maxLenData+1] = "/0"; // yyyy}
static int cmdstate = 0; // 0=discard, 1=readcmd, 2=readdata, 3=process
static int cmdlen = 0;
int count = 0;
if (debug) {Serial << endl << "CMDS.. ";}
while (Serial.available() > 0) {
// read the incoming byte:
if (cmdstate != 3 ) { inByte = Serial.read(); count++; }
// busy reading data
if ( cmdstate == 2 ) {
if (inByte != '}') {
cmd_data[cmdlen] = inByte; cmdlen++; //add txt.
//test for overrun on cmd
if ( cmdlen >= maxLenData ) {
cmd_data[cmdlen] = 0;
Serial << "Err DATA overrun " << cmd_data << endl;
cmd_command[0] = 0; cmd_data[0] = 0; cmdlen=0; cmdstate=0;
}
}
else { //found closing bracket
cmd_data[cmdlen] = 0; cmdstate = 3; cmdlen = 0;
}
}
// busy reading command text
if ( cmdstate == 1 ) {
if ( !( (inByte==':') or (inByte=='}') or (inByte=='{')) ) {
cmd_command[cmdlen] = inByte; cmdlen++; //add txt.
//test for overrun on cmd
if ( cmdlen >= maxLenCmd ) {
cmd_command[cmdlen] = 0;
Serial << "Err CMD overrun " << cmd_command << endl;
cmd_command[0] = 0; cmdlen=0; cmdstate=0;
}
}
else { //Found controll char
if (debug) { Serial << "CMD - Found Ctrl[" << inByte << "]"; }
cmd_command[cmdlen] = 0; cmdstate = 2; cmd_data[0] = 0;
if (inByte == '}') { cmdstate = 3; } //skip data
if ( (cmdlen == 0) or (inByte == '{') ) { cmdstate = 0; } //command to short
cmdlen = 0;
}
}
// search for opening bracket
if (cmdstate==0) {
if ( inByte=='{' ) {
cmdstate = 1; cmdlen = 0;
}
else {
if (count < 2) { Serial << "Discard:"; }
Serial << (char)inByte;
}
};
if ( cmdstate == 3 ) {
if (debug) {Serial << "Command{" << cmd_command << "} Data{" << cmd_data << "}" << endl;}
// Do something !!
boolean cmdfound = false;
//strcmp returns 0 if match.
if (!strcmp(cmd_command,"temp")) { cmdfound = true;
Serial << "Run temperature read .." << cmd_data << endl;
//call custom function 1 here
}
if (!strcmp(cmd_command,"test2")) { cmdfound = true;
Serial << "Found Test2.." << cmd_data;
//call custom function2 here
}
if (! cmdfound) {
Serial << "Cmd{" << cmd_command << "} not found." << endl;
}
cmdstate = 0; cmdlen = 0 ; cmd_data[0] = 0 ; cmd_command[0] = 0;
}
// say what you got:
//Serial << "IN:" << inByte << ":";
} //while
if (debug) {Serial << endl;}
}
//######################...
