www.pudn.com > IEC104_codeamaterial.rar > ctpri.c, change:2010-09-21,size:8150b


/*------------------------------------------------------------------------ 
 Module:		ctpri.c 
 Author:		solar 
 Project:		 
 Creation Date:  
 Description:	 
------------------------------------------------------------------------*/ 
/*------------------------------------------------------------------------ 
 $Log: $ 
------------------------------------------------------------------------*/ 
 
#include "syscfg.h" 
 
 
#ifdef	INCLUDE_COMMTEST 
 
#define CT_ID   0 
 
#define EV_SCHEDULE    0x100 
 
#include "string.h" 
#include "bsp.h" 
#include "os.h" 
#include "sys.h" 
 
#define MINLEN 10 
#define MAXLEN 110 
#define MaxComBufSize 120 
#define SEND 0x80 
 
struct TESTHEAD_t{ 
	BYTE sync[6]; 
	WORD addr; 
	BYTE command; 
	BYTE length; 
}; 
 
static   struct TESTHEAD_t* tph;                 // test packed head 
static   BYTE m_SyncWord[2];    
static   WORD  m_nLength; 
static   WORD m_wTxNum; 
static   WORD m_wRxMeNum; 
static   WORD m_wRxRespNum; 
static   DWORD m_dwTxTotalNum; 
static   DWORD m_dwRxTotalMeNum; 
static   DWORD m_dwRxTotalRespNum; 
static   DWORD m_dwTotalRxBadNum; 
static   DWORD m_dwTotalBreakNum; 
static   DWORD m_dwTotalTimeoutNum; 
	  
static   BYTE m_RxBuf[MaxComBufSize]; 
static   WORD m_wRxHead; 
static   WORD m_wRxTail; 
static   BYTE m_TxBuf[MaxComBufSize]; 
static   WORD m_wTxHead; 
static   WORD m_wTxTail; 
    
static   void RecFrm(void); 
static   void EditTx(void); 
static   void SDToBuf(void); 
static   void SendToTeam(void); 
static   BOOL FindHead(void);   
static   void Rxd(void); 
static   void Txd(void); 
static   void OnTimeOut(void); 
static   void Schedule(void);	  
  
 
void ctpri(void) 
{   
    DWORD events; 
 
    evReceive(CT_ID, EV_TM1 | EV_RX_AVAIL| EV_TX_AVAIL | EV_SCHEDULE, &events); 
 
    if (events & EV_RX_AVAIL)  Rxd(); 
		 
    if (events & EV_TX_AVAIL)  Txd();	 
       
    if (events & EV_TM1)	 OnTimeOut(); 
	  
    if (events & EV_SCHEDULE) Schedule();   
}    
 
void ctpriinit(void) 
{ 
  m_SyncWord[0]=0xEB; 
  m_SyncWord[1]=0x90; 
  m_nLength=-1; 
  memset(m_RxBuf,0,MaxComBufSize); 
  m_wRxHead=0; 
  m_wRxTail=0; 
  memset(m_TxBuf,0,MaxComBufSize); 
  m_wTxHead=0; 
  m_wTxTail=0; 
  m_wTxNum=0; 
  m_wRxMeNum=0; 
  m_wRxRespNum=0; 
  m_dwTxTotalNum=0; 
  m_dwRxTotalMeNum=0; 
  m_dwRxTotalRespNum=0; 
  m_dwTotalRxBadNum=0; 
  m_dwTotalBreakNum=0; 
  m_dwTotalTimeoutNum=0; 
 
  evSend(CT_ID,EV_SCHEDULE); 
  tCreate(CT_ID, (void *)NULL, (ENTRYPTR)ctpri); 
} 
  
static void  Rxd(void) 
{ 
  int RNum; 
  int i; 
  if (m_wRxHead<m_wRxTail) 
  { 
    if (m_wRxHead!=0) 
    { 
      i=0; 
      while(m_wRxHead<m_wRxTail) 
      { 
        m_RxBuf[i]=m_RxBuf[m_wRxHead]; 
	    m_wRxHead++; 
	    i++; 
      }	 
      m_wRxTail=i; 
      m_wRxHead=0; 
    }  
  } 
  else 
   m_wRxHead=m_wRxTail=0;  
 
  RNum=commRead(CT_ID,(m_RxBuf+m_wRxTail),MaxComBufSize-m_wRxTail,0); 
  m_wRxTail+=RNum; 
 
  /*myprintf("m_wRxHead=%d m_wRxTail=%d,\n",m_wRxHead,m_wRxTail);*/ 
  /*myprintf("%d,%d\n",m_wRxHead,m_wRxTail);*/ 
 
  /*myprintf("\nREC:  "); 
  for (i=0; i<RNum; i++) 
  	myprintf("%d ", m_RxBuf[m_wRxTail + i]);*/ 
 
 
  if ((m_wRxTail-m_wRxHead)>=(m_nLength+MINLEN)) 
    RecFrm(); 
}   
 
static BOOL FindHead(void) 
{ 
  tph=(struct TESTHEAD_t *)(m_RxBuf+m_wRxHead); 
  if((tph->sync[0]==0xeb)  && (tph->sync[1]==0x90) && (tph->sync[2]==0xeb)  && (tph->sync[3]==0x90) && (tph->sync[4]==0xeb)  && (tph->sync[5]==0x90))  
   return(TRUE); 
  m_wRxHead++; 
  return(FALSE); 
}  
 
