CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Rs232 timeout

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Requan



Joined: 11 May 2008
Posts: 74

View user's profile Send private message

Rs232 timeout
PostPosted: Tue Jun 12, 2012 7:46 am     Reply with quote

Dear All,
I wrote program for modbus communication, so i have two devices host and sensor.
Host send request and wait for answer from sensor. I did timeout in loop.
Strange thing is that when host first time(after restart) ask sensor i need put timeout more then 70000, from next time to forever 300 is enough.
Is this normal?

Functions:
Code:

#INT_RDA
void serial_isr() {
   int t;

   buffer[next_in]=getc();
   t=next_in;
   if(++next_in==BUFFER_SIZE)
      next_in=0;
   if(next_in==next_out) //get rid of the ';' here - wrong.....
      next_in=t;    // Buffer full !!
}



BYTE bgetc()
{
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}


int1 ModbusReceiveData(int16 BytesQuantity, int32 TimeOutValue,int8 SlaveID, int8 FunctionCode)
{
   int1 Status = False;
   int index=0;
   BYTE temp_chr;
   int32 TimeOutCounter = 0;
 
         while (TimeOutCounter < TimeOutValue)
         {
            if (bkbhit)
            {
            TimeOutCounter = 0;

               //Now have a character
               temp_chr = bgetc(); //get the character
               
               Modbus.BufferRs232[index]=temp_chr; //store the character
               if (index<32) ++index; //and update the counter
               
               
               if (index== BytesQuantity)               
               {
                  ModbusCalcCrc(BytesQuantity - 2);
                  if((Modbus.BufferRs232[index - 1] == Modbus.CRC_Hi) && (Modbus.BufferRs232[index - 2] == Modbus.CRC_Lo ))
                  {
                     if((Modbus.BufferRs232[0]==SlaveID)&&(Modbus.BufferRs232[1]==FunctionCode))// sprawdzam odebrany kod funkcji
                     {
                      Status =  TRUE;
                     }
                     else
                     {
                        Status =  FALSE;
                     }
                  }
                  else
                  {
                     Status = FALSE;
                  }
                  break;
               }
            }
            TimeOutCounter++;
         }
   
   return Status;             
}


void ModbusReadRegisters_Request(int8 SlaveID,int8 StartingAddress,int8 RegisterQuantity) //zapytanie na komende 03
{
   output_high(TXEN);
   Modbus.BufferRs232[0]  =  SlaveID;
   Modbus.BufferRs232[1]  =  Modbus.Read_holding_register;
   Modbus.BufferRs232[2]  =  0;
   Modbus.BufferRs232[3]  =  StartingAddress;
   Modbus.BufferRs232[4]  =  0;
   Modbus.BufferRs232[5]  =  RegisterQuantity;
   ModbusCalcCrc(6);
   Modbus.BufferRs232[6]  =  Modbus.CRC_Lo;
   Modbus.BufferRs232[7]  =  Modbus.CRC_Hi;
   
   for(int i = 0;i < 8; i++)
   {
      putc(Modbus.BufferRs232[i]);
   }
  delay_ms(3);
  output_low(TXEN);
}




Main:
Code:

 while(TRUE)
   {
      ModbusReadRegisters_Request(1,0x64,0x04);        //01 03 00 64 00 04 05 D6
       
     
      if (ModbusReceiveData(21,70000,1,3)) 
      {
         printf(lcd_putc,"\fT:%2.1fC H:%2.1f%%",Temp.ft,Humi.ft);
      }
      else
      {
       printf(lcd_putc,"\fError");
      }
      delay_ms(1000);
       
   }


Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group