Skip to content

Instantly share code, notes, and snippets.

@pc0808f
Last active February 3, 2016 08:49
Show Gist options
  • Save pc0808f/0c0a537890e45730fd81 to your computer and use it in GitHub Desktop.
Save pc0808f/0c0a537890e45730fd81 to your computer and use it in GitHub Desktop.
gcodetest
#define MAX_BUF (64) // What is the longest message Arduino can store?
static char buffer[MAX_BUF]; // where we store the message until we get a ';'
static int sofar; // how much is in the buffer
int inByte = 0; // incoming serial byte
void setup()
{
// start serial port at 9600 bps:
Serial1.begin(9600);
}
void loop()
{
if (Serial1.available() > 0) {
// get incoming byte:
inByte = Serial.read();
if(sofar<MAX_BUF) buffer[sofar++]=inByte;
if(inByte=='\n'){
buffer[sofar]=0;
processCommand();
parser_ready();
}
}
}
void processCommand(){
long cmd;
cmd=parsenumber('M',-1);
switch(cmd){
case 104:
Serial1.println("ok");
break;
case 105:
Serial1.println("ok");
break;
case 106:
Serial1.println("ok");
break;
case 107:
Serial1.println("ok");
break;
default:
Serial1.println("error");
break;
}
}
float parsenumber(char code,float val) {
char *ptr=buffer;
while(ptr && *ptr && ptr<buffer+sofar) {
if(*ptr==code) {
return atof(ptr+1);
}
ptr=strchr(ptr,' ')+1;
}
return val;
}
void parser_ready() {
sofar=0; // clear input buffer
//last_cmd_time = millis();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment