I need do something like this...
void setup(){
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
String c = Serial.read();
if (c == "Ping"){
Serial.println("Ok");
}
}
}
But cant compile... need help...
I need do something like this...
void setup(){
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
String c = Serial.read();
if (c == "Ping"){
Serial.println("Ok");
}
}
}
But cant compile... need help...
Hello you can't do like that.Serial.read() returns to a byte so reading it to a string variable it wont work on the right way.you have to create an array for incoming data and then read every byte on serial port to its array element.
I also suggest you use a char data type and at the end just terminate the array using "\0" on the pre last element.This will give you a String and then you can compare it
Read this http://arduino.cc/en/Reference/String
You can only read one character at a time from the serial port. What you'll want to do is terminate your string with a flag. Endline ('\n') works well for this in most situations.
Here's the general process:
Here's a piece of some code i wrote for a previous project, it will give you something to play with:
#define CMDBUFFER_SIZE 32
void setup()
{
Serial.begin(9600);
}
void loop()
{
}
void serialEvent()
{
static char cmdBuffer[CMDBUFFER_SIZE] = "";
char c;
while(Serial.available())
{
c = processCharInput(cmdBuffer, Serial.read());
Serial.print(c);
if (c == '\n')
{
Serial.println();
//Full command received. Do your stuff here!
if (strcmp("HELLO", cmdBuffer) == 0)
{
Serial.println("\r\nYou typed hello!");
}
cmdBuffer[0] = 0;
}
}
delay(1);
}
char processCharInput(char* cmdBuffer, const char c)
{
//Store the character in the input buffer
if (c >= 32 && c <= 126) //Ignore control characters and special ascii characters
{
if (strlen(cmdBuffer) < CMDBUFFER_SIZE)
{
strncat(cmdBuffer, &c, 1); //Add it to the buffer
}
else
{
return '\n';
}
}
else if ((c == 8 || c == 127) && cmdBuffer[0] != 0) //Backspace
{
cmdBuffer[strlen(cmdBuffer)-1] = 0;
}
return c;
}
Edit: This will echo everything back to you. It will look funny in the IDE serial monitor, but in putty or hyperterminal it makes more sense.
The example assumes that there is a known sequence (characters) serving as carriage return. If there is no such sequence, it becomes a little harder, but one possible solution is to work with a timer that is started in a serialEvent().
simple serial string capture using a , as a string delimiter.
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
thanks all...
zoomkat:
simple serial string capture using a , as a string delimiter.if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
I am using a this method to receive a string in the arduino i would like to check a text file for this string but cant get it to work.
It won't accept the string in the function. Is there a way i could compare these to variables?
char buf[11];
String inputString="";
myFile.read(buf,11);
if(strncmp(buf, inputString, 9) == 0)
{
Serial.println("match");
break;
}
I am using a this method to receive a string
No, you are NOT. You are receiving a String. NOT the same thing AT ALL.
So, quit that crap. Collect the data in a NULL terminated array of chars, also known as a string.
Hello, I read the method to compare two strings. But it takes a sudden know the chains, I wanted to know if it was possible to compare strings but without knowing the chain will be read?
So I read a first time chain, I have stock. Then I read the second frame and I compare with the previous.
But it takes a sudden know the chains
All the words are spelled correctly, but the arrangement makes no sense.
I wanted to know if it was possible to compare strings but without knowing the chain will be read?
It is possible to compare strings. It is not possible to compare a string and a String, except character be character.
So I read a first time chain, I have stock.
All the words are spelled correctly, but the arrangement makes no sense.
If you compile zoomkat's code, it takes 3298 bytes. By replacing the String object with a char array (i.e., a C string--note lowercase):
//econjack 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
char readString[50];
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
char *ptr;
int charsRead;
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
charsRead = Serial.readBytesUntil('\n', readString, sizeof(readString) - 1); //read entire line
readString[charsRead] = '\0'; // Make it a C string
ptr = readString; // point to the start of the string
int limit = strlen(readString);
for (int i = 0; i <= limit; i++) {
if (readString[i] == ',') { // We found a delimiter
//do stuff
readString[i] = '\0'; // Overwrite the comma
Serial.println(ptr); //prints string to serial port out
ptr = &readString[i + 1]; // Look at next part of input string
}
}
}
}
the code size drops to 2150 bytes. The String class is easy to use, but at the expense of additional memory plus the possibility of memory fragmentation.
Yes character be character.
I would compare an unknown frame until a new frame be read.
The example assumes that there is a known sequence (characters)
I do not know the frames
I read first frame , I put D0 HIGH , and repeat up to received frame different so I put D1 HIGH.
Thx Pauls
I use tgm1175 code , but I know the frame to compare.
If I did not know how to compare frames in succession ?
#include <Ethernet.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0xFC, 0x17};
IPAddress ip(192,168,23,154);
IPAddress server(192,168,23,153);
EthernetClient client;
void setup()
{
pinMode (CONTROLLINO_D0, OUTPUT);
pinMode (CONTROLLINO_D1, OUTPUT);
Ethernet.begin(mac, ip);
Serial.begin(57600);
while (!Serial){;}
delay(1000);
Serial.println("connexion...");
if (client.connect(server,2111))
{
Serial.println("connecte");
}
else
{
Serial.println("connexion echoue");
}
}
void loop()
{
static char cmdBuffer[CMDBUFFER_SIZE] = "";
char c;
while(client.available())
{
c = processCharInput(cmdBuffer, client.read());
Serial.print(c);
if (c == '\n')
{
Serial.println();
if (strcmp("6047078288", cmdBuffer) == 0)
{
digitalWrite(CONTROLLINO_D0,HIGH);
digitalWrite(CONTROLLINO_D1,LOW);
digitalWrite(CONTROLLINO_D2,LOW);
else if (strcmp("NOREAD", cmdBuffer) == 0)
{
digitalWrite(CONTROLLINO_D1,LOW);
digitalWrite(CONTROLLINO_D0,LOW);
digitalWrite(CONTROLLINO_D2,HIGH);
}
else // sinon
{
digitalWrite(CONTROLLINO_D0,LOW);
digitalWrite(CONTROLLINO_D1,HIGH);
digitalWrite(CONTROLLINO_D2,LOW);
}
cmdBuffer[0] = 0;
}
}
//delay(1);
}
char processCharInput(char* cmdBuffer, const char c)
{
if (c >= 32 && c <= 126)
{
if (strlen(cmdBuffer) < CMDBUFFER_SIZE)
{
strncat(cmdBuffer, &c, 1);
}
else
{
return '\n';
}
}
else if ((c == 8 || c == 127) && cmdBuffer[0] != 0)
{
cmdBuffer[strlen(cmdBuffer)-1] = 0;
}
return c;
}
HoltOne:
I use tgm1175 code , but I know the frame to compare.If I did not know how to compare frames in succession ?
What do you mean when you use the word "frame" ?
...R
frame = sequence , sorry ...
I Know sequence compare , it is " 6047078288 "
but if I do not know the sequence , how to compare with the following ?
HoltOne:
frame = sequence , sorry ...I Know sequence compare , it is " 6047078288 "
but if I do not know the sequence , how to compare with the following ?
I'm a little clearer - but things are still hazy. Don't you need to know one of the sequences (strings) in order to do any comparison?
Maybe the strstr() function would be useful. It finds one string within another.
...R
I read a sequence using a reader. This sequence I want to save and when the second sequence happens I compare it with the first .
Then I must have two buffer ?
I have write code :
void loop()
{
static char buffer1[buffer_SIZE];
static char buffer2[buffer_SIZE];
short *b1;
short *b2;
b1 = buffer1;
b2 = buffer2;
char c;
while(client.available())
{
c = processCharInput(buffer1, client.read());
Serial.print(c);
if ( c == '\n')
{
Serial.println();
memcpy(buffer2, buffer1, 10*sizeof(short));
}
short *inv;
inv = b1;
b1 = b2;
b2 = inv;
if (strcmp(buffer1, buffer2) == 0)
{
digitalWrite(CONTROLLINO_D0, HIGH);
}
}
I'm still struggling to understand.
Do you mean that you want to receive a sequence of characters and store it in a variable (a char array).
Then later you want to compare another sequence of characters to see if it is the same as the one you have saved?
If so I believe the strstr() function that I linked to in Reply #15 is what is needed.
If I have not understood what you want please provide a more comprehensive description - perhas with some examples.
Where do the sequences of characters come from?
What is the purpose of your project?
...R
HoltOne:
I read a sequence using a reader. This sequence I want to save and when the second sequence happens I compare it with the first .Then I must have two buffer ?
Yes, you need two buffers. Your use of strcmp is correct. Is there a problem? If so, please explain and provide full code.
@Robin2 is exactly right !
So I have a data matrix code reader that reads a sequence whenever a code is read by a sensor
The sequences are always 10 characters
Example " 6521985423 "
But I do not know the sequences normally , and so when this sequence is always the same so I continued to do the same action, but when the sequence changes I have to make another action.
That's why I try to make a comparison with two sequences that I do not know the departing
@sterretje my first code it is
#include <Ethernet.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0xFC, 0x17};
IPAddress ip(192,168,23,154);
IPAddress server(192,168,23,153);
EthernetClient client;
void setup()
{
pinMode (CONTROLLINO_D0, OUTPUT);
pinMode (CONTROLLINO_D1, OUTPUT);
Ethernet.begin(mac, ip);
Serial.begin(57600);
while (!Serial){;}
delay(1000);
Serial.println("connexion...");
if (client.connect(server,2111))
{
Serial.println("connecte");
}
else
{
Serial.println("connexion echoue");
}
}
void loop()
{
static char cmdBuffer[CMDBUFFER_SIZE] = "";
char c;
while(client.available())
{
c = processCharInput(cmdBuffer, client.read());
Serial.print(c);
if (c == '\n')
{
Serial.println();
if (strcmp("6047078288", cmdBuffer) == 0)
{
digitalWrite(CONTROLLINO_D0,HIGH);
digitalWrite(CONTROLLINO_D1,LOW);
digitalWrite(CONTROLLINO_D2,LOW);
else if (strcmp("NOREAD", cmdBuffer) == 0)
{
digitalWrite(CONTROLLINO_D1,LOW);
digitalWrite(CONTROLLINO_D0,LOW);
digitalWrite(CONTROLLINO_D2,HIGH);
}
else // sinon
{
digitalWrite(CONTROLLINO_D0,LOW);
digitalWrite(CONTROLLINO_D1,HIGH);
digitalWrite(CONTROLLINO_D2,LOW);
}
cmdBuffer[0] = 0;
}
}
//delay(1);
}
char processCharInput(char* cmdBuffer, const char c)
{
if (c >= 32 && c <= 126)
{
if (strlen(cmdBuffer) < CMDBUFFER_SIZE)
{
strncat(cmdBuffer, &c, 1);
}
else
{
return '\n';
}
}
else if ((c == 8 || c == 127) && cmdBuffer[0] != 0)
{
cmdBuffer[strlen(cmdBuffer)-1] = 0;
}
return c;
}
It is correct but in the event I know the sequence, whereas I do not have the know