Pages: [1] 2   Go Down
Author Topic: Serial communication USB using C# (Visual Studio)  (Read 7294 times)
0 Members and 3 Guests are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
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() > 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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
Code:
if (Serial.available() > 0)
    {
      for(int x = 0; x<2;x++)
      {
      message1[x] = Serial.read();
      }
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

there is a error in the condition checking you are making

if (Serial.available() > 0)
    {
      for(int x = 0; x<2;x++)
      {
      message1
  • = 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
  • = Serial.read();
      }

hope this helps!

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

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so new code is this:
Code:
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
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This is my code:
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?????????????
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 smiley-razz
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It ain't working...
Code:
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
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Entering a twice will make the LED bulb...
Logged

Pages: [1] 2   Go Up
Jump to: