Serial data management problem in arduino mega

Hello
Im using arduino serial port 1 to communicate with serial 232 device (using max232 chip) i send to the device text command and getting the temp on the device
The device has some issues with buffer and to overcome the issue i need to send a dummy command so it will clean the buffer and than i send again the temp command ...so far so good the code works perfect and i reciving valid data parsit to float and life is good however i want to be able to reset the value if there is no device connected
When im shutoff the device im stack at " case 0" and its look like after the serial1.print command done everything freez if there is no port alive
My Q is how can i detect if the communication was stop ??

char myByte;
int str;
float Fstr;
int c =0;
String inString = "";  

 
void setup() {

  // initialize both serial ports:

  Serial.begin(38400);

  Serial1.begin(38400); 

}

 

void loop() {

switch (c)

{

  case 0:

  Serial1.print("read-temp .s\r"); // sending request to device for temp

  delay(10);

  c = 1;

 

  case 1:

       if (Serial1.available()>0) {

             int inChar = Serial1.read(); // reciving the data and parsing it to get clean temp

                  if (isDigit(inChar))

                     {

                     inString += (char)inChar;

                      }

                       if (inChar == '\n') {               

                       inString.remove(3);              

                       str = inString.toInt();

                       Fstr =float(str)/10; // this is the value i want to reset if there no communication

                      Serial.println (Fstr,1);

                      inString = "";

                    }                 

                  break;

              }

               else

               c=2;


 

          case 2:

          Serial1.print("unknown command \r"); // after every command need to send false data and ignore the return value before i send again the temp command

          delay(10);        
          c  =3;

 

          case 3:

   

         if (Serial1.available()>0) {         

            myByte = Serial1.read(); ignore ther return data

               

          break;

        }

         else

         delay(10);

         c=0;   // return to another round to get fresh temp value

       }

  // Serial.println (Fstr); if i print heare the value and turn off the device i will get the last result was update and i cant find way to reset it ....

}

after you send your command to read the temperature start a timer, e.g. using millis()
if no reply has been received in ten seconds display a message, e.g.

void loop() {
static long int timer=0;
switch (c)
{
  case 0:
  Serial1.print("read-temp .s\r"); // sending request to device for temp
  delay(10);
  timer=millis();
  c = 1;
  case 1:
       if(millis()-timer > 10000)
        {
          c=0;
          Serial.println("no reply from device");
        }
       if (Serial1.available()>0) {

My Q is how can i detect if the communication was stop ??

How would YOU determine that no one was going to reply to your post?

Ok that was working great ... thanks
But when out i disconnect only 1 pin out i think the rx pin its not go to timeout becuse it recognuze some invalid data so it want reset the timer i think i need to check the data i recive if it valid or not.....

if you know what the incomming data format is you can check for invalid characters and if received wait a few seconds, flush the input buffers and prompt for temperature again

looking again at the code in post #1 I note that you are using String to buffer received data which is not recommended on small microcontrollers
I would suggest using a char array and do bounds check
if your incomming data exceeds the bounds assume you are receiving garbage