Go Down

Topic: Serial communication USB using C# (Visual Studio) (Read 7 times) previous topic - next topic

PaulS

Code: [Select]
    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?

Daniel Formosa

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

PaulS

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

Daniel Formosa

Ok tnx, it is now working :D
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: [Select]
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

PGT

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

Go Up