How do you read the serial port and make a series of commands form it.
Eg if (string == “????”) do this , if (string == “??#?”) then do this.
I`ve found this code to make one command:
if ( Serial.available() > 0) { // if there are bytes waiting on the serial port
char inByte = Serial.read(); // read a byte
delay(10); // *** added to make it work ***
if (inByte == '*') { // if that byte is the desired character
int len = 5; // expected string is 6 bytes long
char inString[len]; // declare string variable
for (int i = 0; i < len; i++) {
inString = Serial.read(); }
if ( strstr(inString, "reset") != NULL ) // check to see if the respose is "reset"*
resetChip(); // reset the chip after waiting for the specified # of milliseconds*
You have already got the code that you need, in that example. You just accumulate characters into a character array of suitable size (don't run off the end!) and then compare the string with the commands you want to execute. Using 'strstr' isn't the best way, since you also have 'strcmp' in the C library for a direct comparison. When 'strcmp' returns zero, the two strings match:
if (strcmp (cmd, "dothis") == 0) {
//perform relevant action here
}
Remember to handle any 'newline' characters properly, that is, terminate your character-acquiring code when you get one.
'strcmp', 'strcat' and 'strlen' are all part of the C Standard Library, and have been since the early days of C. In ordinary C programming, there's a header file 'string.h' that includes prototypes for these functions.
Not sure where the exact correspondence between C/C++ and Arduino sketch code is defined.
Some code which will let you do exactly this. It's in Processing, but it will work on an Arduino if you just fix the serial stuff and take out the drawing bits and pieces.
import processing.serial.*;
Serial port;
String buff = "";
int NEWLINE = 10;
void setup()
{
size(512, 256);
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
background(53);
stroke(255);
while (port.available() > 0)
serialEvent(port.read());
}
void serialEvent(int serial)
{
if (serial != NEWLINE) {
// Store all the characters on the line.
buff += char(serial);
}
else {
// The end of each line is marked by two characters, a carriage
// return and a newline. We're here because we've gotten a newline,
// but we still need to strip off the carriage return.
buff = buff.substring(0, buff.length()-1);
// parse buff here and do whatever you want
buff = "";
}
}
That Processing code uses the C++ object type "String", which may well use dynamic memory allocation (malloc/free or new/delete). It may use up the Arduino's memory rather quickly! Best to use ordinary statically-sized character arrays in Arduino code.
Processing uses java, the arduino uses C/C++. Java has a superficial similarity to C++ but that code will not work on the Arduino.
Code using the C function strcmp should work but its simpler to use single letter commands if you are devising your own command structure.
There is an example of this here: Arduino Playground - Servo
strcmp returns 0 when the strings are equal, not 255
I tried making a test program to see why the logic don`t work.
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.flush();
}
void loop() {
if ( Serial.available() > 0) { // if there are bytes waiting on the serial port
char inByte = Serial.read(); // read a byte
delay(10); // *** added to make it work ***
if (inByte == '*') { // if that byte is the desired character
int len = 15; // expected string is 6 bytes long
char inString[len]; // declare string variable
for (int i = 0; i < len; i++) {
delay(5);
inString = Serial.read();
}*
int j=0;*
do*
{*
Serial.print(inString[j], DEC);*
Serial.print(" - ");*
Serial.println(inString[j]);*
j++;*
} while ( inString[j] != -1 );*
Serial.println( strcmp(inString, "reset" ));
if ( strcmp(inString, "reset") == 255 ) *
{*
Serial.println("reset");*
} *
}}}* typed and sent *reset and got back:- 114 - r 101 - e 115 - s 101 - e 116 - t 255 reset
C/C++ strings are always terminated by a 0. Library functions like strcmp depend on this. That's the only way they know when the comparison should stop!
You are getting the -1 (a.k.a 255) in your buffer because you call Serial.read() 15 times, and when there is nothing there to read, the function returns -1.
You can copy from one (sub-)string to another with the 'strcmp' function. Make a character pointer that points into the "middle" of a string, then use that as an argument to 'strcpy'.