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 CCS Technical Support

TimeOut -RS232

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







TimeOut -RS232
PostPosted: Mon Apr 09, 2007 10:39 pm     Reply with quote

Code:

#include <18f452.h> //Device in use
#include <stdlib.h>
#include <CTYPE.h>
#include <STRING.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#use rs232(stream=CH1,baud=9600,xmit=PIN_C6,rcv=PIN_C7)//Hw UART-->INT RDA
#use rs232(stream=CH2,baud=9600,xmit=PIN_B2,rcv=PIN_B1,TIMEOUT=5000,ERRORS)//SW UART-->not use an interrupt services

/*****************************************************************************/

#define Time_out_status   PIN_D1
#define Normol_status     PIN_D4
#define Valve_status      PIN_D0

/*************************  Variable Decralation  *****************************/

BOOLEAN RX=0,MODE;
int i,next_in=0;
float Inact_valve,act_valve,Ovalue;//,O_actu,Cvalue;
byte Otemp[8],Temp_Command[8],Ctemp[5];
char Command;
unsigned char act_str[8],Inact_str[8];

char *ptr;

/*****************************************************************************/
/************************  Sub Functions Decralation**************************/

void CHECK_Valve(float);
void receive_untill(char);
void recv_ox(void);
void task_timeout(void);

/*****************************************************************************/
/************************  INT Rx Service Routine  ***************************/

#INT_RDA  //Rx PIN Connect to D.O. Meter
void hw_uart_isr (){
char rs232_temp;   

           rs232_temp = fgetc(CH1);

                  Temp_Command[next_in]=rs232_temp;
                        
                     if (rs232_temp==0x0d){
                           //next_in=0;
                     ptr = strchr(Temp_Command,'C');

                     Command = *(ptr+1);//(Temp_Command[next_in-5]);
                     Ctemp[0]= *(ptr+2);//(Temp_Command[next_in-4]);
                     Ctemp[1]= *(ptr+3);//(Temp_Command[next_in-3]);
                     Ctemp[2]= *(ptr+4);//(Temp_Command[next_in-2]); //Must be " . "         
                     Ctemp[3]= *(ptr+5);//(Temp_Command[next_in-1]);
                      Ctemp[4]= 0x00;
                           next_in=0;                  
                     switch (Command){
            
                        case 'A' :
                              MODE=1;
                              strcpy(act_str,Ctemp);
                              //delay_ms(100);
                              fprintf(CH1,"RA%s\n\r",Ctemp);
                              RX=1;
                              break;
                        case 'I' :
                              MODE=0;
                              strcpy(Inact_str,Ctemp);
                              //delay_ms(100);
                              fprintf(CH1,"RI%s\n\r",Ctemp);
                              RX=1;
                              break;
                        default :
                              break;
                     }//close switch
                  
                  return;
      } else
            next_in++;
            return;
         
}//Close sub routine

/*****************************************************************************/
/**************************  MAIN Routine  *****************************/

void main ()
{
 
enable_interrupts(INT_RDA);

enable_interrupts(GLOBAL);

set_tris_d(0b11101100);//Set D0  for Output port

   next_in=0;

for(;;){
            
               if(RX){
                     if(MODE){
                        act_valve=atof(act_str);
                        RX=0;
                     }else
                        Inact_valve=atof(Inact_str);
                        RX=0;
      
               }//Close if RX
         delay_ms(2000);//wait

         recv_ox();//go to get Data and cheak for  toggle valve

         CHECK_Valve(OValue);// Cheak for open and close
         fprintf(CH1,"%3.1f",OValue);
   }
}

/*****************************************************************************/
/***************************  CHECK_Valve Routine  ***************************/

void CHECK_Valve(float Ans){
         if (Ans <=act_valve){
            output_high(PIN_D0);
               //break;
         }else if ((Ans >act_valve) & (Ans >= Inact_valve)){
            output_low(PIN_D0);
   }
}

/***************************************************************************/
/*********************** receive_untill  Routine  **************************/

void receive_untill(char cpr){
   char temp;
   do{
      temp=fgetc(CH2);
      if(temp==0){
            task_timeout();
         }
   }while(!(temp==cpr));
}

/***************************************************************************/
/*************************** recv_ox  Routine  *****************************/

void recv_ox(void){
byte temp[16];

      do{
          receive_untill(0x02);
         i=0;
         do{
            temp[i]=fgetc(CH2);
                                                                 if(temp==0){
            task_timeout();
         }
         }while(!(temp[i++]==0x0d));
      }while(!(i>14));
         
      Otemp[0]=*(temp+11);
      Otemp[1]=*(temp+12);
      Otemp[2]='.';
      Otemp[3]=*(temp+13);
      Otemp[4]=0x00;
      OValue = atof(Otemp);//now OValue already  a  float value      
}

void task_timeout(){
   char time_error;   
   do {   
      fprintf(CH1,"Time Out\n\r");
      time_error =fgetc(CH2);
      delay_ms(2000);
   }while(!(time_error==0));
}


/*****************************************************************************/
/********************************** End **************************************/
/*****************************************************************************/


My code check for time out 2 times. 1. recive_untill and 2.recv_ox
when disconnect in recive_untill UART can come back read a data because
i have (while!(temp==cpr)) cpr is a start of frame but it not work in case of recv_ox because i don't know why?
now i need a function for Resart or goto read start of frame...

please help me, this is the last chance for me....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 09, 2007 11:18 pm     Reply with quote

Quote:

#use rs232(stream=CH1,baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#use rs232(stream=CH2,baud=9600,xmit=PIN_B2,rcv=PIN_B1,TIMEOUT=5000,ERRORS)

You have ERRORS in the software UART. It doesn't work with the software
UART. It only works with the hardware UART. You need to move it to
the CH1 UART statement.

Quote:

rs232_temp = fgetc(CH1);

Temp_Command[next_in]=rs232_temp;

if (rs232_temp==0x0d){
//next_in=0;
ptr = strchr(Temp_Command,'C');

In the code above, you're putting incoming characters into an array.
You don't check to see if the index (next_in) has gone past the end
of the array. Also, you're using a string function (strchr), but you
haven't put a zero byte (0x00) on the end of the received characters,
and therefore the array is not a string. The strchr() function needs
to see the 0x00 at the end so it know when to stop searching.

Your code has many problems beyond these. You're doing fprintf()
operations inside the isr. You shouldn't do time-comsuming operations
in an isr. You have a return statement at the end of the isr. You don't
need it. No CCS example code in any example file has a 'return' at the
end of an isr. Etc.
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