Hi,
I'm sending a chain 'azerty' for example from my pc to the arduino.
All is Ok except that the arduino receives it character by character. what is the good way to have the whole 'azerty' in one time or to reconstruct it ?
Thanks a lot
seb
Read a text received on serial arduino port ?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: Read a text received on serial arduino port ?
something like thisand later on a little of thiswill read a string off the serial port and finish when it has received 16 characters or a carriage return. the result will be in a nice null terminated character array suitable for use with c library string functions.
Code: Select all
#define INLENGTH 16
#define INTERMINATOR 13
char inString[INLENGTH+1];
int inCount;
Code: Select all
inCount = 0;
do {
while (!Serial.available()); // wait for input
inString[inCount] = Serial.read(); // get it
if (inString [inCount] == INTERMINATOR) break;
} (++inCount < INLENGTH));
inString[inCount] = 0; // null terminate the string
-
- Posts: 2
- Joined: Wed Sep 28, 2011 10:35 am
Re: Read a text received on serial arduino port ?
Dear mtbf0,
I want to do the exact same thing as freeseb. But the problem is that for some reason your code is not right, and i can't compile it.
The arduino IDE says:
"sketch_sep28c.cpp: In function 'void loop()':
sketch_sep28c:17: error: expected `while' before '(' token
sketch_sep28c:17: error: expected `;' before ')' token
sketch_sep28c:17: error: expected primary-expression before ')' token
sketch_sep28c:17: error: expected `;' before ')' token"
Line17 is: "if (inString [inCount] == INTERMINATOR) break;"
Please help me by fixing the code above.
And if you could I would like you to help me by telling me how I can compare the "text" that i receive to another string. Let's suppose I receive something and I want to check if the text I received is "OK". I want to know how to check this. I am developing a project for my university and I've been looking for this answer for about 10 days. I can only receive the first character.
Please help...
I want to do the exact same thing as freeseb. But the problem is that for some reason your code is not right, and i can't compile it.
The arduino IDE says:
"sketch_sep28c.cpp: In function 'void loop()':
sketch_sep28c:17: error: expected `while' before '(' token
sketch_sep28c:17: error: expected `;' before ')' token
sketch_sep28c:17: error: expected primary-expression before ')' token
sketch_sep28c:17: error: expected `;' before ')' token"
Line17 is: "if (inString [inCount] == INTERMINATOR) break;"
Please help me by fixing the code above.
And if you could I would like you to help me by telling me how I can compare the "text" that i receive to another string. Let's suppose I receive something and I want to check if the text I received is "OK". I want to know how to check this. I am developing a project for my university and I've been looking for this answer for about 10 days. I can only receive the first character.
Please help...
Code: Select all
#define INLENGTH 16
#define INTERMINATOR 13
char inString[INLENGTH+1];
int inCount;
void setup() {
}
void loop(){
inCount = 0;
do {
while (!Serial.available()); // wait for input
inString[inCount] = Serial.read(); // get it
if (inString [inCount] == INTERMINATOR) break;
} (++inCount < INLENGTH));
inString[inCount] = 0; // null terminate the string
}
- adafruit_support_bill
- Posts: 89346
- Joined: Sat Feb 07, 2009 10:11 am
Re: Read a text received on serial arduino port ?
Some small syntax errors there. Try this:
Code: Select all
#define INLENGTH 16
#define INTERMINATOR 13
char inString[INLENGTH+1];
int inCount;
void setup() {
}
void loop(){
inCount = 0;
do
{
while (!Serial.available()); // wait for input
inString[inCount] = Serial.read(); // get it
if (inString [inCount] == INTERMINATOR) break;
} while(true);
(++inCount < INLENGTH);
inString[inCount] = 0; // null terminate the string
}
Use strncmp() http://www.cplusplus.com/reference/clib ... g/strncmp/how I can compare the "text" that i receive to another string.
-
- Posts: 2
- Joined: Wed Sep 28, 2011 10:35 am
Re: Read a text received on serial arduino port ?
Can you please add to the code the Serialprint command to print the whole "word" that was received through the RXD pin?
So if it has received the word "TEST", I need it to be printed on my Serial Monitor.
So if it has received the word "TEST", I need it to be printed on my Serial Monitor.
- adafruit_support_bill
- Posts: 89346
- Joined: Sat Feb 07, 2009 10:11 am
Re: Read a text received on serial arduino port ?
We can point you in the right direction, but we can't write your code for you. Serial.println() is the function you need.
If you are new to programming, you may want to start with the Arduino Tutorials for some step-by-step instructions.
If you are new to programming, you may want to start with the Arduino Tutorials for some step-by-step instructions.
- pockybum522
- Posts: 12
- Joined: Wed Oct 19, 2011 1:00 pm
Re: Read a text received on serial arduino port ?
My apologies for posting to an old thread, but this is the top hit on google for what I was searching and I wanted to share with others my heavily, heavily modified code I found http://fayazkadir.com/blog/?p=2291 < There.
I ended up coming up with something you should be able to plug right into your project and have string reading capabilities over serial. I just tested this with the duemilanove and serial monitor to confirm it's working.
If you're using the arduino serial monitor, be sure to change no line ending to carriage return and 9600 baud.
To read strings:
I ended up coming up with something you should be able to plug right into your project and have string reading capabilities over serial. I just tested this with the duemilanove and serial monitor to confirm it's working.
If you're using the arduino serial monitor, be sure to change no line ending to carriage return and 9600 baud.
To read strings:
Code: Select all
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // Initialize serial port
}
void loop()
{
serialReader();
}
void serialReader(){
int makeSerialStringPosition;
int inByte;
char serialReadString[50];
const int terminatingChar = 13; //Terminate lines with CR
inByte = Serial.read();
makeSerialStringPosition=0;
if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)
while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
makeSerialStringPosition++; //Increment position in array
//if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
inByte = Serial.read(); // Read next byte
}
if (inByte == terminatingChar) //If we terminated properly
{
serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
Serial.println(serialReadString);
if (strcmp(serialReadString, "LEDOn") == 0) digitalWrite(13, HIGH);
if (strcmp(serialReadString, "LEDOff") == 0) digitalWrite(13, LOW);
}
}
}
- ygreq
- Posts: 54
- Joined: Mon May 02, 2011 11:17 am
Re: Read a text received on serial arduino port ?
Look what I found:
Thanks to PaulS on arduino.cc forum
Code: Select all
boolean started = false;
boolean ended = false;
char inData[24]; // Size as appropriate
byte index = 0;
#define SOP "{"
#define EOP "}"
void loop()
{
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 24-1) // Array size
{
inData[index++] = inChar;
inData[index] = '\0';
}
}
}
if(started && ended)
{
// Parse the data in inData here...
}
}
Please be positive and constructive with your questions and comments.