UART HEX COMMANDS NOT WORKING

I am supposed to send hex commands via the uart and receive hex response
I must send the following hex code and get 3 hex responses and are MSB,LSB and the uart checksum.
The following was my code,but i didnt get the reading.
I use an ultrasonic sensor as the serial device .Depending on the distance of obstacle ,the MSB and LSB varies.
I also want it to view it in the serial monitor.

The following is my code:-
void setup()
{
Serial3.begin(19200);
Serial.begin(9600);
}
void loop()
{
int sensor1=0;
byte message = {0x00,0x55,0x11,0x02,0x00 };
Serial3.write(message, sizeof(message));
sensor1=Serial3.read();
Serial.print(sensor1);
Serial.println();
delay(1000);
}

Serial3.write(message, sizeof(message));
sensor1=Serial3.read();

You need to wait until the sensor has actually sent some data, before you call the read() function.

See Serial Input Basics.

Do you mean that the program must be coded like the following:-

void setup() { Serial3.begin(19200); Serial.begin(9600); } void loop() { int sensor1=0; byte message[] = {0x00,0x55,0x11,0x02,0x00 }; Serial3.write(message, sizeof(message)); delay(2000); sensor1=Serial3.read(); Serial.print(sensor1); Serial.println(); }

Please clarify my doubt.

Or you could to check to see that there is something to read, before reading it.

Please remember to use code tags when posting code.

Sure sir,next time,i will insert the code tag. I will make few attempts in this hex coding issue and revert you back.

hariramraja:
Do you mean that the program must be coded like the following:-

void setup()
{
Serial3.begin(19200);
Serial.begin(9600);
}
void loop()
{
int sensor1=0;
byte message = {0x00,0x55,0x11,0x02,0x00 };
Serial3.write(message, sizeof(message));
delay(2000);
sensor1=Serial3.read();
Serial.print(sensor1);
Serial.println();

}

Please clarify my doubt.

you may want to try this instead of using “delay(2000)” (unless you what to check for data every 2s)

while(1<Serial3.available()); // wait for reply from sensor

while(Serial3.available()){ received data from sensor
Serial.print(Serial3.read());
}
Serial.println();

that should work if you are only writing the data to the serial monitor.
However you if you intend to do more with the code, you may also want to implement a “wait timeout” so that you code does not hang when no data is available