www.pudn.com > iec104.rar > NsIec104Server.c, change:2003-09-05,size:93778b


/* 
 
            Modify Record  
            2002-11-16            Add Network Card Value Geting From nsc200.ini 
            2002-12-26            According   Nedd,adding Send chang yc 
 
*/ 
#include <socket.h> 
#include <sockLib.h> 
#include <stdlib.h> 
#include <sysLib.h> 
#include <logLib.h> 
#include <errno.h> 
#include <string.h>  
#include <stdio.h>  
#include <time.h> 
#include "wdLib.h" 
#include "in.h"		 
#include "ioLib.h"	 
 
#include "constant.h" 
#include "NsDataBase.h" 
#include "NsExtTime.h" 
#include "NsIoFunction.h" 
#include "NsCommon.h" 
#include "NsReadOnly.h" 
#include "ExtDataBase.h" 
 
#include "Project/NsIec104.h" 
 
static NS_IEC104_MAIN      NsIec104Struct; 
 
 
        void       NsNetServerProcess(void); 
 
static  void       NsIec104Process(void); 
static  void       NsIec104Init(unsigned char Card,short Port); 
static  void       NsIec104NetInit(void); 
 
static  void       NsIec104Reset(void); 
static  void       NsIec104ProcessFormatI(void); 
static  void       NsIec104ProcessFormatS(void); 
static  void       NsIec104ProcessFormatU(void); 
static  void       NsIec104Interrogation(void); 
static  void       NsIec104InterrogationGroup(void); 
static  void       NsIec104InterrogationAll(void); 
static  void       NsIec104ProcessYxGroup(unsigned char Group); 
static  void       NsIec104ProcessYcGroup(unsigned char Group); 
static  void       NsIec104ProcessCellGroup(unsigned char Group); 
static  void       NsIec104ProcessTime(void); 
static  void       NsIec104ProcessYkYt(void); 
static  void       NsIec104ProcessPulse(void); 
static  void       NsIec104DefaultGroup(unsigned char Group); 
static  void       NsIec104ProcessPulseData(void); 
static  void       NsIec104SendGroup(void); 
 
static  void       NsIec104SendYkMessage(short Who,unsigned char Action); 
static  void       NsIec104ReceiveYkReturn(void); 
static  void       NsIec104SendYkSelectConfirm(void); 
static  void       NsIec104SendYkExeConfirm(void); 
static  void       NsIec104SendYkEscConfirm(void); 
static  unsigned char        NsIec104SendCosSoe(void); 
static  void       NsIec104InitYk(void); 
static  void       NsIec104CycleCount(void); 
static  void       NsIec104SendChangeYc(void); 
/* 
static void 	   Iec104testCosSoe(void); 
void 		debug104(void); 
*/ 
static  void       NsIec104Init(unsigned char Card,short Port) 
{ 
 unsigned short    i; 
 int               Result; 
 char              Buff[32]; 
 char              Ip0Buff[64],Ip0Route[64]; 
 char              Ip1Buff[64],Ip1Route[64]; 
 NS_SYSTEM_DATA_MODE	SysData; 
 
    NsIec104Struct.LinkState=NS_DISCONNECTED; 
    NsIec104Struct.NsNewSocketId=0; 
    getforsecond(&NsIec104Struct.LinkValidTime); 
    NsIec104Struct.ReceiveIndex=0;   
    NsIec104Struct.ReceiveIndexLength=0;   
    NsIec104Struct.SendMeNumber=0;   
    NsIec104Struct.HasSendNumber=0; 
    NsIec104Struct.NeedSendNumber=0; 
    NsIec104Struct.LeftSendNumber=0; 
    NsIec104Struct.Card=Card; 
    NsIec104Struct.Port=Port; 
    NsIec104Struct.YkStartSign=OFF; 
    NsIec104Struct.Table=0; 
    NsIec104Struct.NetRunSign=OFF; 
    NsIec104Struct.SetNetSign=OFF; 
 
    Result=ReadNsc200Ini("NsIec104","SetTimeFlag",Buff); 
    if(Result) 
    { 
        NsIec104Struct.SetTimeFlag=1; 
    } 
 
    for(i=0;i<NS_IEC104_USE_CYCLE;i++)  
    { 
	getforsecond(&NsIec104Cycle[i].LastTime); 
	NsIec104Cycle[i].TimeNap= NsIec104Nap[i]; 
        NsIec104Cycle[i].TimeSign=OFF;  
    } 
 
    for(i=0;i<NS_IEC104_MAX_YC;i++) 
        NsIec104Struct.YcNap[i]=5; 
    NsIec104Struct.YcChangeCycle=0; 
     
    	NsIec104Struct.TableNo = NsGetCommTable(Port); 
	GetSysDataMode(NsIec104Struct.TableNo,&SysData); 
	/*NsIec104Struct.XYxNum  = SysData.NsYxTableNumber; 
	 
	NsIec104Struct.XYmNum  = SysData.NsYmTableNumber;*/ 
	NsIec104Struct.XYcNum  = SysData.NsYcTableNumber; 
	NsIec104Struct.XYcCycleNum = (NsIec104Struct.XYcNum + NS_IEC104_ONCE_YC_COUNT - 1) / NS_IEC104_ONCE_YC_COUNT; 
	        
} 
 
 
static  void        NsIec104NetInit(void) 
{ 
 int               Result; 
 char              Buff[32]; 
 char              Ip0Buff[64],Ip0Route[64]; 
 char              Ip1Buff[64],Ip1Route[64]; 
 
    NsTcpServerInit(NsIec104Struct.Card,NsIec104Struct.Port,"10.14.127.200"); 
    switch(NsIec104Struct.Card) 
    { 
        case 0: 
                Result=ReadNsc200Ini("NsIec104","Route0Sign",Buff); 
                if(Result) 
                { 
                   NsReadIpAddress("NsIec104","Route0Net",Ip0Buff);      
                   NsReadIpAddress("NsIec104","Route0Ip",Ip0Route);   
                   NsAddRoute(Ip0Buff,Ip0Route);    
                } 
                break; 
        case 1: 
                Result=ReadNsc200Ini("NsIec104","Route1Sign",Buff); 
                if(Result) 
                { 
                   NsReadIpAddress("NsIec104","Route1Net",Ip1Buff);      
                   NsReadIpAddress("NsIec104","Route1Ip",Ip1Route);      
                   NsAddRoute(Ip1Buff,Ip1Route);    
                } 
                break; 
        default: 
                break; 
    } 
} 
 
 
 
 
static  void        NsIec104InitYk(void) 
{ 
 
    NsIec104Struct.YkStartSign=OFF; 
    NsIec104Struct.YkNowState=0xff; 
    NsIec104Struct.YkCellNumber=0xff; 
    NsIec104Struct.YkKgNumber=0xff; 
 
} 
 
