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


/*------------------------------------------------------------------------ 
 Module:		ctsec.c 
 Author:		solar 
 Project:		 
 Creation Date:  
 Description:	 
------------------------------------------------------------------------*/ 
/*------------------------------------------------------------------------ 
 $Log: $ 
------------------------------------------------------------------------*/ 
 
#include "syscfg.h" 
 
#ifdef	INCLUDE_COMMTEST 
 
#include "string.h" 
#include "bsp.h" 
#include "os.h" 
#include "sys.h" 
 
#define CT_ID   0 
 
#define MINLEN 10 
#define MAXLEN 110 
#define MaxComBufSize 120 
#define SEND 0x80 
 
struct TESTHEAD_t{ 
	unsigned char sync[6]; 
	unsigned short addr; 
	unsigned char command; 
	unsigned char length; 
}; 
 
static      WORD m_wAddr; 
static      BYTE m_SyncWord[2];    
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      struct TESTHEAD_t *tph;                 //test packet head   
static      BYTE m_byWpl;                      //wanted packet lngth 
static      DWORD m_dwTotalBreakNum; 
    
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);   
 
void ctsec(void) 
{ 
    DWORD events; 
 
    evReceive(CT_ID, EV_RX_AVAIL|EV_TX_AVAIL, &events); 
 
    if (events & EV_RX_AVAIL)  Rxd(); 
		 
    if (events & EV_TX_AVAIL)  Txd();	 
}    
 
void ctsecinit(void) 
{ 
	m_SyncWord[0]=0xEB; 
	m_SyncWord[1]=0x90; 
	memset(m_RxBuf,0,MaxComBufSize); 
	m_wRxHead=0; 
	m_wRxTail=0; 
	memset(m_TxBuf,0,MaxComBufSize); 
	m_wTxHead=0; 
	m_wTxTail=0; 
	m_wAddr=0xFFFF; 
	m_dwTotalBreakNum=0; 
 
    tCreate(CT_ID, (void *)NULL, (ENTRYPTR)ctsec); 
} 
 
static void Rxd(void) 
{ 
  int RNum; 
  int i;  
  if (m_wRxHead<m_wRxTail) 
   { 
    if (m_wRxHead!=0) 
     { 
      for(i=0;i<m_wRxTail-m_wRxHead;i++) 
      	m_RxBuf[i]=m_RxBuf[m_wRxHead+i] ; 
      m_wRxTail-=m_wRxHead; 
      m_wRxHead=0; 
     }  
   } 
  else 
   m_wRxHead=m_wRxTail=0;  
 
  RNum=commRead(CT_ID,(m_RxBuf+m_wRxTail),MaxComBufSize-m_wRxTail,0); 
 
  /*myprintf("\nREC:  "); 
  for (i=0; i<RNum; i++) 
  	myprintf("%d ", m_RxBuf[m_wRxTail + i]);*/  
  
  m_wRxTail+=RNum; 
   
  /*myprintf("Rec:%d,%d\n",m_wRxHead,m_wRxTail);*/ 
   
  if ((m_wRxTail-m_wRxHead)>=MINLEN) 
    RecFrm(); 
}   
 
static BOOL FindHead(void) 
{ 
	while (m_wRxTail-m_wRxHead>=MINLEN) 
	{ 
     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)&&(tph->command==SEND)&&((tph->addr==0xFFFF)||(tph->addr==m_wAddr)))  
		   return(TRUE); 
		 m_wRxHead++; 
	} 
	return(FALSE); 
} 
	 
static void RecFrm(void) 
{ 
  int i; 
  BYTE *data; 
  struct VSysClock SystemClock; 
   
  while (m_wRxTail-m_wRxHead>=MINLEN) 
  { 
  	if(FindHead()==FALSE) return; 
 
    /*myprintf("\n1-Rece len=%d\n",tph->length);*/ 
	 
    if(tph->length+MINLEN>m_wRxTail-m_wRxHead) return; 
     
    data=(BYTE*)(tph+1); 
 
    /*myprintf("\n2-Rece len=%d\n",tph->length);*/ 
	 
    for(i=0;i<tph->length;i++) 
	{ 
        /*myprintf("%d ", data[i]);*/ 
		if(data[i]!=i)  
		{ 
			myprintf("\nRece len=%d\n",tph->length); 
    		break; 
		}	 
    }    
	 
    if(i!=tph->length) 
    { 
      m_wRxHead++; 
      continue; 
    } 
     
    EditTx(); 
    m_wRxHead+=(MINLEN+tph->length); 
    if(tph->length==0) 
    { 
      m_byWpl=0; 
    } 
    else if(tph->length==MAXLEN-MINLEN) 
    { 
      if(m_byWpl+1==MAXLEN-MINLEN) 
      { 
     	GetSysClock((void *)&SystemClock,SYSCLOCK); 
   	    myprintf("%04d-%02d-%02d %02d:%02d:%02d 端口%d接收正确\n",SystemClock.wYear,SystemClock.byMonth,SystemClock.byDay,SystemClock.byHour,SystemClock.byMinute,SystemClock.bySecond,CT_ID);		 
		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); 
   	    m_byWpl=0; 
      } 
    } 
    else if(m_byWpl+1!=tph->length) 
   	  m_byWpl=0; 
    else m_byWpl++; 
  } 
}            
 
static void SDToBuf(void) 
{ 
  BYTE i; 
  m_wTxHead=m_wTxTail=0; 
  memcpy(m_TxBuf,m_RxBuf+m_wRxHead,MINLEN); 
  m_TxBuf[8]&=(~SEND);  
  for(i=0;i<tph->length;i++) 
    m_TxBuf[MINLEN+i]=i;    
  m_wTxTail=MINLEN+tph->length;     
  SendToTeam(); 
} 
  
static void SendToTeam(void) 
{ 
  int i; 
 
  /*if (m_wTxHead==0) 
  { 
	 struct TESTHEAD_t* tphTemp=(struct TESTHEAD_t*)m_TxBuf; 
	 myprintf("SEC:send len=%d\n",	tphTemp->length);	  
  } 	 
  else 
	  myprintf("SEC:m_wTxHead=%d m_wTxTail=%d,\n",m_wTxHead,m_wTxTail);*/ 
 
  i=commWrite(CT_ID,(m_TxBuf+m_wTxHead),m_wTxTail-m_wTxHead,0); 
   
  /*logMsg("i send=%d\n",i,0,0,0,0,0);*/ 
  m_wTxHead+=i; 
} 
 
static void Txd(void) 
{ 
  if (m_wTxHead<m_wTxTail)  SendToTeam(); 
}    
 
static void EditTx(void) 
{ 
  SDToBuf(); 
} 
 
#endif