I wonder if someone could give me a head start....
Basically i have two arduinos connected to each other serially.
Im able to send basic one char characters o each other and get each arduino to act upon reciving a specific char.
Doing things this was limits me though as i can only use letters "A-Z" and numbers "0-9"
How would i go about setting something up like this:
Incoming strings:
$Start, $Stop, $Pause, The $ would tell the arduino that the incoming message os a Valid message. Thanks All.
Read each character received. Store it in the next position in an array, and append a NULL. When the end of the packet arrives, compare the string with the known values, using strcmp().
The only thing that will make this difficult for you is that you don't have an end of packet marker. You need to add one.
ok... so far i have this but it doesnt seem to work and i cant spot why?
The string that would be sent to it is: $#hello or $#byeee
What am i missing here?
Thanks
#include <string.h>
char command[6];
const int ledPin = 45; // the number of the LED pin
byte index;
char inchar;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
}
int i;
void loop() {
index = 0;
/////////////////////////////////////////////Look for $
if(Serial1.available() >0)
{
inchar=Serial1.read();
if (inchar=='
)
{
////////////////////////////////////////////// If serIn sees "$" do this
for (i=0; i < 6; i++)
{
command[i] = Serial.read();
}
}
}
/////////////////////////////////////////////// Compare entered char
if(command[0]=='#'){
if(strcmp(command, "#hello") == 0) // test to see if the two strings are equal
{
digitalWrite(ledPin, HIGH);
}
else if (strcmp(command, "#byeee") == 0) // test to see if the two strings are equal
{
digitalWrite(ledPin, LOW);
}
}
ok, i can get this working to some extent but its not what im actually after. so let me start again.
Ill be sending strings to the arduino.
I want to be able to compare a string with the arduino.
I need a start packet marker and a end packet marker.
So say my start packet marker is "$" and my end packet marker is "Enter on the keyboard aka CR"
So if any data comes into the arduino and it doesnt start with a "$" and end with "Carriage Return" it will be ignored..
This isn't a string validation issue but a serial communications issue.
It's a VERY common issue.
This is currently 1 of 3 separate threads on this issue on the first page of this forum.
There are literally dozens of threads on this issue regurgitating the same solutions ad nauseum.
A little searching, a lot of reading, and you'll see that the answers to your question already exist.
/////////////////////////////////////////////Look for $
if(Serial1.available() >0)
{
inchar=Serial1.read();
if (inchar=='
What you've told it here is that as soon as there is at least a single character in the buffer, read it and check if it's a '$'. Then you say read whatever else is in the buffer, even if there is nothing there. Since there is no delay, it will run well before the second byte reaches the arduino, meaning it wouldn't work.
Instead, on every iteration of loop(), read just a single character. If it's the start marker, "reset" your char array. If it's the end packet, null terminate it and do what you want to it, otherwise, add it to your char array.)
{
////////////////////////////////////////////// If serIn sees "$" do this
for (i=0; i < 6; i++)
{
command[i] = Serial.read();
}
What you've told it here is that as soon as there is at least a single character in the buffer, read it and check if it's a '$'. Then you say read whatever else is in the buffer, even if there is nothing there. Since there is no delay, it will run well before the second byte reaches the arduino, meaning it wouldn't work.
Instead, on every iteration of loop(), read just a single character. If it's the start marker, "reset" your char array. If it's the end packet, null terminate it and do what you want to it, otherwise, add it to your char array.
If it's the start marker, "reset" your char array. If it's the end packet, null terminate it and do what you want to it, otherwise, add it to your char array.
How do i add a delay in here
if(Serial1.available() >0)
{
inchar=Serial1.read();
if (inchar=='
and how do i reset my char array and terminate the end packet?
Thanks)
{
delay(1000);
and how do i reset my char array and terminate the end packet?
Thanks
You don't, there is no need for delays in this code. You just let it run constantly and every time it sees a byte in the Serial buffer, it reads and and decides what to do with it.
and how do i reset my char array and terminate the end packet?
You need a variable to keep track of where you are in the char array. Whenever you add a character to the array, you increment that index variable. To "reset" it, simply set the index variable back to 0. To terminate it, add a null terminated byte to the end of the char array.
I have saved this sketch that PaulS has created for serial comms. I call it serial_comms_perfect because for me it is perfect.
You will need to change your SOP and EOP...packet markers. You will also need to add your compare conditionals. You might need other changes that I have not spotted.
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(9600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
Serial.print(inData);
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
jstone:
Basically i have two arduinos connected to each other serially.
Im able to send basic one char characters o each other and get each arduino to act upon reciving a specific char.
Doing things this was limits me though as i can only use letters "A-Z" and numbers "0-9"
You might want to use unprintable characters as your markers. ASCII STX (start transmission) and ETX (end transmission) are standards since way back (60's?) as characters 2 and 3. Printable characters don't start until 'spacebar' as character 32.
It may be easier to use Serial.write( value ) like this:
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.write(45); // send a byte with the value 45
int bytesSent = Serial.write(“hello”); //send the string “hello” and return the length of the string.
}
And then you can send all the printables including '$' back and forth.