void NsNetServerProcessv(void) 
{ 
 
    while(1) 
    { 
      taskDelay(88); 
    } 
} 
 
 
void NsNetServerProcess(void) 
{ 
 unsigned long     Now,Nap; 
 unsigned short    Card; 
 int               Result; 
 char              Buff[32]; 
  
   Card=0; 
   Result=ReadNsc200Ini("NsTasks","NsServerCard",Buff); 
   if(Result) 
   { 
      Card=Result; 
      if(Card<=2&&Card>0) 
      { 
        Card-=1; 
      } 
   } 
   NsIec104Init(Card,2404); 
   for(;;) 
   { 
        NsIec104Struct.SystemState = NsGetSysParameter(NS_NSCC_STATE); 
        NsIec104Struct.NetRunSign  = NsNetConfigState(); 
 
        if(NsIec104Struct.NetRunSign==ON&&NsIec104Struct.SetNetSign==OFF&&NsIec104Struct.SystemState==DUTY) 
        { 
           NsIec104NetInit(); 
           NsIec104Struct.SetNetSign=ON; 
        } 
        if(NsIec104Struct.NetRunSign==ON&&NsIec104Struct.SetNetSign==ON&&NsIec104Struct.SystemState==DUTY) 
        { 
          getforsecond(&Now); 
          Nap=DiffMsLong(Now,NsIec104Struct.LinkValidTime); 
          if(Nap>NS_IEC104_PERMIT_TIME) 
          { 
             NsIec104Struct.LinkState=NS_DISCONNECTED; 
          } 
          if(NsIec104Struct.YkStartSign==ON) 
          { 
             Nap=DiffMsLong(Now,NsIec104Struct.YkStartTime); 
             if(Nap>NS_IEC104_YK_PERMIT_TIME) 
             { 
                 NsIec104InitYk(); 
             } 
          } 
          if(NsIec104Struct.LinkState==NS_DISCONNECTED) 
          { 
 
              NsIec104Struct.NsNewSocketId=NsTcpConnect(NsIec104Struct.NsNewSocketId,NsIec104Struct.Card); 
 
              if(NsIec104Struct.NsNewSocketId>0) 
              { 
                 NsIec104Struct.LinkState=NS_CONNECTED;   
                 NsIec104Struct.SendMeNumber=0;   
                 getforsecond(&NsIec104Struct.LinkValidTime); 
              } 
              else  
             { 
                 /*NsIec104Init(NsIec104Struct.Card,NsIec104Struct.Port);*/ 
                 
                /*  NsTcpServerInit(NsIec104Struct.Card,NsIec104Struct.Port,"10.14.8.23");*/ 
                 NsIec104Struct.LinkState=NS_DISCONNECTED; 
                 NsIec104Reset(); 
                 taskDelay(866); 
             } 
          }   
          else  
          { 
            
    	     NsIec104Struct.ReceiveIndexLength=recv(NsIec104Struct.NsNewSocketId,&NsIec104Struct.ReceiveBuffer[NsIec104Struct.ReceiveIndex],198,0); 
    	      
             if(NsIec104Struct.ReceiveIndexLength>0) 
             { 
                NsIec104Struct.ReceiveLength+=NsIec104Struct.ReceiveIndexLength; 
                if(NsIec104Struct.ReceiveLength<NS_IEC104_RECEIVEBUFFER) 
                { 
                    NsIec104Process(); 
                } 
                else  
                { 
                    NsIec104Reset(); 
                } 
                /* 
                debug104(); 
				Iec104testCosSoe(); 
				*/ 
                getforsecond(&NsIec104Struct.LinkValidTime); 
             } 
              
             NsIec104ReceiveYkReturn(); 
             NsIec104SendGroup(); 
 
             if(NsIec104Struct.LeftSendNumber==0) 
             { 
                  NsIec104Struct.CosSign=NsIec104SendCosSoe(); 
                  if(NsIec104Struct.CosSign==0) 
                  { 
                     NsIec104SendChangeYc(); 
                  } 
             } 
          } 
          NsIec104CycleCount(); 
      } 
      taskDelay(18); 
   } 
} 
 
static void  NsIec104SendChangeYc(void) 
{ 
 
unsigned short   General,Index; 
unsigned char    *P; 
unsigned short   i,RealNumber; 
         short   *Pyc,YcValue,YcNap; 
float		fYc; 
 
 
 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];                                /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];  /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=9;                        /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=0;                  /*    information object count No 7 Byte */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=3;                 /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x0;                      /*    reason  */ 
 
          NsIec104Struct.SendBuffer[Index++]=0x01; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
/*              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
*/ 
          NsIec104Struct.YcLibIndex=NsIec104Struct.YcChangeCycle*NS_IEC104_ONCE_YC_COUNT; 
          /*NsIec104Struct.YcChangeCycle=(NsIec104Struct.YcChangeCycle+1)%NS_IEC104_CHANGE_YC_CYCLE;*/ 
          NsIec104Struct.YcChangeCycle=(NsIec104Struct.YcChangeCycle+1)%NsIec104Struct.XYcCycleNum; 
           
          NsIec104Struct.YcChangeCount=0; 
 
          switch(NsIec104Struct.Table) 
          { 
                 case 0: 
                      NsGetDd1Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT); 
                      break; 
                 case 1: 
                      NsGetDd2Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT); 
                      break; 
                 case 2: 
                      NsGetDd3Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT); 
                      break; 
                 case 3: 
                      NsGetDd4Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT); 
                      break; 
          } 
 
          for(i=0;i<NS_IEC104_ONCE_YC_COUNT;i++) 
          { 
                 /*Pyc=(short *)&NsIec104Struct.DataBuff[2*i]; 
                 YcValue= *Pyc; 
                 YcNap=YcValue-NsIec104Struct.YcLastValue[NsIec104Struct.YcLibIndex+i]; 
                 if(YcNap<0) 
                      YcNap=NsIec104Struct.YcLastValue[NsIec104Struct.YcLibIndex+i]-YcValue; 
  
                 if(YcNap>NsIec104Struct.YcNap[NsIec104Struct.YcLibIndex+i]) 
                 { 
                      NsIec104Struct.YcChangeCount++; 
                      NsIec104Struct.YcLastValue[NsIec104Struct.YcLibIndex+i]=YcValue;*/     
                      /* Information Object Address Now Three Bytes */ 
                      General=0x701+NsIec104Struct.YcLibIndex+i; 
                      P=(unsigned char    *)&General; 
                      NsIec104Struct.SendBuffer[Index++]=P[0]; 
                      NsIec104Struct.SendBuffer[Index++]=P[1]; 
                      NsIec104Struct.SendBuffer[Index++]=0; 
 
                      /*fYc = YcValue * 1.0; 
		      P=(unsigned char    *)&fYc; 
			 
		      NsIec104Struct.SendBuffer[Index++]=P[0]; 
                      NsIec104Struct.SendBuffer[Index++]=P[1]; 
 		      NsIec104Struct.SendBuffer[Index++]=P[2]; 
                      NsIec104Struct.SendBuffer[Index++]=P[3];*/ 
 		      NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i]; 
                      NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i + 1]; 
                      NsIec104Struct.SendBuffer[Index++]=0x00;  /*qds*/ 
                  /*}*/ 
          } 
 
          /* Repair Length  and Count*/ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          NsIec104Struct.SendBuffer[7]=NsIec104Struct.YcChangeCount; 
          if(NsIec104Struct.YcChangeCount>0) 
          {/* 
             printf("\n Co=%d Ind=%dGe=%d",NsIec104Struct.YcChangeCount,NsIec104Struct.YcLibIndex,General); 
			*/ 
             if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
             { 
	               perror ("Client FirstWrite Error"); 
             } 
          } 
} 
 
 
static void  NsIec104SendGroup(void) 
{ 
  if(NsIec104Struct.NeedSendNumber>0&&NsIec104Struct.HasSendNumber<NS_IEC104_MAX_K) 
  { 
     /* 
           printf("\n GroupId=%d",NsIec104Struct.NsNewSocketId); 
     */ 
     if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[NsIec104Struct.LeftSendNumber][0],NsIec104Struct.GroupSendLength[NsIec104Struct.LeftSendNumber],0x0))<0) 
     { 
        perror ("GRoup Erroe"); 
     } 
      
     NsIec104Struct.LeftSendNumber++;   
     /*  
       printf("\n Send Group=%d",NsIec104Struct.SendLength); 
      */ 
    if(NsIec104Struct.LeftSendNumber>=NsIec104Struct.NeedSendNumber) 
    { 
       NsIec104Struct.HasSendNumber=0; 
       NsIec104Struct.NeedSendNumber=0; 
       NsIec104Struct.LeftSendNumber=0; 
    } 
  } 
  taskDelay(108); 
 
} 
 
 
static void  NsIec104Process(void) 
{ 
 int              i; 
 
/*      printf("r:"); 
      for(i=0;i<NsIec104Struct.ReceiveLength;i++) 
          printf("%4x",NsIec104Struct.ReceiveBuffer[i]); 
      printf("\n"); 
*/       
    if(NsIec104Struct.ReceiveLength>=NS_IEC_104_MIN_LENGTH&&NsIec104Struct.ReceiveBuffer[0]==NS_IEC_104_HEAD) 
    {                             
       NsIec104Struct.FormatType=NsIec104Struct.ReceiveBuffer[2]&0x03; 
       if(NsIec104Struct.FormatType==0||NsIec104Struct.FormatType==2)            /* I */ 
       { 
              NsIec104ProcessFormatI(); 
       } 
       else  
       { 
              if(NsIec104Struct.FormatType==1)     /*  S  */  
                   NsIec104ProcessFormatS(); 
              else 
              { 
                   if(NsIec104Struct.FormatType==3)/*  U  */ 
                      NsIec104ProcessFormatU(); 
              } 
        } 
        NsIec104Reset(); 
    } 
 
} 
 
static  void  NsIec104ProcessFormatI(void) 
{ 
int i; 
unsigned  char   *p; 
unsigned  short  NoWay; 
/*  
       printf("\nFormatI"); 
*/ 
       NsIec104Struct.ApduLength=NsIec104Struct.ReceiveBuffer[1]; 
       if(NsIec104Struct.ApduLength>NS_IEC_104_MIN_LENGTH) 
       { 
          NsIec104Struct.ReceiveHimNumber[0]=NsIec104Struct.ReceiveBuffer[2]; 
          NsIec104Struct.ReceiveHimNumber[1]=NsIec104Struct.ReceiveBuffer[3]; 
          NoWay=*(unsigned short *)&NsIec104Struct.ReceiveHimNumber[0]; 
          NoWay>>=1; 
          NoWay+=1; 
          NoWay<<=1; 
          p=(unsigned char *)&NoWay; 
          NsIec104Struct.ReceiveHimNumber[0]=p[0]; 
          NsIec104Struct.ReceiveHimNumber[1]=p[1]; 
 
          NsIec104Struct.AsduType=NsIec104Struct.ReceiveBuffer[6];   
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
             NsIec104Struct.TransmitReason[i]=NsIec104Struct.ReceiveBuffer[NS_STARTREASON+i]; 
          
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.CommonAsduAddress[i]=NsIec104Struct.ReceiveBuffer[NS_STARTASDUADDRESS+i]; 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.InformationObject[i]=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+i]; 
 
          NsIec104Struct.AsduQualifier=NsIec104Struct.ReceiveBuffer[NS_STARTQUALIFIER];   
 
          switch(NsIec104Struct.AsduType) 
          { 
             case 46: 
             case 47: 
                      NsIec104Struct.YkYtType=NsIec104Struct.AsduType; 
                      NsIec104ProcessYkYt(); 
                      break; 
             case 100:  /* Interrogation */ 
                      NsIec104Interrogation(); 
                      break; 
             case 101: 
                      NsIec104ProcessPulse(); 
                      break; 
             case 103: 
                      NsIec104ProcessTime(); 
                      /* 
                        printf("\n Time1"); 
                       */ 
                      break; 
             default: 
                      break; 
          } 
       } 
} 
 
 
static  unsigned  char       NsIec104SendCosSoe(void) 
{ 
unsigned short     Number; 
NS_SOE_FORMAT      SoeData; 
NS_COS_FORMAT      CosData;    
unsigned short     Index,SendCount; 
unsigned char      *P,i,CosSendSign; 
unsigned short     RealNumber,Object,Msecond; 
 
  Index=0; 
  CosSendSign=0; 
  Number=NsCheckDd1Cos(); 
  if(Number>0) 
  { 
          if(Number>36) 
             Number=36; 
          SendCount=0; 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=0x01;                 /*    0x01--->0x02 asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=1;                  /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=0x03;               /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
          for(i=0;i<Number;i++) 
          { 
               if(NsGetDd1CosLib(&CosData)==ON) 
               { 
                 SendCount++; 
                 /* Information Object Address now three byte:  0xc01---0xc80 */ 
                 Object=CosData.SendNumber+0x01; 
                 P=(unsigned char *)&Object; 
                 NsIec104Struct.SendBuffer[Index++]=P[0]; 
                 NsIec104Struct.SendBuffer[Index++]=P[1]; 
                 NsIec104Struct.SendBuffer[Index++]=0x00; 
                 NsIec104Struct.SendBuffer[Index++]=CosData.State; 
               } 
 
          } 
           
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[7]=SendCount;  /* vsq number */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
 
  } 
  else  
  { 
      Number=NsCheckDd1Soe(); 
      if(Number>0) 
      { 
         if(Number>18) 
            Number=18; 
          SendCount=0; 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=30;                 /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=1;                  /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=0x03;               /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          for(i=0;i<Number;i++) 
          { 
              if(NsGetDd1SoeLib(&SoeData)==ON) 
              {                
                 SendCount++; 
                 /* Information Object Address now three byte:  0xc01---0xc80 */ 
                 Object=SoeData.SendNumber+0x01; 
                 P=(unsigned char *)&Object; 
                 NsIec104Struct.SendBuffer[Index++]=P[0]; 
                 NsIec104Struct.SendBuffer[Index++]=P[1]; 
                 NsIec104Struct.SendBuffer[Index++]=0x00; 
                 NsIec104Struct.SendBuffer[Index++]=SoeData.State; 
                 Msecond=SoeData.Msecond+SoeData.Second*1000; 
                 P=(unsigned char *)&Msecond; 
                 NsIec104Struct.SendBuffer[Index++]=P[0]; 
                 NsIec104Struct.SendBuffer[Index++]=P[1]; 
                 NsIec104Struct.SendBuffer[Index++]=SoeData.Minute; 
                 NsIec104Struct.SendBuffer[Index++]=SoeData.Hour; 
                 NsIec104Struct.SendBuffer[Index++]=SoeData.Day; 
                 NsIec104Struct.SendBuffer[Index++]=SoeData.Month; 
                 NsIec104Struct.SendBuffer[Index++]=(SoeData.Year-2000); 
 
              } 
           } 
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[7]=SendCount;  /* vsq number */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
      } 
      else  
      { 
         if(NsIec104Cycle[0].TimeSign== ON) 
         { 
                    Index=0; 
                    NsIec104Struct.SendBuffer[Index++]=0x68; 
                    NsIec104Struct.SendBuffer[Index++]=0x04; 
                    NsIec104Struct.SendBuffer[Index++]=0x43; 
                    NsIec104Struct.SendBuffer[Index++]=0x00; 
                    NsIec104Struct.SendBuffer[Index++]=0x00; 
                    NsIec104Struct.SendBuffer[Index++]=0x00; 
                    NsIec104Cycle[0].TimeSign=OFF; 
         } 
      } 
  } 
  if(Index>0) 
  { 
     NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0); 
     CosSendSign=0x01; 
  } 
  return CosSendSign; 
} 
 
 
 
static  void       NsIec104ProcessYkYt(void) 
{ 
 
unsigned char     YkInformation,YkReason,YkWhat; 
unsigned short    YkObject; 
unsigned char     Media,YkAction; 
NS_RELATION       Dear; 
 
/* 
     Information Object Address 0xb01---0xb80   
 for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
             NsIec104Struct.TransmitReason[i]=NsIec104Struct.ReceiveBuffer[NS_STARTREASON+i]; 
for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.InformationObject[i]=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+i]; 
 
*/ 
 
 YkInformation=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+NS_IEC_104_INFORMATIONBYTE]; 
 YkReason=NsIec104Struct.ReceiveBuffer[NS_STARTREASON]; 
 
 YkObject=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION]+NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+1]*256; 
 YkObject-=0xb01; 
 
 YkWhat=0;  /* nothing */  
 if(YkReason==6) 
 { 
      if(YkInformation&0x80)    /* YkSelect  */ 
          YkWhat=1; 
      else  
          YkWhat=2;             /*  YkExe*/ 
 
 
 } 
 else  
     if(YkReason==8)  /* Esc*/ 
        YkWhat=3; 
 
 switch(YkWhat) 
 { 
   case 1:  /* select */ 
         NsIec104Struct.ReceiveYkId=YkObject; 
         YkAction=YkInformation&0x03; 
         switch(YkAction) 
         { 
           case  1:   /* Open */ 
                  NsIec104Struct.YkCellAction=NS_TRIP; 
                  break; 
           case  2:    /*  close */ 
                  NsIec104Struct.YkCellAction=NS_CLOSE;        
                  break;      
           default: 
                  break; 
         } 
         NsIec104Struct.YkCellNumber=NsGetYkCell(NS_YK_CELL_NUMBER,NsIec104Struct.ReceiveYkId); 
         NsIec104Struct.YkKgNumber=NsGetYkCell(NS_YK_CELL_KG,NsIec104Struct.ReceiveYkId); 
         NsIec104Struct.YkNowState=NS_YK_SELECT; 
         Media=NsGetYkCell(NS_YK_CELL_CPU,NsIec104Struct.ReceiveYkId);  
         Dear.NsYkPort=NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT; 
         Dear.NsYkSlavePort=Media; 
         NsPutDear(&Dear,6); 
         NsIec104SendYkMessage(NS_YK_SELECT,NsIec104Struct.YkCellAction); 
         NsIec104Struct.YkStartSign=ON; 
         getforsecond(&NsIec104Struct.YkStartTime); 
         /* 
            printf("\n Send Yk SelectId=%d Cell=%d Kg=%d",NsIec104Struct.ReceiveYkId,NsIec104Struct.YkCellNumber,NsIec104Struct.YkKgNumber); 
          */ 
         break; 
   case 2:                   /*  YkExe   */ 
         NsIec104Struct.ReceiveYkId=YkObject; 
         YkAction=YkInformation&0x03; 
         switch(YkAction) 
         { 
            case  1:   /* Open */ 
                  NsIec104Struct.YkCellAction=NS_TRIP; 
                  break; 
            case  2:    /*  close */ 
                  NsIec104Struct.YkCellAction=NS_CLOSE;        
                  break;      
            default: 
                  NsIec104Struct.YkCellAction=0xff;        
                  break; 
          } 
          NsIec104Struct.YkCellNumber=NsGetYkCell(NS_YK_CELL_NUMBER,NsIec104Struct.ReceiveYkId); 
          NsIec104Struct.YkKgNumber=NsGetYkCell(NS_YK_CELL_KG,NsIec104Struct.ReceiveYkId); 
          NsIec104Struct.YkNowState=NS_YK_EXE; 
          Media=NsGetYkCell(NS_YK_CELL_CPU,NsIec104Struct.ReceiveYkId);  
          Dear.NsYkPort=NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT; 
          Dear.NsYkSlavePort=Media; 
          NsPutDear(&Dear,6); 
          NsIec104SendYkMessage(NS_YK_EXE,NsIec104Struct.YkCellAction); 
          NsIec104Struct.YkStartSign=ON; 
          getforsecond(&NsIec104Struct.YkStartTime); 
          /* 
           printf("\n Send Yk ExeId=%d Cell=%d Kg=%d",NsIec104Struct.ReceiveYkId,NsIec104Struct.YkCellNumber,NsIec104Struct.YkKgNumber); 
           */ 
          break; 
   case 3: 
          NsIec104Struct.ReceiveYkId=YkObject; 
          YkAction=YkInformation&0x03; 
          switch(YkAction) 
          { 
            case  1:   /* Open */ 
                  NsIec104Struct.YkCellAction=NS_TRIP; 
                  break; 
            case  2:    /*  close */ 
                  NsIec104Struct.YkCellAction=NS_CLOSE;        
                  break;      
            default: 
                  NsIec104Struct.YkCellAction=0xff;        
                  break; 
          } 
          NsIec104Struct.YkCellNumber=NsGetYkCell(NS_YK_CELL_NUMBER,NsIec104Struct.ReceiveYkId); 
          NsIec104Struct.YkKgNumber=NsGetYkCell(NS_YK_CELL_KG,NsIec104Struct.ReceiveYkId); 
          NsIec104Struct.YkNowState=NS_YK_ESC; 
          Media=NsGetYkCell(NS_YK_CELL_CPU,NsIec104Struct.ReceiveYkId);  
          Dear.NsYkPort=NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT; 
          Dear.NsYkSlavePort=Media; 
          NsPutDear(&Dear,6); 
          NsIec104SendYkMessage(NS_YK_ESC,NsIec104Struct.YkCellAction); 
          NsIec104Struct.YkStartSign=ON; 
          getforsecond(&NsIec104Struct.YkStartTime); 
          /* 
           printf("\n Send Yk EscId=%d Cell=%d Kg=%d",NsIec104Struct.ReceiveYkId,NsIec104Struct.YkCellNumber,NsIec104Struct.YkKgNumber); 
           */ 
          break; 
   default: 
          break; 
  }  
} 
 
 
static void NsIec104SendYkMessage(short Who,unsigned char Action) 
{ 
 
     char Dd1Message[16]; 
 
     switch(Who) 
     { 
       case  NS_YK_ESC: 
             Dd1Message[0]=SEND_YK_HOST_DD1;      
             Dd1Message[1]=NS_YK_ESC;          
             Dd1Message[2]=NsIec104Struct.YkCellNumber; 
             Dd1Message[3]=NsIec104Struct.YkKgNumber; 
             Dd1Message[4]=NsIec104Struct.YkCellAction; 
             NsHostSendYkMessage(SEND_YK_HOST_DD1,8,Dd1Message); 
             break; 
       case  NS_YK_EXE: 
             Dd1Message[0]=SEND_YK_HOST_DD1;   
             Dd1Message[1]=NS_YK_EXE;       
             Dd1Message[2]=NsIec104Struct.YkCellNumber; 
             Dd1Message[3]=NsIec104Struct.YkKgNumber; 
             Dd1Message[4]=NsIec104Struct.YkCellAction; 
             NsHostSendYkMessage(SEND_YK_HOST_DD1,8,Dd1Message); 
             break; 
       case  NS_YK_SELECT: 
             Dd1Message[0]=SEND_YK_HOST_DD1;   
             Dd1Message[1]=NS_YK_SELECT;       
             Dd1Message[2]=NsIec104Struct.YkCellNumber; 
             Dd1Message[3]=NsIec104Struct.YkKgNumber; 
             Dd1Message[4]=NsIec104Struct.YkCellAction; 
             NsHostSendYkMessage(SEND_YK_HOST_DD1,8,Dd1Message); 
             break; 
     } 
} 
 
static void NsIec104ReceiveYkReturn(void) 
{ 
           char    ReturnMessage[16]; 
           int     Length,HasMessage; 
  NS_RELATION      Dear; 
 
   if(NsIec104Struct.YkStartSign==ON) 
   { 
       NsGetDear(&Dear); 
       if(Dear.NsYkPort==(NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT)) 
       { 
          /* 
            printf("\n Yk Return"); 
           */ 
          if(NsHostReceiveYkMessage(SEND_YK_HOST_DD1,ReturnMessage)==1) 
          { 
 
               /*       
                           printf("\nYkMes0=%0x--1=%0x--2=%0x---3=%0x---4=%0x", 
                           ReturnMessage[0],ReturnMessage[1],ReturnMessage[2],ReturnMessage[3],ReturnMessage[4]); 
               */ 
                      
              if(ReturnMessage[0]==SEND_YK_HOST_DD1) 
              {  
                 if(NsIec104Struct.YkNowState==ReturnMessage[1]) 
                 { 
                    if((ReturnMessage[2]==NsIec104Struct.YkCellNumber)&& 
                       (ReturnMessage[3]==NsIec104Struct.YkKgNumber)&& 
                       (ReturnMessage[4]==NsIec104Struct.YkCellAction)) 
                    { 
                         NsIec104Struct.YkError=ReturnMessage[5]; 
                         if(NsIec104Struct.YkNowState==NS_YK_SELECT) 
                         {    
                            NsIec104SendYkSelectConfirm(); 
                            /* 
                               printf("\n Selec Confim"); 
                             */ 
                         } 
                         else  
                         { 
                           if(NsIec104Struct.YkNowState==NS_YK_EXE) 
                              NsIec104SendYkExeConfirm(); 
                           else  
                           { 
                              if(NsIec104Struct.YkNowState==NS_YK_ESC) 
                                 NsIec104SendYkEscConfirm(); 
                           } 
                         } 
                     }  
                 } 
              } 
           } 
        } 
   } 
} 
 
static  void       NsIec104SendYkSelectConfirm(void) 
{ 
 
 
unsigned short   YkObject,Index; 
unsigned char    *P,i; 
unsigned short   RealNumber; 
 
 
 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;          /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=1;           /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=0x07;          /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          /* Information Object Address now three byte:  0xc01---0xc80 */ 
          YkObject=NsIec104Struct.ReceiveYkId+0xb01; 
          P=(unsigned char *)&YkObject; 
          NsIec104Struct.SendBuffer[Index++]=P[0]; 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
              /* Value  */ 
          if(NsIec104Struct.YkCellAction==NS_TRIP) 
             NsIec104Struct.SendBuffer[Index++]=0x81; 
          else  
               if(NsIec104Struct.YkCellAction==NS_CLOSE) 
                  NsIec104Struct.SendBuffer[Index++]=0x82; 
               else  
                  NsIec104Struct.YkCellAction=0xff;        
          
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
              perror ("Client FirstWrite Error"); 
          } 
} 
 
static  void       NsIec104SendYkExeConfirm(void) 
{ 
 
 
unsigned short   YkObject,Index; 
unsigned char    *P,i; 
unsigned short   RealNumber; 
 
 
          /* YkExe Confirm */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;                 /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=1;           /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=0x07;          /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          /* Information Object Address now three byte:  0xc01---0xc80 */ 
          YkObject=NsIec104Struct.ReceiveYkId+0xb01; 
          P=(unsigned char *)&YkObject; 
          NsIec104Struct.SendBuffer[Index++]=P[0]; 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
              /* Value  */ 
          if(NsIec104Struct.YkCellAction==NS_TRIP) 
             NsIec104Struct.SendBuffer[Index++]=0x01; 
          else  
               if(NsIec104Struct.YkCellAction==NS_CLOSE) 
                  NsIec104Struct.SendBuffer[Index++]=0x02; 
               else  
                  NsIec104Struct.YkCellAction=0xff;        
          
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
              perror ("Client FirstWrite Error"); 
          } 
          taskDelay(100); 
          /* YkExe  End */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;                 /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=1;           /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=10;          /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          /* Information Object Address now three byte:  0xc01---0xc80 */ 
          YkObject=NsIec104Struct.ReceiveYkId+0xb01; 
          P=(unsigned char *)&YkObject; 
          NsIec104Struct.SendBuffer[Index++]=P[0]; 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
              /* Value  */ 
          if(NsIec104Struct.YkCellAction==NS_TRIP) 
             NsIec104Struct.SendBuffer[Index++]=0x01; 
          else  
               if(NsIec104Struct.YkCellAction==NS_CLOSE) 
                  NsIec104Struct.SendBuffer[Index++]=0x02; 
               else  
                  NsIec104Struct.YkCellAction=0xff;        
          
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
              perror ("Client FirstWrite Error"); 
          } 
          taskDelay(8); 
} 
 
static  void       NsIec104SendYkEscConfirm(void) 
{ 
 
 
unsigned short   YkObject,Index; 
unsigned char    *P,i; 
unsigned short   RealNumber; 
 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;                 /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=1;                  /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=0x09;               /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          /* Information Object Address now three byte:  0xc01---0xc80 */ 
          YkObject=NsIec104Struct.ReceiveYkId+0xb01; 
          P=(unsigned char *)&YkObject; 
          NsIec104Struct.SendBuffer[Index++]=P[0]; 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
              /* Value  */ 
          if(NsIec104Struct.YkCellAction==NS_TRIP) 
             NsIec104Struct.SendBuffer[Index++]=0x81; 
          else  
               if(NsIec104Struct.YkCellAction==NS_CLOSE) 
                  NsIec104Struct.SendBuffer[Index++]=0x82; 
               else  
                  NsIec104Struct.YkCellAction=0xff;        
          
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
              perror ("Client FirstWrite Error"); 
          } 
} 
 
 
 
static  void       NsIec104ProcessPulseAck(void) 
{ 
unsigned short   i,Index,RealNumber; 
unsigned char    *P; 
 
          /*  First Send  Confirm */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
 
          NsIec104Struct.SendBuffer[Index++]=101; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=0x07; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.Qcc; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	               perror ("Client Write Error"); 
          } 
 
 
} 
 
static  void       NsIec104ProcessPulseEnd(void) 
{ 
unsigned short   i,Index,RealNumber; 
unsigned char    *P; 
 
          /*  end confirm*/ 
          taskDelay(16); 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=101; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=10; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.Qcc; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0) 
          { 
	               perror ("Client Write Error"); 
          } 
 
} 
 
 
static  void       NsIec104ProcessPulse(void) 
{ 
unsigned short   General,Index; 
unsigned char    *P; 
unsigned short    i,j,RealNumber; 
 
 
 
     NsIec104Struct.Qcc=NsIec104Struct.ReceiveBuffer[NS_IEC_104_QCC_POSITION];   
     NsIec104Struct.FreezeSign=NsIec104Struct.Qcc&0xc0; 
      
     NsIec104Struct.FreezeSign>>=6; 
     NsIec104Struct.PulseGroup=NsIec104Struct.Qcc&0x3f; 
 
     switch(NsIec104Struct.FreezeSign) 
     { 
        case 0: 
               NsIec104ProcessPulseData(); 
               break; 
        case 1: 
        case 2: 
        case 3: 
               NsIec104ProcessPulseAck(); 
               break; 
     }            
} 
 
static  void       NsIec104ProcessPulseGroup(void) 
{ 
unsigned short   General,Index; 
unsigned char    *P,start; 
unsigned short    i,j,RealNumber,YmLib; 
 
 
     for(start=0;start<2;start++) 
     { 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=15;                 /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=16;           /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=37+NsIec104Struct.PulseGroup;          /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x0;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
          YmLib=start*16+32*(NsIec104Struct.PulseGroup-1); 
          switch(NsIec104Struct.Table) 
          { 
           case 0: 
                 NsGetDd1Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16); 
                 break; 
           case 1: 
                 NsGetDd2Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16); 
                 break; 
           case 2: 
                 NsGetDd3Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16); 
                 break; 
           case 3: 
                 NsGetDd4Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16); 
                 break; 
          } 
 
          for(i=0;i<16;i++) 
          { 
              /* Information Object Address now three byte:  0xc01---0xc80 */ 
 
              NsIec104Struct.SendBuffer[Index++]=0x80+i+start*16+32*(NsIec104Struct.PulseGroup-1); 
              NsIec104Struct.SendBuffer[Index++]=0x0c; 
              NsIec104Struct.SendBuffer[Index++]=0; 
              /* Value  */ 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i]; 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i+1]; 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i+2]; 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i+3]; 
              /* sequence number  */ 
              NsIec104Struct.SendBuffer[Index++]=start*16+i; 
          }   
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	               perror ("Client FirstWrite Error"); 
          } 
     } 
} 
 
static  void       NsIec104ProcessPulseAll(void) 
{ 
unsigned short      General,Index; 
unsigned char       *P,GroupIndex,start; 
unsigned short      i,j,RealNumber; 
unsigned long       InformationObject; 
 
 
   GroupIndex=0; 
   NsIec104Struct.NeedSendNumber=0; 
   NsIec104Struct.HasSendNumber=0; 
   NsIec104Struct.LeftSendNumber=0; 
 
   /*  First Send  Confirm */ 
   Index=0; 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68; 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e; 
 
   RealNumber=(NsIec104Struct.SendMeNumber<<1); 
   P=(unsigned char *)&RealNumber; 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];              /*     send number       */ 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1]; 
   NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=101; 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1; 
 
   for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
       NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x07; 
              
   for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
       NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
   for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
       NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.InformationObject[i]; 
 
   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.Qcc; 
   NsIec104Struct.GroupSendLength[GroupIndex]=Index; 
 
   GroupIndex++; 
   NsIec104Struct.NeedSendNumber++; 
    
   for(start=0;start<NS_IEC_104_TOTAL_YM_TIMES;start++)           /* total 128 ym send 8 times */             
   { 
          Index=0; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=15;                            /*    asdu type   */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NS_IEC_104_ONCE_YM ;           /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=37  ;          /*    reason  */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          switch(NsIec104Struct.Table) 
          { 
           case 0: 
                 NsGetDd1Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM); 
                 break; 
           case 1: 
                 NsGetDd2Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM); 
                 break; 
           case 2: 
                 NsGetDd3Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM); 
                 break; 
           case 3: 
                 NsGetDd4Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM); 
                 break; 
          } 
          for(i=0;i<NS_IEC_104_ONCE_YM;i++) 
          { 
              /* Information Object Address now three byte:  0xc01---0xc80 */ 
              InformationObject=0xc01+i+start*NS_IEC_104_ONCE_YM; 
              P=(unsigned char *)&InformationObject; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0]; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1]; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[2]; 
              /* Value  */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i]; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i+1]; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i+2]; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i+3]; 
              /* sequence number  */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=i; 
          }   
          /* Repair Length */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][1]=Index-2; 
          NsIec104Struct.GroupSendLength[GroupIndex]=Index; 
          /* 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength,0x0))<0) 
          { 
	               perror ("Client FirstWrite Error"); 
          } 
           */ 
          GroupIndex++; 
          NsIec104Struct.NeedSendNumber++; 
 
    } 
    /* Send Pulse End Packet */ 
    Index=0; 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68; 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e; 
 
    RealNumber=(NsIec104Struct.SendMeNumber<<1); 
    P=(unsigned char *)&RealNumber; 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];              /*     send number       */ 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1]; 
    NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=101; 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1; 
 
    for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
        NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=10; 
              
    for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
        NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
    for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
        NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.InformationObject[i]; 
 
    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.Qcc; 
    NsIec104Struct.GroupSendLength[GroupIndex]=Index; 
    /* 
    if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength,0x0)) < 0) 
    { 
	               perror ("Client Write Error"); 
    } 
    */ 
/* mask 2003-01-24 
    GroupIndex++; 
    NsIec104Struct.NeedSendNumber++; 
*/ 
} 
 
static  void       NsIec104ProcessPulseData(void) 
{ 
 
 
     switch(NsIec104Struct.PulseGroup) 
     { 
        case 1: 
        case 2: 
        case 3: 
        case 4: 
               NsIec104ProcessPulseAck(); 
               NsIec104ProcessPulseGroup(); 
               NsIec104ProcessPulseEnd(); 
               break; 
        case 5: 
               NsIec104ProcessPulseAll(); 
               break; 
     } 
} 
 
static  void     NsIec104ProcessTime(void) 
{ 
 
unsigned short   i,Index,RealNumber,Mmsecond; 
unsigned char    *P,Year,Month,Day,Hour,Minute,Second; 
struct tm        NowTime; 
 
          /*  First Send  Confirm */ 
          for(i=0;i<7;i++) 
          { 
               NsIec104Struct.TimeSave[i]=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+i]; 
          /*      
                   printf("\n Time=%d",NsIec104Struct.TimeSave[i]); 
            */    
          } 
          Mmsecond=(NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION] 
                    +256*NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+1])%1000;  
 
          Second=(NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION] 
                    +256*NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+1])/1000;  
          Minute=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+2]&0x3f; 
          Hour=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+3]&0x1f; 
          Day=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+4]&0x1f; 
          Month=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+5]&0x0f; 
          Year=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+6]&0x7f; 
/* 
          if(NsIec104Struct.SetTimeFlag) 
          { 
              NowTime.tm_sec   = Second; 
              NowTime.tm_min   = Minute; 
              NowTime.tm_hour  = Hour; 
              NowTime.tm_mday  = Day; 
              NowTime.tm_mon   = Month;  
              NowTime.tm_year  = Year + 2000;  
              setvxTime(&NowTime); 
          } 
*/ 
          /* 
              printf("\n Year=%d Month=%d Day=%d Hour=%d Min=%d Sec=%d Msec=%d", 
                         Year,Month,Day,Hour,Minute,Second,Mmsecond); 
           */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
 
          NsIec104Struct.SendBuffer[Index++]=103; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=0x07; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          for(i=0;i<7;i++) 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.TimeSave[i]; 
 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	               perror ("Client Write Error"); 
          } 
} 
 
 
 
 
 
 
static void     NsIec104Interrogation(void) 
{ 
 
 
     switch(NsIec104Struct.AsduQualifier) 
     { 
         case 20:   /*  All Interrogation */ 
                  NsIec104InterrogationAll(); 
                  break; 
         
         default: 
                  NsIec104InterrogationGroup(); 
                  break; 
     } 
 
} 
 
static void     NsIec104InterrogationGroup(void) 
{ 
 unsigned  char  WhichGroup; 
 
 
  if(NsIec104Struct.AsduQualifier>=NS_IEC104_GROUP_BEGIN&&NsIec104Struct.AsduQualifier<=NS_IEC104_GROUP_END) 
  { 
     WhichGroup=NsIec104Struct.ReceiveBuffer[NS_IEC_104_GROUP_BYTE]; 
     WhichGroup-=21; 
     switch(WhichGroup) 
     { 
 
         case 0: 
         case 1: 
         case 2: 
         case 3: 
         case 4: 
         case 5: 
         case 6: 
         case 7: 
                 NsIec104ProcessYxGroup(WhichGroup); 
                 break; 
         case 8: 
         case 9: 
         case 10: 
         case 11: 
                 NsIec104ProcessYcGroup(WhichGroup); 
                 break; 
         case 12:             /*  location information  */ 
                 NsIec104DefaultGroup(WhichGroup); 
                 break; 
         case 13:             /*  BCD Information  */ 
                 NsIec104DefaultGroup(WhichGroup); 
                 break; 
         case 14:             /*  cell state */ 
                 NsIec104DefaultGroup(WhichGroup); 
                 break; 
         case 15:             /*  backup use */ 
                 NsIec104DefaultGroup(WhichGroup); 
                 break; 
         default: 
                 NsIec104DefaultGroup(WhichGroup); 
                 break; 
     } 
 
  } 
 
} 
 
static void    NsIec104DefaultGroup(unsigned char Group) 
{ 
unsigned short   General,Index; 
unsigned char    *P; 
unsigned short    i,j,RealNumber; 
 
 
          /*  First Send  Confirm */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
 
          NsIec104Struct.SendBuffer[Index++]=100; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=0x07; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=21+Group; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	               perror ("Client Write Error"); 
          } 
 
          taskDelay(6); 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=100; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=10; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=21+Group; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0) 
          { 
	               perror ("Client Write Error"); 
          } 
} 
 
 
 
static void    NsIec104ProcessYxGroup(unsigned char Group) 
{ 
unsigned short     General,Index,YxLibIndex; 
unsigned char      *P,start,YxBit,YxByte; 
unsigned short     i,j,g,RealNumber; 
 
 
          /*  Once Gonce need send 64 Yx  8 bytes   Group from 0 begin*/ 
          /*  First Send  Confirm */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];                                    /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];     /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
 
          NsIec104Struct.SendBuffer[Index++]=100;                                    /*      transmit  type    */ 
          NsIec104Struct.SendBuffer[Index++]=1;                                      /*      vsq               */  
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++)                                      /*      reason            */ 
               NsIec104Struct.SendBuffer[Index++]=0x07; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=21+Group; 
          /*  
             printf("\n YxGroup=%d",Group); 
           */ 
 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	          perror ("Client Write Error"); 
          } 
          for(start=0;start<2;start++) 
          { 
            taskDelay(60);           
            Index=0; 
            NsIec104Struct.SendBuffer[Index++]=0x68; 
            NsIec104Struct.SendBuffer[Index++]=0x00; 
 
            RealNumber=(NsIec104Struct.SendMeNumber<<1); 
            P=(unsigned char *)&RealNumber; 
            NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
            NsIec104Struct.SendBuffer[Index++]=P[1]; 
            NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
            NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
            NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
            NsIec104Struct.SendBuffer[Index++]=1;                 /*    asdu type   */ 
            NsIec104Struct.SendBuffer[Index++]=64|0x80;           /*    information object d",count  */ 
 
            /*  REASONBYTE  now is two */ 
            NsIec104Struct.SendBuffer[Index++]=21+Group;          /*    reason  */ 
            NsIec104Struct.SendBuffer[Index++]=0x00;              /*    reason  */ 
 
              
            for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
            /* Information Object Address now three byte*/ 
 
            General=0x01+start*0x40+Group*0x80; 
            P=(unsigned char    *)&General; 
            NsIec104Struct.SendBuffer[Index++]=P[0]; 
            NsIec104Struct.SendBuffer[Index++]=P[1]; 
            NsIec104Struct.SendBuffer[Index++]=0; 
            YxLibIndex=start*8+Group*16; 
 
            switch(NsIec104Struct.Table) 
            { 
              case 0: 
                   NsGetDd1Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8); 
                   break; 
              case 1: 
                   NsGetDd2Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8); 
                   break; 
              case 2: 
                   NsGetDd3Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8); 
                   break; 
              case 3: 
                   NsGetDd4Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8); 
                   break; 
            } 
            for(i=0;i<8;i++)  /* totla 64 yx*/ 
            { 
              
                  YxByte=NsIec104Struct.DataBuff[i];  
                  for(g=0;g<8;g++) 
                  { 
                      YxBit=(YxByte>>g)&0x01; 
                      NsIec104Struct.SendBuffer[Index++]=YxBit; 
                  } 
            } 
            /* Repair Length */ 
            NsIec104Struct.SendBuffer[1]=Index-2; 
            if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
            { 
	               perror ("Client FirstWrite Error"); 
            } 
         } 
 
         taskDelay(6); 
         Index=0; 
         NsIec104Struct.SendBuffer[Index++]=0x68; 
         NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=100; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=10; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=21+Group; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0) 
          { 
	               perror ("Client Write Error"); 
          } 
 
} 
 
static void    NsIec104ProcessYcGroup(unsigned char Group) 
{ 
unsigned short   General,Index; 
unsigned char    *P; 
unsigned short   i,RealNumber,YcLibIndex; 
 
 
 
          /*  Once Gonce need send 60 Yc  Group Number is 8 9 10 11 begin from 8 */ 
          /*  First Send  Confirm */ 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
 
          NsIec104Struct.SendBuffer[Index++]=100; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=0x07; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=21+Group; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	               perror ("Client Write Error"); 
          } 
 
          taskDelay(60);           
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x00; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=9;                 /*    asdu type   */ 
          NsIec104Struct.SendBuffer[Index++]=64|0x80;           /*    information object count  */ 
 
           /*  REASONBYTE  now is two */ 
          NsIec104Struct.SendBuffer[Index++]=21+Group;          /*    reason  */ 
          NsIec104Struct.SendBuffer[Index++]=0x0;               /*    reason  */ 
 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
          /* Information Object Address now three byte*/ 
          General=0x701+Group*0x80; 
          P=(unsigned char    *)&General; 
          NsIec104Struct.SendBuffer[Index++]=P[0]; 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendBuffer[Index++]=0; 
 
          YcLibIndex=0; 
          switch(NsIec104Struct.Table) 
          { 
                 case 0: 
                      NsGetDd1Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64); 
                      break; 
                 case 1: 
                      NsGetDd2Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64); 
                      break; 
                 case 2: 
                      NsGetDd3Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64); 
                      break; 
                 case 3: 
                      NsGetDd4Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64); 
                      break; 
          } 
 
          for(i=0;i<64;i++) 
          { 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i]; 
              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i+1]; 
              NsIec104Struct.SendBuffer[Index++]=0x00;  /*qds*/ 
 
          } 
          /* Repair Length */ 
          NsIec104Struct.SendBuffer[1]=Index-2; 
          if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0) 
          { 
	               perror ("Client FirstWrite Error"); 
          } 
          /* 
            printf("\nYcGroupFirst=%d",Group); 
           */ 
          taskDelay(6); 
          Index=0; 
          NsIec104Struct.SendBuffer[Index++]=0x68; 
          NsIec104Struct.SendBuffer[Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          P=(unsigned char *)&RealNumber; 
          NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */ 
          NsIec104Struct.SendBuffer[Index++]=P[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.SendBuffer[Index++]=100; 
          NsIec104Struct.SendBuffer[Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.SendBuffer[Index++]=10; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.SendBuffer[Index++]=21+Group; 
 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0) 
          { 
	               perror ("Client Write Error"); 
          } 
          /* 
            printf("\nEndwrite=%d",NsIec104Struct.SendLength); 
           */ 
} 
 
 
static void    NsIec104ProcessCellGroup(unsigned char Group) 
{ 
 
 
 
} 
 
 
 
static void     NsIec104InterrogationAll(void) 
{ 
unsigned short    i,Index,start,g; 
         long     ShiftValue; 
unsigned char     *PGive,j,GroupIndex,YxByte,YxBit,*pYc; 
unsigned short    RealNumber,YxLibIndex,YcLibIndex; 
unsigned long     InformationObject; 
unsigned short	  sYc; 
float		  fYc; 
 
 
          /*  First Send  Confirm */ 
          GroupIndex=0; 
          NsIec104Struct.NeedSendNumber=0; 
          NsIec104Struct.HasSendNumber=0; 
          NsIec104Struct.LeftSendNumber=0; 
 
 
          Index=0; 
          NsIec104Struct.GroupSendBuffer[0][Index++]=0x68; 
          NsIec104Struct.GroupSendBuffer[0][Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          PGive=(unsigned char *)&RealNumber; 
          NsIec104Struct.GroupSendBuffer[0][Index++]=PGive[0];              /*     send number       */ 
          NsIec104Struct.GroupSendBuffer[0][Index++]=PGive[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
 
          NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
 
          NsIec104Struct.GroupSendBuffer[0][Index++]=100; 
          NsIec104Struct.GroupSendBuffer[0][Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.GroupSendBuffer[0][Index++]=0x07; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.GroupSendBuffer[0][Index++]=20; 
          NsIec104Struct.GroupSendLength[0]=Index; 
/* 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[0][0],NsIec104Struct.GroupSendLength[0],0x0))<0) 
          { 
	               perror ("Client Write Error"); 
          } 
*/ 
 
          /*  Second Send  Yx   total send 1024 yx  */ 
          GroupIndex++; 
          NsIec104Struct.NeedSendNumber++; 
 
          for(start=0;start<NS_IEC_104_TOTAL_YX_TIMES;start++) 
          { 
              Index=0; 
     
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e; 
 
              RealNumber=(NsIec104Struct.SendMeNumber<<1); 
              PGive=(unsigned char *)&RealNumber; 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[0];              /*     send number       */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[1]; 
              NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1;      /*    asdu type   */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NS_IEC_104_ONCE_YX|0x80;     /*    information object count  */ 
 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=20;    /*  reason  */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0;    /*  reason  */ 
 
              
              for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
                  NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
              InformationObject=0x01+start*NS_IEC_104_ONCE_YX; 
              PGive=(unsigned char *)&InformationObject; 
              for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
              { 
                  NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[i]; 
              } 
 
              YxLibIndex=start*NS_IEC_104_ONCE_YX_BYTES; 
              switch(NsIec104Struct.Table) 
              { 
                case 0: 
                       NsGetDd1Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES); 
                       break; 
                case 1: 
                       NsGetDd2Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES); 
                       break; 
                case 2: 
                       NsGetDd3Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES); 
                       break; 
                case 3: 
                       NsGetDd4Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES); 
                       break; 
              } 
              for(j=0;j<NS_IEC_104_ONCE_YX_BYTES;j++) 
              { 
                
                  YxByte=NsIec104Struct.DataBuff[j];  
                  for(g=0;g<8;g++) 
                  { 
                      YxBit=(YxByte>>g)&0x01; 
                      NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=YxBit; 
                  } 
              } 
 
                               /* 
                                  Debug Yx Message  
                                  for(j=0;j<NS_IEC_104_ONCE_YX;j++) 
                                  { 
                                    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x30; 
                                  } 
                               */ 
            
 
              /* Repair Length */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][1]=Index-2; 
              NsIec104Struct.GroupSendLength[GroupIndex]=Index; 
/* 
              if((NsIec104Struct.GroupSendLength[GroupIndex] = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength[GroupIndex],0x0)) < 0) 
              { 
	               perror ("Client Write Error"); 
              } 
*/ 
              NsIec104Struct.NeedSendNumber++; 
              GroupIndex++; 
          } 
           
          /*  Third  Send  Yc    */ 
          for(start=0;start<NS_IEC_104_TOTAL_YC_TIMES;start++) 
          { 
 
             Index=0; 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68; 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x00; 
 
             RealNumber=(NsIec104Struct.SendMeNumber<<1); 
             PGive=(unsigned char *)&RealNumber; 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[0];              /*     send number       */ 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[1]; 
             NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=9;                     /* 11->21   sigle yc asdu type   */ 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NS_IEC_104_ONCE_YC|0x80;               /*    information object count  */ 
 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=20;                    /*    reason  */ 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0;                   /*    reason  */ 
 
              
             for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
                 NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
              InformationObject=0x701+start*NS_IEC_104_ONCE_YC; 
              PGive=(unsigned char *)&InformationObject; 
              for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
              { 
                  NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[i]; 
              } 
 
 
              YcLibIndex=start*NS_IEC_104_ONCE_YC; 
              switch(NsIec104Struct.Table) 
              { 
                 case 0: 
                      NsGetDd1Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC); 
                      break; 
                 case 1: 
                      NsGetDd2Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC); 
                      break; 
                 case 2: 
                      NsGetDd3Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC); 
                      break; 
                 case 3: 
                      NsGetDd4Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC); 
                      break; 
              } 
 
              for(j=0;j<NS_IEC_104_ONCE_YC;j++) 
              { 
                  
		/*sYc = *((unsigned short *)&NsIec104Struct.DataBuff[j*2]); 
		fYc = sYc * 1.0; 
		pYc = (unsigned char *)&fYc;		 
		NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[0]; 
                 NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[1]; 
		NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[2]; 
		NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[3];*/ 
		NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[2*j]; 
		NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[2*j + 1]; 
                 NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0;                 /*  qds    */ 
              } 
              /* 
                  printf("\n Yc Send "); 
              */ 
 
              /* Repair Length */ 
              NsIec104Struct.GroupSendBuffer[GroupIndex][1]=Index-2; 
              NsIec104Struct.GroupSendLength[GroupIndex]=Index; 
/* 
              if((NsIec104Struct.GroupSendLength[GroupIndex]=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength[GroupIndex],0x0))<0) 
              { 
	               perror ("Client Write Error"); 
              } 
*/ 
              NsIec104Struct.NeedSendNumber++; 
              GroupIndex++; 
          } 
 
 
          /*  Fiveth Send  Over  */ 
          Index=0; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e; 
 
          RealNumber=(NsIec104Struct.SendMeNumber<<1); 
          PGive=(unsigned char *)&RealNumber; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[0];              /*     send number       */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[1]; 
          NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535; 
 
 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */ 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1]; 
 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=100; 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1; 
 
          for(i=0;i< NS_IEC_104_REASONBYTE;i++) 
               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=10; 
              
          for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++) 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i]; 
 
 
          for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++) 
             NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.InformationObject[i]; 
 
          NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=20; 
          NsIec104Struct.GroupSendLength[GroupIndex]=Index; 
/* 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength[GroupIndex],0x0)) < 0) 
          { 
	               perror ("Client Write Error"); 
          } 
*/ 
 
 
          NsIec104Struct.NeedSendNumber++; 
          GroupIndex++; 
 
 
} 
 
 
static  void  NsIec104ProcessFormatS(void) 
{ 
int Index; 
/* 
       printf("\nFormatS"); 
 
       Index=0; 
       NsIec104Struct.SendBuffer[0]=0x68; 
       NsIec104Struct.SendBuffer[1]=0x04; 
       NsIec104Struct.SendBuffer[2]=0x0b; 
       NsIec104Struct.SendBuffer[3]=0x00; 
       NsIec104Struct.SendBuffer[4]=0x00; 
       NsIec104Struct.SendBuffer[5]=0x00; 
 
       NsIec104Struct.ReceiveHimNumber[0]=NsIec104Struct.ReceiveBuffer[2]; 
       NsIec104Struct.ReceiveHimNumber[1]=NsIec104Struct.ReceiveBuffer[3]; 
 
      if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],6,0x0)) < 0) 
      { 
	               perror ("Client Write Error"); 
      } 
      printf("\nwrite=%d",NsIec104Struct.SendLength); 
	  */ 
} 
 
 
static  void  NsIec104ProcessFormatU(void) 
{ 
int Index; 
int i; 
 
 
       NsIec104Struct.UCommand=NsIec104Struct.ReceiveBuffer[2]; 
       Index=0; 
       if(NsIec104Struct.UCommand&NS_IEC_104_STARTDT) 
       { 
          Index=6; 
          NsIec104Struct.SendBuffer[0]=0x68; 
          NsIec104Struct.SendBuffer[1]=0x04; 
          NsIec104Struct.SendBuffer[2]=0x0b; 
          NsIec104Struct.SendBuffer[3]=0x00; 
          NsIec104Struct.SendBuffer[4]=0x00; 
          NsIec104Struct.SendBuffer[5]=0x00; 
          /* 
          printf("\nFormatU STARTDT"); 
          */ 
 
          NsIec104Struct.SendMeNumber=0x00; 
          NsIec104Struct.ReceiveHimNumber[0]=0x00;               
          NsIec104Struct.ReceiveHimNumber[1]=0x00; 
          /*NsIec104Struct.NeedSendNumber=0; 
          NsIec104Struct.HasSendNumber=0; 
          NsIec104Struct.LeftSendNumber=0;*/ 
 
       } 
       else  
       { 
          if(NsIec104Struct.UCommand&NS_IEC_104_STOPDT) 
          { 
               Index=6; 
               NsIec104Struct.SendBuffer[0]=0x68; 
               NsIec104Struct.SendBuffer[1]=0x04; 
               NsIec104Struct.SendBuffer[2]=0x23; 
               NsIec104Struct.SendBuffer[3]=0x00; 
               NsIec104Struct.SendBuffer[4]=0x00; 
               NsIec104Struct.SendBuffer[5]=0x00; 
 
               NsIec104Struct.SendMeNumber=0x00; 
               NsIec104Struct.ReceiveHimNumber[0]=0x00;               
               NsIec104Struct.ReceiveHimNumber[1]=0x00; 
               NsIec104Struct.NeedSendNumber=0; 
               NsIec104Struct.HasSendNumber=0; 
               NsIec104Struct.LeftSendNumber=0; 
 
 
          } 
          else  
          { 
                 if(NsIec104Struct.UCommand&NS_IEC_104_TESTFR) 
                 { 
                    Index=6; 
                    NsIec104Struct.SendBuffer[0]=0x68; 
                    NsIec104Struct.SendBuffer[1]=0x04; 
                    NsIec104Struct.SendBuffer[2]=0x83; 
                    NsIec104Struct.SendBuffer[3]=0x00; 
                    NsIec104Struct.SendBuffer[4]=0x00; 
                    NsIec104Struct.SendBuffer[5]=0x00; 
                 } 
          } 
 
       } 
       if(Index>0) 
       { 
          if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],6,0x0)) < 0) 
          { 
	               perror ("Client Write Error"); 
          } 
          /* 
          printf("\ns:"); 
          for (i = 0; i < 6; i++) 
          	printf("%4x",NsIec104Struct.SendBuffer[i]); 
          printf("\n"); 
          */ 
       } 
} 
 
 
 
static  void  NsIec104Reset(void) 
{ 
 
NsIec104Struct.ReceiveLength=0; 
NsIec104Struct.ReceiveIndexLength=0; 
NsIec104Struct.ReceiveIndex=0; 
NsIec104Struct.HasSendNumber=0; 
 
} 
 
 
 
 
static  void       NsIec104CycleCount(void) 
{ 
unsigned short i; 
unsigned long  Nap,NowSecond; 
 
 
 
  getforsecond(&NowSecond); 
  for(i=0;i<NS_IEC104_USE_CYCLE;i++)  
  { 
	Nap = DiffMsLong(NowSecond,NsIec104Cycle[i].LastTime); 
	if(Nap >= NsIec104Cycle[i].TimeNap)  
        { 
		NsIec104Cycle[i].TimeSign= ON; 
		getforsecond(&NsIec104Cycle[i].LastTime); 
	} 
  } 
} 
/* 
void debug104(void) 
{ 
unsigned char Buff[256]; 
short i; 
 
   for(i=0;i<200;i++) 
      Buff[i]=0; 
 
   for(i=0;i<96;i+=2) 
     Buff[i]=i+100; 
 
   NsPutDd1Lib(NS_YC_DATA,0,0,&Buff[0],48); 
 
      for(i=0;i<12;i++) 
         Buff[i]=0x0f;     
      NsPutDd1Lib(NS_YX_DATA,0,0,&Buff[0],12); 
} 
 
static void Iec104testCosSoe(void) { 
	struct tm NowTime; 
	static i = 0; 
	static unsigned char st = 0; 
	NS_COS_FORMAT	CosValue; 
	NS_SOE_FORMAT	SoeValue; 
 
	getvxTime(&NowTime); 
for(i=0;i<10;i++){ 
	SoeValue.SendNumber = i; 
	SoeValue.KgNumber = i; 
	SoeValue.CellNumber = 0; 
	SoeValue.RealAddress = 1; 
	SoeValue.Type = 2; 
			SoeValue.State = st; 
				 
			SoeValue.Year = 2003; 
			SoeValue.Month = 7; 
			SoeValue.Day = 22; 
			SoeValue.Hour = 15; 
				 
			SoeValue.Minute = 00; 
			SoeValue.Second = i; 
			SoeValue.Msecond = 888; 
 
				NsPutNetSoeLib(&SoeValue); 
				NsPutDd1SoeLib(&SoeValue); 
				NsPutDd2SoeLib(&SoeValue); 
				NsPutDd3SoeLib(&SoeValue); 
				NsPutAllSoeLib(&SoeValue); 
			 
		CosValue.SendNumber =i; 
		CosValue.KgNumber = i; 
		CosValue.CellNumber = 0; 
		CosValue.RealAddress = 1; 
		CosValue.Type = 2; 
		CosValue.State = st; 
		 
			NsPutNetCosLib(&CosValue); 
			NsPutDd1CosLib(&CosValue); 
			NsPutDd2CosLib(&CosValue); 
			NsPutDd3CosLib(&CosValue); 
			NsPutAllCosLib(&CosValue); 
 
} 
		if (st == 0) st = 1; 
		else  st = 0; 
} 
*/