static void RecFrm(void) 
{ 
  int i; 
  BOOL YesMe; 
  BYTE *Data; 
  while (m_wRxHead<m_wRxTail) 
  { 
    while ((m_wRxHead<m_wRxTail)&&(m_RxBuf[m_wRxHead]!=m_SyncWord[0])) 
    { 
      m_wRxHead++; 
    } 
    if ((m_wRxTail-m_wRxHead)<(m_nLength+MINLEN)) 
      return; 
    if (FindHead()== TRUE)  
    { 
    	if (tph->command&SEND) 
     	{ 
     		YesMe=TRUE; 
			myprintf("me\n"); 
    	}	 
    	else 
     		YesMe=FALSE;  
    	if(tph->length!=m_nLength) 
     	{ 
		   m_dwTotalRxBadNum++;		    
		   myprintf("m_dwTotalRxBadNum=%ld m_nLength=%d RecLen=%d\n",m_dwTotalRxBadNum,m_nLength,tph->length); 
		   m_wRxHead++; 
     	}	 
    	else 
     	{	  
      	   Data=(BYTE*)(tph+1); 
           for(i=0;i<m_nLength;i++) 
        	 if (Data[i]!=i) 
       	 	 { 
                 myprintf("!!"); 
				 break; 
        	 }	  
           if (i!=m_nLength)       
           { 
        	 m_wRxHead+=MINLEN+i; 
           }  
      	   else 
       	   { 
       		 if (YesMe==TRUE) 
        	 { 
        		m_wRxMeNum++; 
        		m_dwRxTotalMeNum++; 
        	 }  
       		 else 
       	 	 { 
       	 	    m_wRxRespNum++; 
        	 	m_dwRxTotalRespNum++; 
         	 }   
 
             m_wRxHead+=MINLEN+m_nLength;   
       	   }	  
 
		   /*myprintf("m_nLength=%d RecLen=%d\n",m_nLength,tph->length);*/ 
 
           if (YesMe==FALSE)   EditTx(); 
     	} 
    }// end of if(findhead())  
  }//end of while(head<tail) 
}            
 
static void SDToBuf(void) 
{ 
  BYTE i; 
  BYTE *Data; 
  struct TESTHEAD_t* tphTemp=(struct TESTHEAD_t*)m_TxBuf; 
  m_wTxHead=m_wTxTail=0; 
  tphTemp->sync[0]=0xEB; 
  tphTemp->sync[1]=0x90; 
  tphTemp->sync[2]=0xEB; 
  tphTemp->sync[3]=0x90; 
  tphTemp->sync[4]=0xEB; 
  tphTemp->sync[5]=0x90; 
  tphTemp->addr=0xFFFF; 
  tphTemp->command=SEND;  
  tphTemp->length=(BYTE)m_nLength; 
  Data=(BYTE*)(tphTemp+1); 
  for(i=0;i<m_nLength;i++) 
    Data[i]=i; 
  m_wTxTail=m_nLength+MINLEN; 
  m_wTxNum++; 
  m_dwTxTotalNum++; 
  SendToTeam(); 
} 
  
static void SendToTeam(void) 
{ 
  int i; 
 
  if (m_wTxHead==0) 
  { 
     /*struct TESTHEAD_t* tphTemp=(struct TESTHEAD_t*)m_TxBuf; 
	 myprintf("send len=%d\n",	tphTemp->length);	 */ 
 
  }	 	 
  else 
  	 myprintf("m_wTxHead=%d m_wTxTail=%d,\n",m_wTxHead,m_wTxTail); 
   
  i=commWrite(CT_ID,(m_TxBuf+m_wTxHead),m_wTxTail-m_wTxHead,1); 
  //myprintf("i send=%d\n",i); 
 
  /*if (i<=0)  
  	myprintf("i send=%d\n",i);*/ 
	  
  m_wTxHead+=i; 
} 
 
static void Txd(void) 
{ 
  if (m_wTxHead<m_wTxTail)  SendToTeam(); 
}    
 
static void EditTx(void) 
{ 
  struct VSysClock SystemClock; 
  int FrmLen; 
  //float j; 
  int j; 
   
  tmEvCancle(CT_ID); 
   
  m_nLength++;   
  if (m_nLength > (MAXLEN-MINLEN)) 
  { 
   	GetSysClock((void *)&SystemClock,SYSCLOCK); 
   	/*j=((float)m_wRxMeNum/m_wTxNum)*100; 
    myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d自发自收正确率:%.1f%%\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,j); 
  	j=((float)m_wRxRespNum/m_wTxNum)*100; 
    myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d应答正确率:%.1f%%\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,j);*/ 
 
   	j=(m_wRxMeNum/m_wTxNum)*100; 
    myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d自发自收正确率:%d%%\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,j); 
  	j=(m_wRxRespNum/m_wTxNum)*100; 
    myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d应答正确率:%d%%\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,j);	 
    //myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d收%d:发%d\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,m_wRxRespNum, m_wTxNum); 
    myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d累计通断次数:%ld\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,m_dwTotalBreakNum);	     
	myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d累计超时次数:%ld\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID,m_dwTotalTimeoutNum); 
 
   	m_wTxNum=0; 
   	m_wRxMeNum=0; 
   	m_wRxRespNum=0; 
   	m_nLength=0; 
  }	  
 
  FrmLen=MINLEN+m_nLength; 
  SDToBuf(); 
 
  tmEvEvery(CT_ID, 100); 
}  
  
static void OnTimeOut(void) 
{ 
  m_dwTotalTimeoutNum++; 
  EditTx(); 
}  
  
static void Schedule(void) 
{ 
  EditTx(); 
} 
 
#endif