Dear all,
I am trying to communicate the arduino serially through usb communication by sending a character.
For using forms I used this link (very helpfull) - http://csharp.simpleserial.com/
Then I wrote this code in arduino
char message1[2];
char message2[] = {'a','b'};
char message3;
//char message[3];
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
message3 = Serial.read();
} //read message
if (message3 == 'a') //i.e. begin
{
for(;;)
{
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
}
else
{
digitalWrite(13, HIGH); // set the LED on
}
}
THIS WORKS FINE. WHEN I ENTER a the LED BLINKS JUST AS I WANT!!
BUT now I want to enter a string and I cannot work it out.
Looking back at the C# code (FOR SENDING 2 CHARACTERS):
(This is what I think)
// If the port is Open, declare a char[] array with one element.
char[] buff = new char[2];
// Load element 0 with the key character.
buff[0] = e.KeyChar;
serialPort1.Write(buff,0,1);
buff[1] = e.KeyChar;
serialPort1.Write(buff,0,1);
That is what I think , but I still not getting it working,
The arduino code is this
char message1[2];
char message2[] = {'a','b'};
char message3;
char message4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
//pinMode(12, OUTPUT);
if (Serial.available() > 0)
{
message3 = Serial.read();
message4 = Serial.read();
} //read message
if (message3 == 'a' && message4 == 'b') //i.e. begin
{
for(;;)
{
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
}
else
{
digitalWrite(13, HIGH); // set the LED on
}
}
PLEASE HELP!!
Note: 1st code is all WORKING - RECIEVING A CHAR
but 2nd code is not working when trying to recieve more than one CHAR
tnx