Pages: 1 [2]   Go Down
Author Topic: Serial communication USB using C# (Visual Studio)  (Read 6510 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45974
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45974
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want the LED to be turned on and off ONLY when the two characters "a" and "b" are entered IN THAT ORDER:
Code:
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
      }
   }
}
Logged

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

Ok tnx that works...smiley-grin

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45974
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if ((Serial.available() == 2) && (Serial.available()<3))
Huh? If there are exactly 2 bytes available to read, isn't that less than 3?

You really need to re-think this whole process of sending serial data. You MUST use some sort of end-of-packet marker.

You have code that does one thing when there are 2 bytes available to read, and code that does something else if there are 3 bytes available. How can you get to 3 bytes available without the "2 bytes available" code being executed first?
Logged

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

End of packet marker....what does that mean?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45974
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you type a post or a reply, how do I know when to stop reading? That is pretty simple, really. When I encounter some punctuation!

There, there are three "end of packet markers" shown (., !, and ?, in case you missed them).
Logged

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

Ok tnx, it is now working smiley-grin
this is my code, when user enters R - it switches on LED, while entering G - infinitley blinking it....
Now to save these characters into an array....

This is my code till now
Code:
char startbyte;
char test = '>';
char messageref[2];
char messagegas[3];

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

void loop()
{
  if (Serial.available() > 0)
  {
    startbyte = Serial.read();
   
    if(startbyte == 'R')
    {
      Serial.print(startbyte);
      while(1)
      {
      if(Serial.available() == 2)
      {
        //Serial.print('a');
        for(int i = 0; i < 2 ; i++)
        {
          messageref[i] = Serial.read();
        }
     
        if(messageref[0] == '1' && messageref[1] == '2')
        {
          digitalWrite(13,HIGH);
        delay(1000);
        digitalWrite(13,LOW);
        delay(1000);
        }
       }
      }
    }
    else if(startbyte == 'G')
    {
      //Serial.print(startbyte);
      while(1)
      {
      if(Serial.available() == 3)
      {
        //Serial.print('a');
        for(int i = 0; i < 3 ; i++)
        {
          messagegas[i] = Serial.read();
        }
     
        if(messagegas[0] == '7' && messagegas[1] == '2' && messagegas[2] == '0')
        {
          while(1)
          {
          digitalWrite(13,HIGH);
          delay(1000);
          digitalWrite(13,LOW);
          delay(1000);
          }
        }
       }
      }
    }
  }
}

tnx
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 212
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ehm.. serial.print    vs    serial.println("your complete string here");   // will send it as a whole string on a a line and sends cariage return   

 smiley-roll-sweat?
Logged

Pages: 1 [2]   Go Up
Jump to: