Serial Communication

Hello Members
I am having problems in making my code understand my commands that I am sending through hyper terminal. Please find attached the code below and help me trouble shoot. I receive the initial introduction lines on the PC end but the "loop" is not responding to my commands.

#include <SoftwareSerial.h>
/-----( Declare Constants and Pin Numbers )-----/
#define Rx 10 //Serial Receive pin
#define Tx 11 //Serial Transmit pin

#define TxControl 3 //RS485 Direction control

#define Pin13LED 13

SoftwareSerial RSSerial(Rx, Tx); // RX, TX

/-----( Declare Variables )-----/
int byteReceived,val ;
byte A,B;

void setup() /****** SETUP: RUNS ONCE ******/
{
pinMode(Pin13LED, OUTPUT);
pinMode(TxControl, OUTPUT);
pinMode(Rx,INPUT);
pinMode(Tx, OUTPUT);
RSSerial.begin(9600); // set the data rate

digitalWrite(TxControl,HIGH);
RSSerial.write("Arduino UNO Live...\n");
RSSerial.write("Please Enter a valid command ...\n");
digitalWrite(TxControl,LOW);
while(RSSerial.read()>=0);
delay(2000);
}

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
val= RSSerial.available();
byte Command[] ={0,A,B};

while (val!=Command[1]||Command[2]);
{
digitalWrite(Pin13LED,HIGH);
delay(1000);
digitalWrite(Pin13LED,LOW);}

if (val ==Command[1]){

digitalWrite(TxControl,HIGH);
byteReceived= RSSerial.read();
RSSerial.write("byteReceived");
while(RSSerial.read()>=0);
digitalWrite(TxControl,LOW);
delay(1000);
}
else if (val ==Command[2]){
digitalWrite(TxControl,HIGH);
RSSerial.write("Inavalid");

while(RSSerial.read()>=0)
digitalWrite(TxControl,LOW);
delay(1000);}

}

Please edit and wrap your code with tags.

Also, comments would go leaps and bounds to understand what you are trying to accomplish. Most of the code is, quite frankly, nonsense. I've tried to understand and make notes below.

I strongly suspect you don't understand about available() and read() work... Generally you use a if-statement or while-loop with available() to determine when you've received bytes. Then you read those bytes (and no more than has been received) with read(). I'm not sure I follow what you are doing.

val= RSSerial.available();

What's the point of storing this value? available() returns the number of bytes available, not any data received.

  while (val !=Command[1] || Command[2]);

I think you have two syntax errors in this section. Maybe the following code is what you meant. However, I don't understand why you are using the variable "val" here, since it only contains a number, not "commands".

// val contains the number of bytes available, what's the point of these checks?
while ( (val !=Command[1]) || (val != Command[2]))   // note the missing semicolon...
  {
    digitalWrite(Pin13LED,HIGH);
    delay(1000);
    digitalWrite(Pin13LED,LOW);
    delay(1000);  // otherwise, you'll never see the LED turn off
  }
    byteReceived= RSSerial.read();

while(RSSerial.read()>=0);

What? You read in a byte, without checking to see if one is avaiable. Then you keep reading bytes and throwing them away until there are none in the buffer?!

{
  val= RSSerial.available();
byte  Command[] ={0,A,B};
 while (val!=Command[1]||Command[2]);
{

Serial.available() returns the number of bytes waiting in the serial buffer. If you want the value of the first byte in the buffer use SRSSerial.read();
Like:
if(RSSerial.available() > 0) {
val = RSSerial.read():
}

 while (val!=Command[1]||Command[2]);

This is NOT comparing val to Command[1] and to Command[2]. There are no shortcuts to doing things right.

@zain89, do not cross-post. Threads merged. Replies go here.