cabecinhas:
Hi… I’m trying to create a string of characfters to extrat each value but its not working… Im doing something wrong…
Here is a piece of code from a project I did a few years ago. It accepts commands via the serial port and executes the appropriate command.
Optionally, parameters can be specified. For example, you can type:
command
or
command, parameter
The code isn’t complete for your use obviously, you’ll have to edit it, but it should get you going in the right direction.
Hope this helps.
#define buffer_size 32
void setup (void) /* this runs once at Arduino reset */
{
Serial.begin (115200); /* setup serial port baud rate */
}
void loop (void) /* this runs continuously */
{
get_Command(); /* get command and run it */
}
/* table of commands we recognize - upper and lower case INPUT both match this table
so be sure to enter all commands to be found in UPPERCASE */
const char *cmd_table[] = {
"FAN", /* 0 */
"RPM", /* 1 */
"FANSW", /* 2 */
"LIGHTSW", /* 3 */
"ECHO", /* 4 */
"REPLY", /* 5 */
"HELP", /* 6 */
/* go ahead, add more commands! */
};
/* sprintf buffer and line input */
char buffer [buffer_size];
/* search command table and find a match with the line input buffer */
void get_Command (void)
{
int n, x, err;
int line_len;
int cmd_len;
int cmd_count;
if (! (cmd_len = line_len = read_line (buffer, buffer_size))) {
return;
}
/* how many commands are in the command table */
cmd_count = (sizeof (cmd_table) / sizeof (cmd_table[0]));
/* find a comma (command / parameter separator) if present */
for (n = 0; n < line_len; n++) {
if (buffer[n] == ',') {
cmd_len = n; /* when done, cmd_len is the length of the command ONLY */
break;
}
}
for (n = 0; n < cmd_count; n++) {
err = 0;
for (x = 0; x < cmd_len; x++) {
if (cmd_table[n][x] != buffer[x]) {
err++;
}
}
/* if command matched the table and is the same length, then it's valid */
if ( (!err) && (cmd_len == strlen (cmd_table[n]))) {
parse_cmd (n, buffer);
break;
}
}
}
/* extract the numeric part of the command (if any) */
void parse_cmd (int cmd, char *param)
{
char *ptr = param;
int n;
long int value;
int param_len;
param_len = strlen (param);
for (n = 0; n < param_len; n++) {
ptr++;
if (param[n] == ',') {
break; /* found a comma, we're done */
}
}
/* here, ptr points to the parameter (if it exists) ONLY */
if (strlen (ptr) == 0) {
value = -1; /* -1 means "no parameter found" */
} else {
value = atoi (ptr); /* parameter is numeric */
}
exec_cmd (cmd, value); /* call main code (if param == -1 then no param was specified) */
}
/* call the parsed command and pass it the param (if there was one) */
void exec_cmd (int cmd, long int param)
{
/* call the appropriate function for each command */
switch (cmd) {
case 0:
cmd_0 (cmd, param);
break;
case 1:
cmd_1 (cmd, param);
break;
case 2:
cmd_2 (cmd, param);
break;
case 3:
cmd_3 (cmd, param);
break;
case 4:
cmd_4 (cmd, param);
break;
case 5:
cmd_5 (cmd, param);
break;
case 6:
cmd_6 (cmd, param);
break;
default:
break;
}
}
/* read a line from user into buffer, return char count */
int read_line (char *buf, int limit)
{
int x;
int ptr = 0;
buf[0] = 0; /* clear first byte of buffer */
if (!Serial.available()) {
return 0; /* no data, exit */
}
while (1) {
if (Serial.available()) {
x = toupper (Serial.read()); /* uppercase everything */
if (x == 0x0D) { /* carriage return means done typing */
if (echo) {
Serial.print ("\r\n"); /* echo a newline */
}
buf[ptr] = 0; /* mark end of string */
return ptr; /* ptr equals character count */
}
if (x == 0x08) { /* backspace */
if (ptr > 0) { /* if not already at the start of the line... */
ptr--; /* back up pointer */
buf[ptr] = 0; /* erase bs'd character */
if (echo) {
Serial.print ( (char) 0x08); /* erase bs'd char on the terminal */
Serial.print ( (char) 0x20);
Serial.print ( (char) 0x08);
}
}
} else {
if ( (ptr < (limit - 1)) && (x != ' ')) {
if (echo) {
Serial.print ( (char) x); /* echo received char */
}
buf[ptr] = x;
ptr++;
}
}
}
}
}