Serial communication USB using C# (Visual Studio)

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

Why don't you write the whole byte array in your C# code?

Because the user has to enter it, since this is a part of a project involving a database of car models,
Therefore the user has to enter the car model and a 4 digit no later on....

any idea how i can do more than one char pls?
tnx

Right, but they don't have to enter it one character at a time. Why not have a prompt that asks for all of the inputs and then transmits them all at once?

if (Serial.available() > 0) 
    { 
     
      message3 = Serial.read();
      message4 = Serial.read();

If there is at least one character available to read, read them both. Do you see the problem with this statement?

BiohazrD - I want to have a form; i.e. having a text box and the user enters in this text box....

PaulS - I tried this code but still no hope....

if (Serial.available() > 0) 
    { 
      for(int x = 0; x<2;x++)
      {
      message1[x] = Serial.read();
      }

there is a error in the condition checking you are making

if (Serial.available() > 0)
{
for(int x = 0; x<2;x++)
{
message1[x] = Serial.read();
}

Serial.available()>0 checks for the arrival of single character, say for example you need four character input change the condition as

if (Serial.available() > 3)
{
for(int x = 0; x<4;x++)
{
message1[x] = Serial.read();
}

hope this helps!

Cheers,
bala
Tenet Techentronics
http://tenettech.com

That will also not work as you are wanting it to. You need to capture the serial characters as a string.

Ok so new code is this:

if (Serial.available() > 3) 
    { 
      for(int x = 0; x<2;x++)
      {
      message1[x] = Serial.read();
      }//read message
    }
    Serial.print(message1[1]);

The new question is Serial.available()>3 means i want to recieve 2 characters right??

The new prob is that when the message is printed on the screen after i press for example ab, it should print only b but it prints bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

????
TNX

No it doesn't - like I said, that code is wrong.

You could read one char and wait for another char if that first char was an 'a'.
That would be the easiest way to do what you want to do (if I understand correctly what you are wanting to do)

Yes that is what im trying to do....

This is my code:

char message1[2];
char message2[] = {'a','b'};
char message3;
char message4;


void setup() 
{ 
  
Serial.begin(9600);

}

void loop() 
{
 //pinMode(12, OUTPUT); 
   if (Serial.available() > 3) 
    { 
      for(int x = 0; x<2;x++)
      {
      message1[x] = Serial.read();
      }//read message
    }
    Serial.print(message1[1]);
    
if ((message1[0] == 'a') && (message1[1] == 'b'))    //i.e. begin
  {
    //Serial.println("i"); // "i\r" in C#
    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   
      //digitalWrite(13,LOW);
    }
  }

  else 
  {
    digitalWrite(13, HIGH);	// set the LED on
  }
}

Why is it wrong?????????????

Because you added in that code which I said was wrong.

if (Serial.available() > 0)
  {
  char character = Serial.read();
  if(character == 'a'){
      while(1){
      if(Serial.available() > 0)
      break;
      }
  character = Serial.read();
  if(character == 'b')
    {
    //Do something here
    }
  }
}

Hmm, I wonder if I made any mistakes in that - it's a bit early to be writing code :stuck_out_tongue:

It ain't working...

char message1[2];
char message2[] = {'a','b'};
char message3;
char message4;


void setup() 
{ 
  
Serial.begin(9600);

}

void loop() 
{
  if (Serial.available() > 0)
  {
    char character = Serial.read();
    if(character == 'a')
    {
      while(1)
      {
      if(Serial.available() > 0)
      digitalWrite(13,HIGH);
      delay(1000);
      digitalWrite(13,LOW);
      delay(1000);
      break;
      }
    }
    character = Serial.read();
    
    if(character == 'b')
    {
    //Do something here
    }
  }
}

Entering a should blink LED

Entering a twice will make the LED bulb...

When you enter a character, your program determines that there is serial data to read, and it reads that character.

I determines that the character is the letter a, so this code is executed:

      while(1)
      {
      if(Serial.available() > 0)
      digitalWrite(13,HIGH);
      delay(1000);
      digitalWrite(13,LOW);
      delay(1000);
      break;
      }
    }

This loop waits until another character arrives at the serial port. As soon as it arrives, and it doesn't matter what it is, the LED on pin 13 is turned on, one second is wasted, the LED on pin 13 is turned off, another second is wasted, then a jump out of the while loop occurs.

Then, you actually read that character.

If the character that was entered initially was NOT the letter a, you read the next character (there may not be one) and see if the next character is the letter b.

Take some time to write down exactly what you want to send to the serial port, and exactly what you want to have happen when that data arrives at the serial port. Then, we can help you make that happen.

Randomly rewriting the code without a clear set of requirements is an exercise in frustration for all concerned.

Well you've changed what I wrote. I think what I wrote should have worked but it cannot be modified how you did.

PaulS-ok tnx....

I am trying to send two characters from the serial port to the arduino. I have a form having a textbox which sends characters....
In this box I am trying to send the characters a and b.
Reading one character has been succesful, but more than one has not.

In order to see if it is working, when the user writes a and b, LED 13 should blink. If these characters have not been succesfully saved in the string then the LED should switch on ONLY.

That is what I am trying to do.

Tnx, hope I do it tnx to you guys....

If you want the LED to be turned on and off ONLY when the two characters "a" and "b" are entered IN THAT ORDER:

void loop()
{
   if(Serial.available() >= 2) // Do nothing unless there ARE 2 characters to read
   {
      int val1 = Serial.read(); // Read 1st byte
      int val2 = Serial.read(); // Read 2nd byte

      if(val1 == 'a' && val2 == 'b')
      {
        // Flash the LED
      }
   }
}

Ok tnx that works...:smiley:

Now I am trying to arrange it a bit more...
From the GUI (Form), in the textbox when the user enters a 2 digit character, I need the arduino to blink the LED,
while when one enters a 3 digit character, I want it to infinitly blinks the LED.
Note: Blinking the LED is just a test to see that I am recieving the characters.....

this is the code, but for some reason only the BLINK LED once is happining, i.e. entering 2 digits. When one enters the 3 digit, it doesn'r work...

char message1[2];
char gas[3];
char message3;
char message4;


void setup() 
{ 
  
Serial.begin(9600);

}

void loop() 
{
  if(Serial.available() > 0)
  {
    if ((Serial.available() == 2) && (Serial.available()<3))
    {
    /*char val1 = Serial.read(); // Read 1st byte
      char val2 = Serial.read(); // Read 2nd byte

      if(val1 == '1' && val2 == '2')
      {
        digitalWrite(13,HIGH);
        delay(1000);
        digitalWrite(13,LOW);
        delay(1000);
        Serial.print(val1);
        Serial.print(val2);
      }
      */
      
      for(int x=0;x<2;x++)
      {
        message1[x] = Serial.read();
      }
      
      if((message1[0] == '1') && (message1[1] == '2'))
      {
        digitalWrite(13,HIGH);
        delay(1000);
        digitalWrite(13,LOW);
        delay(1000);
        //Serial.print(val1);
        //Serial.print(val2);
      }
   
   }
   
   
   
    else if (Serial.available() >= 3)
  {
    /*char val1 = Serial.read(); // Read 1st byte
      char val2 = Serial.read(); // Read 2nd byte

      if(val1 == '1' && val2 == '2')
      {
        digitalWrite(13,HIGH);
        delay(1000);
        digitalWrite(13,LOW);
        delay(1000);
        Serial.print(val1);
        Serial.print(val2);
      }
      */
      
      for(int x=0;x<3;x++)
      {
        gas[x] = Serial.read();
      }
      
      if((gas[0] == '7') && (gas[1] == '2') && (gas[2] == '0'))
      {
        while(1)
        {
        digitalWrite(13,HIGH);
        delay(1000);
        digitalWrite(13,LOW);
        delay(1000);
        //Serial.print(val1);
        //Serial.print(val2);
        }
      }
   
   }
  }
    
 }

tnx