command from seial port

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*
  • }*
    any ideas & code ideas please

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.

Thanks for the info.

Ive never been into c or c++ , Im move into VB and VB.net .

Can`t find any info about string manipulation in the Arduino Reference (extended) .

So dont know if strcmp and strstr are common to C etc and Im expected to know it.

Is there any notes about this and just not found it ?.

Thank again for help ... Back to coding again ...

'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.

Thanks again, I know have some thing to read and understand.

http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html#g46f3cbd2de457c0fb340a1f379fc33ba

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

I`ve found that you can find if two string equal with :-

if ( strcmp(inString, "reset") == 255 )

But :-
if ( strstr(inString, "reset") != NULL ) will say if it contains the string .

Yes, strstr can be a problem if the command 'tag' string can also be embedded in the message.

strcmp returns 0 when the strings are equal, not 255

SHOULD !!!

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

here is a simple test:

char str1[] = "reset";
char str2[] = "res";
char str3[] = "reseting";

void setup() {
  Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps

  Serial.println( strcmp(str1, "reset" ));
  Serial.println( strcmp(str2, "reset" ));
  Serial.println( strcmp(str3, "reset" ));
}

void loop() {
}

here is the output:
0
-101
105

Strcmp returns zero when equal, negative when less and positive when greater

Peter, the value of the string "reset" is
{ 'r', 'e', 's', 'e', 't', 0 };

Your buffer inString contains
{ 'r', 'e', 's', 'e', 't', 255, 255, 255, ...}

These are not the same, and that's why strcmp reports them as different.

Add this line to the bottom of your test program for verification:

Serial.println(inString);

Mikal

Yes , I get the same .

Just checking , how should a string be terminated ?.

I get reset(-1), should I be adding a extra 0 instead of the -1.

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.

Mikal

thanks
I have found the fault in my program , which like you say is due to not terminating my string.

eg after I see -1 I just add 0 byte to the end.

sort of a new idea , but you know how to remove or start for the end of the string
eg *time 12:12:12

find the line does include time, which I know how to do .
How copy " 12:12:12" to a second string ?

by the way there is also strcasecmp !!! don`t care the case.

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'.

Thanks again.

More tools in my programming bank , only need to know how to use them.

mem:
Processing uses java, the arduino uses C/C++. Java has a superficial similarity to C++ but that code will not work on the Arduino.

Why not? Aside from the fact that substring is not available on the Arduino, are there any other reasons?