NsIec104Server.c
上传用户:liao421
上传日期:2013-01-30
资源大小:11k
文件大小:92k
开发平台:

C/C++

  1. /*
  2.             Modify Record 
  3.             2002-11-16            Add Network Card Value Geting From nsc200.ini
  4.             2002-12-26            According   Nedd,adding Send chang yc
  5. */
  6. #include <socket.h>
  7. #include <sockLib.h>
  8. #include <stdlib.h>
  9. #include <sysLib.h>
  10. #include <logLib.h>
  11. #include <errno.h>
  12. #include <string.h> 
  13. #include <stdio.h> 
  14. #include <time.h>
  15. #include "wdLib.h"
  16. #include "in.h"
  17. #include "ioLib.h"
  18. #include "constant.h"
  19. #include "NsDataBase.h"
  20. #include "NsExtTime.h"
  21. #include "NsIoFunction.h"
  22. #include "NsCommon.h"
  23. #include "NsReadOnly.h"
  24. #include "ExtDataBase.h"
  25. #include "Project/NsIec104.h"
  26. static NS_IEC104_MAIN      NsIec104Struct;
  27.         void       NsNetServerProcess(void);
  28. static  void       NsIec104Process(void);
  29. static  void       NsIec104Init(unsigned char Card,short Port);
  30. static  void       NsIec104NetInit(void);
  31. static  void       NsIec104Reset(void);
  32. static  void       NsIec104ProcessFormatI(void);
  33. static  void       NsIec104ProcessFormatS(void);
  34. static  void       NsIec104ProcessFormatU(void);
  35. static  void       NsIec104Interrogation(void);
  36. static  void       NsIec104InterrogationGroup(void);
  37. static  void       NsIec104InterrogationAll(void);
  38. static  void       NsIec104ProcessYxGroup(unsigned char Group);
  39. static  void       NsIec104ProcessYcGroup(unsigned char Group);
  40. static  void       NsIec104ProcessCellGroup(unsigned char Group);
  41. static  void       NsIec104ProcessTime(void);
  42. static  void       NsIec104ProcessYkYt(void);
  43. static  void       NsIec104ProcessPulse(void);
  44. static  void       NsIec104DefaultGroup(unsigned char Group);
  45. static  void       NsIec104ProcessPulseData(void);
  46. static  void       NsIec104SendGroup(void);
  47. static  void       NsIec104SendYkMessage(short Who,unsigned char Action);
  48. static  void       NsIec104ReceiveYkReturn(void);
  49. static  void       NsIec104SendYkSelectConfirm(void);
  50. static  void       NsIec104SendYkExeConfirm(void);
  51. static  void       NsIec104SendYkEscConfirm(void);
  52. static  unsigned char        NsIec104SendCosSoe(void);
  53. static  void       NsIec104InitYk(void);
  54. static  void       NsIec104CycleCount(void);
  55. static  void       NsIec104SendChangeYc(void);
  56. /*
  57. static void     Iec104testCosSoe(void);
  58. void  debug104(void);
  59. */
  60. static  void       NsIec104Init(unsigned char Card,short Port)
  61. {
  62.  unsigned short    i;
  63.  int               Result;
  64.  char              Buff[32];
  65.  char              Ip0Buff[64],Ip0Route[64];
  66.  char              Ip1Buff[64],Ip1Route[64];
  67.  NS_SYSTEM_DATA_MODE SysData;
  68.     NsIec104Struct.LinkState=NS_DISCONNECTED;
  69.     NsIec104Struct.NsNewSocketId=0;
  70.     getforsecond(&NsIec104Struct.LinkValidTime);
  71.     NsIec104Struct.ReceiveIndex=0;  
  72.     NsIec104Struct.ReceiveIndexLength=0;  
  73.     NsIec104Struct.SendMeNumber=0;  
  74.     NsIec104Struct.HasSendNumber=0;
  75.     NsIec104Struct.NeedSendNumber=0;
  76.     NsIec104Struct.LeftSendNumber=0;
  77.     NsIec104Struct.Card=Card;
  78.     NsIec104Struct.Port=Port;
  79.     NsIec104Struct.YkStartSign=OFF;
  80.     NsIec104Struct.Table=0;
  81.     NsIec104Struct.NetRunSign=OFF;
  82.     NsIec104Struct.SetNetSign=OFF;
  83.     Result=ReadNsc200Ini("NsIec104","SetTimeFlag",Buff);
  84.     if(Result)
  85.     {
  86.         NsIec104Struct.SetTimeFlag=1;
  87.     }
  88.     for(i=0;i<NS_IEC104_USE_CYCLE;i++) 
  89.     {
  90. getforsecond(&NsIec104Cycle[i].LastTime);
  91. NsIec104Cycle[i].TimeNap= NsIec104Nap[i];
  92.         NsIec104Cycle[i].TimeSign=OFF; 
  93.     }
  94.     for(i=0;i<NS_IEC104_MAX_YC;i++)
  95.         NsIec104Struct.YcNap[i]=5;
  96.     NsIec104Struct.YcChangeCycle=0;
  97.     
  98.      NsIec104Struct.TableNo = NsGetCommTable(Port);
  99. GetSysDataMode(NsIec104Struct.TableNo,&SysData);
  100. /*NsIec104Struct.XYxNum  = SysData.NsYxTableNumber;
  101. NsIec104Struct.XYmNum  = SysData.NsYmTableNumber;*/
  102. NsIec104Struct.XYcNum  = SysData.NsYcTableNumber;
  103. NsIec104Struct.XYcCycleNum = (NsIec104Struct.XYcNum + NS_IEC104_ONCE_YC_COUNT - 1) / NS_IEC104_ONCE_YC_COUNT;
  104.        
  105. }
  106. static  void        NsIec104NetInit(void)
  107. {
  108.  int               Result;
  109.  char              Buff[32];
  110.  char              Ip0Buff[64],Ip0Route[64];
  111.  char              Ip1Buff[64],Ip1Route[64];
  112.     NsTcpServerInit(NsIec104Struct.Card,NsIec104Struct.Port,"10.14.127.200");
  113.     switch(NsIec104Struct.Card)
  114.     {
  115.         case 0:
  116.                 Result=ReadNsc200Ini("NsIec104","Route0Sign",Buff);
  117.                 if(Result)
  118.                 {
  119.                    NsReadIpAddress("NsIec104","Route0Net",Ip0Buff);     
  120.                    NsReadIpAddress("NsIec104","Route0Ip",Ip0Route);  
  121.                    NsAddRoute(Ip0Buff,Ip0Route);   
  122.                 }
  123.                 break;
  124.         case 1:
  125.                 Result=ReadNsc200Ini("NsIec104","Route1Sign",Buff);
  126.                 if(Result)
  127.                 {
  128.                    NsReadIpAddress("NsIec104","Route1Net",Ip1Buff);     
  129.                    NsReadIpAddress("NsIec104","Route1Ip",Ip1Route);     
  130.                    NsAddRoute(Ip1Buff,Ip1Route);   
  131.                 }
  132.                 break;
  133.         default:
  134.                 break;
  135.     }
  136. }
  137. static  void        NsIec104InitYk(void)
  138. {
  139.     NsIec104Struct.YkStartSign=OFF;
  140.     NsIec104Struct.YkNowState=0xff;
  141.     NsIec104Struct.YkCellNumber=0xff;
  142.     NsIec104Struct.YkKgNumber=0xff;
  143. }
  144. void NsNetServerProcessv(void)
  145. {
  146.     while(1)
  147.     {
  148.       taskDelay(88);
  149.     }
  150. }
  151. void NsNetServerProcess(void)
  152. {
  153.  unsigned long     Now,Nap;
  154.  unsigned short    Card;
  155.  int               Result;
  156.  char              Buff[32];
  157.  
  158.    Card=0;
  159.    Result=ReadNsc200Ini("NsTasks","NsServerCard",Buff);
  160.    if(Result)
  161.    {
  162.       Card=Result;
  163.       if(Card<=2&&Card>0)
  164.       {
  165.         Card-=1;
  166.       }
  167.    }
  168.    NsIec104Init(Card,2404);
  169.    for(;;)
  170.    {
  171.         NsIec104Struct.SystemState = NsGetSysParameter(NS_NSCC_STATE);
  172.         NsIec104Struct.NetRunSign  = NsNetConfigState();
  173.         if(NsIec104Struct.NetRunSign==ON&&NsIec104Struct.SetNetSign==OFF&&NsIec104Struct.SystemState==DUTY)
  174.         {
  175.            NsIec104NetInit();
  176.            NsIec104Struct.SetNetSign=ON;
  177.         }
  178.         if(NsIec104Struct.NetRunSign==ON&&NsIec104Struct.SetNetSign==ON&&NsIec104Struct.SystemState==DUTY)
  179.         {
  180.           getforsecond(&Now);
  181.           Nap=DiffMsLong(Now,NsIec104Struct.LinkValidTime);
  182.           if(Nap>NS_IEC104_PERMIT_TIME)
  183.           {
  184.              NsIec104Struct.LinkState=NS_DISCONNECTED;
  185.           }
  186.           if(NsIec104Struct.YkStartSign==ON)
  187.           {
  188.              Nap=DiffMsLong(Now,NsIec104Struct.YkStartTime);
  189.              if(Nap>NS_IEC104_YK_PERMIT_TIME)
  190.              {
  191.                  NsIec104InitYk();
  192.              }
  193.           }
  194.           if(NsIec104Struct.LinkState==NS_DISCONNECTED)
  195.           {
  196.               NsIec104Struct.NsNewSocketId=NsTcpConnect(NsIec104Struct.NsNewSocketId,NsIec104Struct.Card);
  197.               if(NsIec104Struct.NsNewSocketId>0)
  198.               {
  199.                  NsIec104Struct.LinkState=NS_CONNECTED;  
  200.                  NsIec104Struct.SendMeNumber=0;  
  201.                  getforsecond(&NsIec104Struct.LinkValidTime);
  202.               }
  203.               else 
  204.              {
  205.                  /*NsIec104Init(NsIec104Struct.Card,NsIec104Struct.Port);*/
  206.                 
  207.                 /*  NsTcpServerInit(NsIec104Struct.Card,NsIec104Struct.Port,"10.14.8.23");*/
  208.                  NsIec104Struct.LinkState=NS_DISCONNECTED;
  209.                  NsIec104Reset();
  210.                  taskDelay(866);
  211.              }
  212.           }  
  213.           else 
  214.           {
  215.            
  216.           NsIec104Struct.ReceiveIndexLength=recv(NsIec104Struct.NsNewSocketId,&NsIec104Struct.ReceiveBuffer[NsIec104Struct.ReceiveIndex],198,0);
  217.           
  218.              if(NsIec104Struct.ReceiveIndexLength>0)
  219.              {
  220.                 NsIec104Struct.ReceiveLength+=NsIec104Struct.ReceiveIndexLength;
  221.                 if(NsIec104Struct.ReceiveLength<NS_IEC104_RECEIVEBUFFER)
  222.                 {
  223.                     NsIec104Process();
  224.                 }
  225.                 else 
  226.                 {
  227.                     NsIec104Reset();
  228.                 }
  229.                 /*
  230.                 debug104();
  231. Iec104testCosSoe();
  232. */
  233.                 getforsecond(&NsIec104Struct.LinkValidTime);
  234.              }
  235.              
  236.              NsIec104ReceiveYkReturn();
  237.              NsIec104SendGroup();
  238.              if(NsIec104Struct.LeftSendNumber==0)
  239.              {
  240.                   NsIec104Struct.CosSign=NsIec104SendCosSoe();
  241.                   if(NsIec104Struct.CosSign==0)
  242.                   {
  243.                      NsIec104SendChangeYc();
  244.                   }
  245.              }
  246.           }
  247.           NsIec104CycleCount();
  248.       }
  249.       taskDelay(18);
  250.    }
  251. }
  252. static void  NsIec104SendChangeYc(void)
  253. {
  254. unsigned short   General,Index;
  255. unsigned char    *P;
  256. unsigned short   i,RealNumber;
  257.          short   *Pyc,YcValue,YcNap;
  258. float fYc;
  259.           Index=0;
  260.           NsIec104Struct.SendBuffer[Index++]=0x68;
  261.           NsIec104Struct.SendBuffer[Index++]=0x00;
  262.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  263.           P=(unsigned char *)&RealNumber;
  264.           NsIec104Struct.SendBuffer[Index++]=P[0];                                /*     send number       */
  265.           NsIec104Struct.SendBuffer[Index++]=P[1];
  266.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  267.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];  /*     receive number    */
  268.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  269.           NsIec104Struct.SendBuffer[Index++]=9;                        /*    asdu type   */
  270.           NsIec104Struct.SendBuffer[Index++]=0;                  /*    information object count No 7 Byte */
  271.            /*  REASONBYTE  now is two */
  272.           NsIec104Struct.SendBuffer[Index++]=3;                 /*    reason  */
  273.           NsIec104Struct.SendBuffer[Index++]=0x0;                      /*    reason  */
  274.           NsIec104Struct.SendBuffer[Index++]=0x01;
  275.           NsIec104Struct.SendBuffer[Index++]=0x00;
  276. /*             
  277.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  278.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  279. */
  280.           NsIec104Struct.YcLibIndex=NsIec104Struct.YcChangeCycle*NS_IEC104_ONCE_YC_COUNT;
  281.           /*NsIec104Struct.YcChangeCycle=(NsIec104Struct.YcChangeCycle+1)%NS_IEC104_CHANGE_YC_CYCLE;*/
  282.           NsIec104Struct.YcChangeCycle=(NsIec104Struct.YcChangeCycle+1)%NsIec104Struct.XYcCycleNum;
  283.           
  284.           NsIec104Struct.YcChangeCount=0;
  285.           switch(NsIec104Struct.Table)
  286.           {
  287.                  case 0:
  288.                       NsGetDd1Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT);
  289.                       break;
  290.                  case 1:
  291.                       NsGetDd2Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT);
  292.                       break;
  293.                  case 2:
  294.                       NsGetDd3Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT);
  295.                       break;
  296.                  case 3:
  297.                       NsGetDd4Lib(NS_YC_DATA,NsIec104Struct.YcLibIndex,NsIec104Struct.DataBuff,NS_IEC104_ONCE_YC_COUNT);
  298.                       break;
  299.           }
  300.           for(i=0;i<NS_IEC104_ONCE_YC_COUNT;i++)
  301.           {
  302.                  /*Pyc=(short *)&NsIec104Struct.DataBuff[2*i];
  303.                  YcValue= *Pyc;
  304.                  YcNap=YcValue-NsIec104Struct.YcLastValue[NsIec104Struct.YcLibIndex+i];
  305.                  if(YcNap<0)
  306.                       YcNap=NsIec104Struct.YcLastValue[NsIec104Struct.YcLibIndex+i]-YcValue;
  307.  
  308.                  if(YcNap>NsIec104Struct.YcNap[NsIec104Struct.YcLibIndex+i])
  309.                  {
  310.                       NsIec104Struct.YcChangeCount++;
  311.                       NsIec104Struct.YcLastValue[NsIec104Struct.YcLibIndex+i]=YcValue;*/    
  312.                       /* Information Object Address Now Three Bytes */
  313.                       General=0x701+NsIec104Struct.YcLibIndex+i;
  314.                       P=(unsigned char    *)&General;
  315.                       NsIec104Struct.SendBuffer[Index++]=P[0];
  316.                       NsIec104Struct.SendBuffer[Index++]=P[1];
  317.                       NsIec104Struct.SendBuffer[Index++]=0;
  318.                       /*fYc = YcValue * 1.0;
  319.       P=(unsigned char    *)&fYc;
  320.       NsIec104Struct.SendBuffer[Index++]=P[0];
  321.                       NsIec104Struct.SendBuffer[Index++]=P[1];
  322.         NsIec104Struct.SendBuffer[Index++]=P[2];
  323.                       NsIec104Struct.SendBuffer[Index++]=P[3];*/
  324.         NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i];
  325.                       NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i + 1];
  326.                       NsIec104Struct.SendBuffer[Index++]=0x00;  /*qds*/
  327.                   /*}*/
  328.           }
  329.           /* Repair Length  and Count*/
  330.           NsIec104Struct.SendBuffer[1]=Index-2;
  331.           NsIec104Struct.SendBuffer[7]=NsIec104Struct.YcChangeCount;
  332.           if(NsIec104Struct.YcChangeCount>0)
  333.           {/*
  334.              printf("n Co=%d Ind=%dGe=%d",NsIec104Struct.YcChangeCount,NsIec104Struct.YcLibIndex,General);
  335. */
  336.              if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  337.              {
  338.                perror ("Client FirstWrite Error");
  339.              }
  340.           }
  341. }
  342. static void  NsIec104SendGroup(void)
  343. {
  344.   if(NsIec104Struct.NeedSendNumber>0&&NsIec104Struct.HasSendNumber<NS_IEC104_MAX_K)
  345.   {
  346.      /*
  347.            printf("n GroupId=%d",NsIec104Struct.NsNewSocketId);
  348.      */
  349.      if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[NsIec104Struct.LeftSendNumber][0],NsIec104Struct.GroupSendLength[NsIec104Struct.LeftSendNumber],0x0))<0)
  350.      {
  351.         perror ("GRoup Erroe");
  352.      }
  353.      
  354.      NsIec104Struct.LeftSendNumber++;  
  355.      /* 
  356.        printf("n Send Group=%d",NsIec104Struct.SendLength);
  357.       */
  358.     if(NsIec104Struct.LeftSendNumber>=NsIec104Struct.NeedSendNumber)
  359.     {
  360.        NsIec104Struct.HasSendNumber=0;
  361.        NsIec104Struct.NeedSendNumber=0;
  362.        NsIec104Struct.LeftSendNumber=0;
  363.     }
  364.   }
  365.   taskDelay(108);
  366. }
  367. static void  NsIec104Process(void)
  368. {
  369.  int              i;
  370. /*      printf("r:");
  371.       for(i=0;i<NsIec104Struct.ReceiveLength;i++)
  372.           printf("%4x",NsIec104Struct.ReceiveBuffer[i]);
  373.       printf("n");
  374. */      
  375.     if(NsIec104Struct.ReceiveLength>=NS_IEC_104_MIN_LENGTH&&NsIec104Struct.ReceiveBuffer[0]==NS_IEC_104_HEAD)
  376.     {                            
  377.        NsIec104Struct.FormatType=NsIec104Struct.ReceiveBuffer[2]&0x03;
  378.        if(NsIec104Struct.FormatType==0||NsIec104Struct.FormatType==2)            /* I */
  379.        {
  380.               NsIec104ProcessFormatI();
  381.        }
  382.        else 
  383.        {
  384.               if(NsIec104Struct.FormatType==1)     /*  S  */ 
  385.                    NsIec104ProcessFormatS();
  386.               else
  387.               {
  388.                    if(NsIec104Struct.FormatType==3)/*  U  */
  389.                       NsIec104ProcessFormatU();
  390.               }
  391.         }
  392.         NsIec104Reset();
  393.     }
  394. }
  395. static  void  NsIec104ProcessFormatI(void)
  396. {
  397. int i;
  398. unsigned  char   *p;
  399. unsigned  short  NoWay;
  400. /* 
  401.        printf("nFormatI");
  402. */
  403.        NsIec104Struct.ApduLength=NsIec104Struct.ReceiveBuffer[1];
  404.        if(NsIec104Struct.ApduLength>NS_IEC_104_MIN_LENGTH)
  405.        {
  406.           NsIec104Struct.ReceiveHimNumber[0]=NsIec104Struct.ReceiveBuffer[2];
  407.           NsIec104Struct.ReceiveHimNumber[1]=NsIec104Struct.ReceiveBuffer[3];
  408.           NoWay=*(unsigned short *)&NsIec104Struct.ReceiveHimNumber[0];
  409.           NoWay>>=1;
  410.           NoWay+=1;
  411.           NoWay<<=1;
  412.           p=(unsigned char *)&NoWay;
  413.           NsIec104Struct.ReceiveHimNumber[0]=p[0];
  414.           NsIec104Struct.ReceiveHimNumber[1]=p[1];
  415.           NsIec104Struct.AsduType=NsIec104Struct.ReceiveBuffer[6];  
  416.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  417.              NsIec104Struct.TransmitReason[i]=NsIec104Struct.ReceiveBuffer[NS_STARTREASON+i];
  418.          
  419.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  420.              NsIec104Struct.CommonAsduAddress[i]=NsIec104Struct.ReceiveBuffer[NS_STARTASDUADDRESS+i];
  421.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  422.              NsIec104Struct.InformationObject[i]=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+i];
  423.           NsIec104Struct.AsduQualifier=NsIec104Struct.ReceiveBuffer[NS_STARTQUALIFIER];  
  424.           switch(NsIec104Struct.AsduType)
  425.           {
  426.              case 46:
  427.              case 47:
  428.                       NsIec104Struct.YkYtType=NsIec104Struct.AsduType;
  429.                       NsIec104ProcessYkYt();
  430.                       break;
  431.              case 100:  /* Interrogation */
  432.                       NsIec104Interrogation();
  433.                       break;
  434.              case 101:
  435.                       NsIec104ProcessPulse();
  436.                       break;
  437.              case 103:
  438.                       NsIec104ProcessTime();
  439.                       /*
  440.                         printf("n Time1");
  441.                        */
  442.                       break;
  443.              default:
  444.                       break;
  445.           }
  446.        }
  447. }
  448. static  unsigned  char       NsIec104SendCosSoe(void)
  449. {
  450. unsigned short     Number;
  451. NS_SOE_FORMAT      SoeData;
  452. NS_COS_FORMAT      CosData;   
  453. unsigned short     Index,SendCount;
  454. unsigned char      *P,i,CosSendSign;
  455. unsigned short     RealNumber,Object,Msecond;
  456.   Index=0;
  457.   CosSendSign=0;
  458.   Number=NsCheckDd1Cos();
  459.   if(Number>0)
  460.   {
  461.           if(Number>36)
  462.              Number=36;
  463.           SendCount=0;
  464.           Index=0;
  465.           NsIec104Struct.SendBuffer[Index++]=0x68;
  466.           NsIec104Struct.SendBuffer[Index++]=0x00;
  467.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  468.           P=(unsigned char *)&RealNumber;
  469.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  470.           NsIec104Struct.SendBuffer[Index++]=P[1];
  471.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  472.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  473.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  474.           NsIec104Struct.SendBuffer[Index++]=0x01;                 /*    0x01--->0x02 asdu type   */
  475.           NsIec104Struct.SendBuffer[Index++]=1;                  /*    information object count  */
  476.            /*  REASONBYTE  now is two */
  477.           NsIec104Struct.SendBuffer[Index++]=0x03;               /*    reason  */
  478.           NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */
  479.              
  480.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  481.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  482.           for(i=0;i<Number;i++)
  483.           {
  484.                if(NsGetDd1CosLib(&CosData)==ON)
  485.                {
  486.                  SendCount++;
  487.                  /* Information Object Address now three byte:  0xc01---0xc80 */
  488.                  Object=CosData.SendNumber+0x01;
  489.                  P=(unsigned char *)&Object;
  490.                  NsIec104Struct.SendBuffer[Index++]=P[0];
  491.                  NsIec104Struct.SendBuffer[Index++]=P[1];
  492.                  NsIec104Struct.SendBuffer[Index++]=0x00;
  493.                  NsIec104Struct.SendBuffer[Index++]=CosData.State;
  494.                }
  495.           }
  496.           
  497.           /* Repair Length */
  498.           NsIec104Struct.SendBuffer[7]=SendCount;  /* vsq number */
  499.           NsIec104Struct.SendBuffer[1]=Index-2;
  500.   }
  501.   else 
  502.   {
  503.       Number=NsCheckDd1Soe();
  504.       if(Number>0)
  505.       {
  506.          if(Number>18)
  507.             Number=18;
  508.           SendCount=0;
  509.           Index=0;
  510.           NsIec104Struct.SendBuffer[Index++]=0x68;
  511.           NsIec104Struct.SendBuffer[Index++]=0x00;
  512.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  513.           P=(unsigned char *)&RealNumber;
  514.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  515.           NsIec104Struct.SendBuffer[Index++]=P[1];
  516.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  517.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  518.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  519.           NsIec104Struct.SendBuffer[Index++]=30;                 /*    asdu type   */
  520.           NsIec104Struct.SendBuffer[Index++]=1;                  /*    information object count  */
  521.            /*  REASONBYTE  now is two */
  522.           NsIec104Struct.SendBuffer[Index++]=0x03;               /*    reason  */
  523.           NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */
  524.              
  525.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  526.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  527.           for(i=0;i<Number;i++)
  528.           {
  529.               if(NsGetDd1SoeLib(&SoeData)==ON)
  530.               {               
  531.                  SendCount++;
  532.                  /* Information Object Address now three byte:  0xc01---0xc80 */
  533.                  Object=SoeData.SendNumber+0x01;
  534.                  P=(unsigned char *)&Object;
  535.                  NsIec104Struct.SendBuffer[Index++]=P[0];
  536.                  NsIec104Struct.SendBuffer[Index++]=P[1];
  537.                  NsIec104Struct.SendBuffer[Index++]=0x00;
  538.                  NsIec104Struct.SendBuffer[Index++]=SoeData.State;
  539.                  Msecond=SoeData.Msecond+SoeData.Second*1000;
  540.                  P=(unsigned char *)&Msecond;
  541.                  NsIec104Struct.SendBuffer[Index++]=P[0];
  542.                  NsIec104Struct.SendBuffer[Index++]=P[1];
  543.                  NsIec104Struct.SendBuffer[Index++]=SoeData.Minute;
  544.                  NsIec104Struct.SendBuffer[Index++]=SoeData.Hour;
  545.                  NsIec104Struct.SendBuffer[Index++]=SoeData.Day;
  546.                  NsIec104Struct.SendBuffer[Index++]=SoeData.Month;
  547.                  NsIec104Struct.SendBuffer[Index++]=(SoeData.Year-2000);
  548.               }
  549.            }
  550.           /* Repair Length */
  551.           NsIec104Struct.SendBuffer[7]=SendCount;  /* vsq number */
  552.           NsIec104Struct.SendBuffer[1]=Index-2;
  553.       }
  554.       else 
  555.       {
  556.          if(NsIec104Cycle[0].TimeSign== ON)
  557.          {
  558.                     Index=0;
  559.                     NsIec104Struct.SendBuffer[Index++]=0x68;
  560.                     NsIec104Struct.SendBuffer[Index++]=0x04;
  561.                     NsIec104Struct.SendBuffer[Index++]=0x43;
  562.                     NsIec104Struct.SendBuffer[Index++]=0x00;
  563.                     NsIec104Struct.SendBuffer[Index++]=0x00;
  564.                     NsIec104Struct.SendBuffer[Index++]=0x00;
  565.                     NsIec104Cycle[0].TimeSign=OFF;
  566.          }
  567.       }
  568.   }
  569.   if(Index>0)
  570.   {
  571.      NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0);
  572.      CosSendSign=0x01;
  573.   }
  574.   return CosSendSign;
  575. }
  576. static  void       NsIec104ProcessYkYt(void)
  577. {
  578. unsigned char     YkInformation,YkReason,YkWhat;
  579. unsigned short    YkObject;
  580. unsigned char     Media,YkAction;
  581. NS_RELATION       Dear;
  582. /*
  583.      Information Object Address 0xb01---0xb80  
  584.  for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  585.              NsIec104Struct.TransmitReason[i]=NsIec104Struct.ReceiveBuffer[NS_STARTREASON+i];
  586. for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  587.              NsIec104Struct.InformationObject[i]=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+i];
  588. */
  589.  YkInformation=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+NS_IEC_104_INFORMATIONBYTE];
  590.  YkReason=NsIec104Struct.ReceiveBuffer[NS_STARTREASON];
  591.  YkObject=NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION]+NsIec104Struct.ReceiveBuffer[NS_STARTINFORMATION+1]*256;
  592.  YkObject-=0xb01;
  593.  YkWhat=0;  /* nothing */ 
  594.  if(YkReason==6)
  595.  {
  596.       if(YkInformation&0x80)    /* YkSelect  */
  597.           YkWhat=1;
  598.       else 
  599.           YkWhat=2;             /*  YkExe*/
  600.  }
  601.  else 
  602.      if(YkReason==8)  /* Esc*/
  603.         YkWhat=3;
  604.  switch(YkWhat)
  605.  {
  606.    case 1:  /* select */
  607.          NsIec104Struct.ReceiveYkId=YkObject;
  608.          YkAction=YkInformation&0x03;
  609.          switch(YkAction)
  610.          {
  611.            case  1:   /* Open */
  612.                   NsIec104Struct.YkCellAction=NS_TRIP;
  613.                   break;
  614.            case  2:    /*  close */
  615.                   NsIec104Struct.YkCellAction=NS_CLOSE;       
  616.                   break;     
  617.            default:
  618.                   break;
  619.          }
  620.          NsIec104Struct.YkCellNumber=NsGetYkCell(NS_YK_CELL_NUMBER,NsIec104Struct.ReceiveYkId);
  621.          NsIec104Struct.YkKgNumber=NsGetYkCell(NS_YK_CELL_KG,NsIec104Struct.ReceiveYkId);
  622.          NsIec104Struct.YkNowState=NS_YK_SELECT;
  623.          Media=NsGetYkCell(NS_YK_CELL_CPU,NsIec104Struct.ReceiveYkId); 
  624.          Dear.NsYkPort=NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT;
  625.          Dear.NsYkSlavePort=Media;
  626.          NsPutDear(&Dear,6);
  627.          NsIec104SendYkMessage(NS_YK_SELECT,NsIec104Struct.YkCellAction);
  628.          NsIec104Struct.YkStartSign=ON;
  629.          getforsecond(&NsIec104Struct.YkStartTime);
  630.          /*
  631.             printf("n Send Yk SelectId=%d Cell=%d Kg=%d",NsIec104Struct.ReceiveYkId,NsIec104Struct.YkCellNumber,NsIec104Struct.YkKgNumber);
  632.           */
  633.          break;
  634.    case 2:                   /*  YkExe   */
  635.          NsIec104Struct.ReceiveYkId=YkObject;
  636.          YkAction=YkInformation&0x03;
  637.          switch(YkAction)
  638.          {
  639.             case  1:   /* Open */
  640.                   NsIec104Struct.YkCellAction=NS_TRIP;
  641.                   break;
  642.             case  2:    /*  close */
  643.                   NsIec104Struct.YkCellAction=NS_CLOSE;       
  644.                   break;     
  645.             default:
  646.                   NsIec104Struct.YkCellAction=0xff;       
  647.                   break;
  648.           }
  649.           NsIec104Struct.YkCellNumber=NsGetYkCell(NS_YK_CELL_NUMBER,NsIec104Struct.ReceiveYkId);
  650.           NsIec104Struct.YkKgNumber=NsGetYkCell(NS_YK_CELL_KG,NsIec104Struct.ReceiveYkId);
  651.           NsIec104Struct.YkNowState=NS_YK_EXE;
  652.           Media=NsGetYkCell(NS_YK_CELL_CPU,NsIec104Struct.ReceiveYkId); 
  653.           Dear.NsYkPort=NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT;
  654.           Dear.NsYkSlavePort=Media;
  655.           NsPutDear(&Dear,6);
  656.           NsIec104SendYkMessage(NS_YK_EXE,NsIec104Struct.YkCellAction);
  657.           NsIec104Struct.YkStartSign=ON;
  658.           getforsecond(&NsIec104Struct.YkStartTime);
  659.           /*
  660.            printf("n Send Yk ExeId=%d Cell=%d Kg=%d",NsIec104Struct.ReceiveYkId,NsIec104Struct.YkCellNumber,NsIec104Struct.YkKgNumber);
  661.            */
  662.           break;
  663.    case 3:
  664.           NsIec104Struct.ReceiveYkId=YkObject;
  665.           YkAction=YkInformation&0x03;
  666.           switch(YkAction)
  667.           {
  668.             case  1:   /* Open */
  669.                   NsIec104Struct.YkCellAction=NS_TRIP;
  670.                   break;
  671.             case  2:    /*  close */
  672.                   NsIec104Struct.YkCellAction=NS_CLOSE;       
  673.                   break;     
  674.             default:
  675.                   NsIec104Struct.YkCellAction=0xff;       
  676.                   break;
  677.           }
  678.           NsIec104Struct.YkCellNumber=NsGetYkCell(NS_YK_CELL_NUMBER,NsIec104Struct.ReceiveYkId);
  679.           NsIec104Struct.YkKgNumber=NsGetYkCell(NS_YK_CELL_KG,NsIec104Struct.ReceiveYkId);
  680.           NsIec104Struct.YkNowState=NS_YK_ESC;
  681.           Media=NsGetYkCell(NS_YK_CELL_CPU,NsIec104Struct.ReceiveYkId); 
  682.           Dear.NsYkPort=NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT;
  683.           Dear.NsYkSlavePort=Media;
  684.           NsPutDear(&Dear,6);
  685.           NsIec104SendYkMessage(NS_YK_ESC,NsIec104Struct.YkCellAction);
  686.           NsIec104Struct.YkStartSign=ON;
  687.           getforsecond(&NsIec104Struct.YkStartTime);
  688.           /*
  689.            printf("n Send Yk EscId=%d Cell=%d Kg=%d",NsIec104Struct.ReceiveYkId,NsIec104Struct.YkCellNumber,NsIec104Struct.YkKgNumber);
  690.            */
  691.           break;
  692.    default:
  693.           break;
  694.   } 
  695. }
  696. static void NsIec104SendYkMessage(short Who,unsigned char Action)
  697. {
  698.      char Dd1Message[16];
  699.      switch(Who)
  700.      {
  701.        case  NS_YK_ESC:
  702.              Dd1Message[0]=SEND_YK_HOST_DD1;     
  703.              Dd1Message[1]=NS_YK_ESC;         
  704.              Dd1Message[2]=NsIec104Struct.YkCellNumber;
  705.              Dd1Message[3]=NsIec104Struct.YkKgNumber;
  706.              Dd1Message[4]=NsIec104Struct.YkCellAction;
  707.              NsHostSendYkMessage(SEND_YK_HOST_DD1,8,Dd1Message);
  708.              break;
  709.        case  NS_YK_EXE:
  710.              Dd1Message[0]=SEND_YK_HOST_DD1;  
  711.              Dd1Message[1]=NS_YK_EXE;      
  712.              Dd1Message[2]=NsIec104Struct.YkCellNumber;
  713.              Dd1Message[3]=NsIec104Struct.YkKgNumber;
  714.              Dd1Message[4]=NsIec104Struct.YkCellAction;
  715.              NsHostSendYkMessage(SEND_YK_HOST_DD1,8,Dd1Message);
  716.              break;
  717.        case  NS_YK_SELECT:
  718.              Dd1Message[0]=SEND_YK_HOST_DD1;  
  719.              Dd1Message[1]=NS_YK_SELECT;      
  720.              Dd1Message[2]=NsIec104Struct.YkCellNumber;
  721.              Dd1Message[3]=NsIec104Struct.YkKgNumber;
  722.              Dd1Message[4]=NsIec104Struct.YkCellAction;
  723.              NsHostSendYkMessage(SEND_YK_HOST_DD1,8,Dd1Message);
  724.              break;
  725.      }
  726. }
  727. static void NsIec104ReceiveYkReturn(void)
  728. {
  729.            char    ReturnMessage[16];
  730.            int     Length,HasMessage;
  731.   NS_RELATION      Dear;
  732.    if(NsIec104Struct.YkStartSign==ON)
  733.    {
  734.        NsGetDear(&Dear);
  735.        if(Dear.NsYkPort==(NsIec104Struct.Card+NS_IEC_104_YK_BASE_PORT))
  736.        {
  737.           /*
  738.             printf("n Yk Return");
  739.            */
  740.           if(NsHostReceiveYkMessage(SEND_YK_HOST_DD1,ReturnMessage)==1)
  741.           {
  742.                /*      
  743.                            printf("nYkMes0=%0x--1=%0x--2=%0x---3=%0x---4=%0x",
  744.                            ReturnMessage[0],ReturnMessage[1],ReturnMessage[2],ReturnMessage[3],ReturnMessage[4]);
  745.                */
  746.                      
  747.               if(ReturnMessage[0]==SEND_YK_HOST_DD1)
  748.               { 
  749.                  if(NsIec104Struct.YkNowState==ReturnMessage[1])
  750.                  {
  751.                     if((ReturnMessage[2]==NsIec104Struct.YkCellNumber)&&
  752.                        (ReturnMessage[3]==NsIec104Struct.YkKgNumber)&&
  753.                        (ReturnMessage[4]==NsIec104Struct.YkCellAction))
  754.                     {
  755.                          NsIec104Struct.YkError=ReturnMessage[5];
  756.                          if(NsIec104Struct.YkNowState==NS_YK_SELECT)
  757.                          {   
  758.                             NsIec104SendYkSelectConfirm();
  759.                             /*
  760.                                printf("n Selec Confim");
  761.                              */
  762.                          }
  763.                          else 
  764.                          {
  765.                            if(NsIec104Struct.YkNowState==NS_YK_EXE)
  766.                               NsIec104SendYkExeConfirm();
  767.                            else 
  768.                            {
  769.                               if(NsIec104Struct.YkNowState==NS_YK_ESC)
  770.                                  NsIec104SendYkEscConfirm();
  771.                            }
  772.                          }
  773.                      } 
  774.                  }
  775.               }
  776.            }
  777.         }
  778.    }
  779. }
  780. static  void       NsIec104SendYkSelectConfirm(void)
  781. {
  782. unsigned short   YkObject,Index;
  783. unsigned char    *P,i;
  784. unsigned short   RealNumber;
  785.           Index=0;
  786.           NsIec104Struct.SendBuffer[Index++]=0x68;
  787.           NsIec104Struct.SendBuffer[Index++]=0x00;
  788.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  789.           P=(unsigned char *)&RealNumber;
  790.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  791.           NsIec104Struct.SendBuffer[Index++]=P[1];
  792.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  793.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  794.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  795.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;          /*    asdu type   */
  796.           NsIec104Struct.SendBuffer[Index++]=1;           /*    information object count  */
  797.            /*  REASONBYTE  now is two */
  798.           NsIec104Struct.SendBuffer[Index++]=0x07;          /*    reason  */
  799.           NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */
  800.              
  801.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  802.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  803.           /* Information Object Address now three byte:  0xc01---0xc80 */
  804.           YkObject=NsIec104Struct.ReceiveYkId+0xb01;
  805.           P=(unsigned char *)&YkObject;
  806.           NsIec104Struct.SendBuffer[Index++]=P[0];
  807.           NsIec104Struct.SendBuffer[Index++]=P[1];
  808.           NsIec104Struct.SendBuffer[Index++]=0x00;
  809.               /* Value  */
  810.           if(NsIec104Struct.YkCellAction==NS_TRIP)
  811.              NsIec104Struct.SendBuffer[Index++]=0x81;
  812.           else 
  813.                if(NsIec104Struct.YkCellAction==NS_CLOSE)
  814.                   NsIec104Struct.SendBuffer[Index++]=0x82;
  815.                else 
  816.                   NsIec104Struct.YkCellAction=0xff;       
  817.          
  818.           /* Repair Length */
  819.           NsIec104Struct.SendBuffer[1]=Index-2;
  820.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  821.           {
  822.               perror ("Client FirstWrite Error");
  823.           }
  824. }
  825. static  void       NsIec104SendYkExeConfirm(void)
  826. {
  827. unsigned short   YkObject,Index;
  828. unsigned char    *P,i;
  829. unsigned short   RealNumber;
  830.           /* YkExe Confirm */
  831.           Index=0;
  832.           NsIec104Struct.SendBuffer[Index++]=0x68;
  833.           NsIec104Struct.SendBuffer[Index++]=0x00;
  834.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  835.           P=(unsigned char *)&RealNumber;
  836.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  837.           NsIec104Struct.SendBuffer[Index++]=P[1];
  838.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  839.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  840.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  841.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;                 /*    asdu type   */
  842.           NsIec104Struct.SendBuffer[Index++]=1;           /*    information object count  */
  843.            /*  REASONBYTE  now is two */
  844.           NsIec104Struct.SendBuffer[Index++]=0x07;          /*    reason  */
  845.           NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */
  846.              
  847.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  848.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  849.           /* Information Object Address now three byte:  0xc01---0xc80 */
  850.           YkObject=NsIec104Struct.ReceiveYkId+0xb01;
  851.           P=(unsigned char *)&YkObject;
  852.           NsIec104Struct.SendBuffer[Index++]=P[0];
  853.           NsIec104Struct.SendBuffer[Index++]=P[1];
  854.           NsIec104Struct.SendBuffer[Index++]=0x00;
  855.               /* Value  */
  856.           if(NsIec104Struct.YkCellAction==NS_TRIP)
  857.              NsIec104Struct.SendBuffer[Index++]=0x01;
  858.           else 
  859.                if(NsIec104Struct.YkCellAction==NS_CLOSE)
  860.                   NsIec104Struct.SendBuffer[Index++]=0x02;
  861.                else 
  862.                   NsIec104Struct.YkCellAction=0xff;       
  863.          
  864.           /* Repair Length */
  865.           NsIec104Struct.SendBuffer[1]=Index-2;
  866.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  867.           {
  868.               perror ("Client FirstWrite Error");
  869.           }
  870.           taskDelay(100);
  871.           /* YkExe  End */
  872.           Index=0;
  873.           NsIec104Struct.SendBuffer[Index++]=0x68;
  874.           NsIec104Struct.SendBuffer[Index++]=0x00;
  875.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  876.           P=(unsigned char *)&RealNumber;
  877.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  878.           NsIec104Struct.SendBuffer[Index++]=P[1];
  879.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  880.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  881.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  882.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;                 /*    asdu type   */
  883.           NsIec104Struct.SendBuffer[Index++]=1;           /*    information object count  */
  884.            /*  REASONBYTE  now is two */
  885.           NsIec104Struct.SendBuffer[Index++]=10;          /*    reason  */
  886.           NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */
  887.              
  888.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  889.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  890.           /* Information Object Address now three byte:  0xc01---0xc80 */
  891.           YkObject=NsIec104Struct.ReceiveYkId+0xb01;
  892.           P=(unsigned char *)&YkObject;
  893.           NsIec104Struct.SendBuffer[Index++]=P[0];
  894.           NsIec104Struct.SendBuffer[Index++]=P[1];
  895.           NsIec104Struct.SendBuffer[Index++]=0x00;
  896.               /* Value  */
  897.           if(NsIec104Struct.YkCellAction==NS_TRIP)
  898.              NsIec104Struct.SendBuffer[Index++]=0x01;
  899.           else 
  900.                if(NsIec104Struct.YkCellAction==NS_CLOSE)
  901.                   NsIec104Struct.SendBuffer[Index++]=0x02;
  902.                else 
  903.                   NsIec104Struct.YkCellAction=0xff;       
  904.          
  905.           /* Repair Length */
  906.           NsIec104Struct.SendBuffer[1]=Index-2;
  907.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  908.           {
  909.               perror ("Client FirstWrite Error");
  910.           }
  911.           taskDelay(8);
  912. }
  913. static  void       NsIec104SendYkEscConfirm(void)
  914. {
  915. unsigned short   YkObject,Index;
  916. unsigned char    *P,i;
  917. unsigned short   RealNumber;
  918.           Index=0;
  919.           NsIec104Struct.SendBuffer[Index++]=0x68;
  920.           NsIec104Struct.SendBuffer[Index++]=0x00;
  921.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  922.           P=(unsigned char *)&RealNumber;
  923.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  924.           NsIec104Struct.SendBuffer[Index++]=P[1];
  925.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  926.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  927.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  928.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.YkYtType;                 /*    asdu type   */
  929.           NsIec104Struct.SendBuffer[Index++]=1;                  /*    information object count  */
  930.            /*  REASONBYTE  now is two */
  931.           NsIec104Struct.SendBuffer[Index++]=0x09;               /*    reason  */
  932.           NsIec104Struct.SendBuffer[Index++]=0x00;               /*    reason  */
  933.              
  934.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  935.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  936.           /* Information Object Address now three byte:  0xc01---0xc80 */
  937.           YkObject=NsIec104Struct.ReceiveYkId+0xb01;
  938.           P=(unsigned char *)&YkObject;
  939.           NsIec104Struct.SendBuffer[Index++]=P[0];
  940.           NsIec104Struct.SendBuffer[Index++]=P[1];
  941.           NsIec104Struct.SendBuffer[Index++]=0x00;
  942.               /* Value  */
  943.           if(NsIec104Struct.YkCellAction==NS_TRIP)
  944.              NsIec104Struct.SendBuffer[Index++]=0x81;
  945.           else 
  946.                if(NsIec104Struct.YkCellAction==NS_CLOSE)
  947.                   NsIec104Struct.SendBuffer[Index++]=0x82;
  948.                else 
  949.                   NsIec104Struct.YkCellAction=0xff;       
  950.          
  951.           /* Repair Length */
  952.           NsIec104Struct.SendBuffer[1]=Index-2;
  953.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  954.           {
  955.               perror ("Client FirstWrite Error");
  956.           }
  957. }
  958. static  void       NsIec104ProcessPulseAck(void)
  959. {
  960. unsigned short   i,Index,RealNumber;
  961. unsigned char    *P;
  962.           /*  First Send  Confirm */
  963.           Index=0;
  964.           NsIec104Struct.SendBuffer[Index++]=0x68;
  965.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  966.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  967.           P=(unsigned char *)&RealNumber;
  968.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  969.           NsIec104Struct.SendBuffer[Index++]=P[1];
  970.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  971.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  972.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  973.           NsIec104Struct.SendBuffer[Index++]=101;
  974.           NsIec104Struct.SendBuffer[Index++]=1;
  975.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  976.                NsIec104Struct.SendBuffer[Index++]=0x07;
  977.              
  978.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  979.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  980.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  981.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  982.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.Qcc;
  983.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  984.           {
  985.                perror ("Client Write Error");
  986.           }
  987. }
  988. static  void       NsIec104ProcessPulseEnd(void)
  989. {
  990. unsigned short   i,Index,RealNumber;
  991. unsigned char    *P;
  992.           /*  end confirm*/
  993.           taskDelay(16);
  994.           Index=0;
  995.           NsIec104Struct.SendBuffer[Index++]=0x68;
  996.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  997.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  998.           P=(unsigned char *)&RealNumber;
  999.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1000.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1001.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1002.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1003.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1004.           NsIec104Struct.SendBuffer[Index++]=101;
  1005.           NsIec104Struct.SendBuffer[Index++]=1;
  1006.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1007.                NsIec104Struct.SendBuffer[Index++]=10;
  1008.              
  1009.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1010.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1011.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1012.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1013.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.Qcc;
  1014.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0)
  1015.           {
  1016.                perror ("Client Write Error");
  1017.           }
  1018. }
  1019. static  void       NsIec104ProcessPulse(void)
  1020. {
  1021. unsigned short   General,Index;
  1022. unsigned char    *P;
  1023. unsigned short    i,j,RealNumber;
  1024.      NsIec104Struct.Qcc=NsIec104Struct.ReceiveBuffer[NS_IEC_104_QCC_POSITION];  
  1025.      NsIec104Struct.FreezeSign=NsIec104Struct.Qcc&0xc0;
  1026.      
  1027.      NsIec104Struct.FreezeSign>>=6;
  1028.      NsIec104Struct.PulseGroup=NsIec104Struct.Qcc&0x3f;
  1029.      switch(NsIec104Struct.FreezeSign)
  1030.      {
  1031.         case 0:
  1032.                NsIec104ProcessPulseData();
  1033.                break;
  1034.         case 1:
  1035.         case 2:
  1036.         case 3:
  1037.                NsIec104ProcessPulseAck();
  1038.                break;
  1039.      }           
  1040. }
  1041. static  void       NsIec104ProcessPulseGroup(void)
  1042. {
  1043. unsigned short   General,Index;
  1044. unsigned char    *P,start;
  1045. unsigned short    i,j,RealNumber,YmLib;
  1046.      for(start=0;start<2;start++)
  1047.      {
  1048.           Index=0;
  1049.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1050.           NsIec104Struct.SendBuffer[Index++]=0x00;
  1051.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1052.           P=(unsigned char *)&RealNumber;
  1053.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1054.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1055.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1056.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1057.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1058.           NsIec104Struct.SendBuffer[Index++]=15;                 /*    asdu type   */
  1059.           NsIec104Struct.SendBuffer[Index++]=16;           /*    information object count  */
  1060.            /*  REASONBYTE  now is two */
  1061.           NsIec104Struct.SendBuffer[Index++]=37+NsIec104Struct.PulseGroup;          /*    reason  */
  1062.           NsIec104Struct.SendBuffer[Index++]=0x0;               /*    reason  */
  1063.              
  1064.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1065.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1066.           YmLib=start*16+32*(NsIec104Struct.PulseGroup-1);
  1067.           switch(NsIec104Struct.Table)
  1068.           {
  1069.            case 0:
  1070.                  NsGetDd1Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16);
  1071.                  break;
  1072.            case 1:
  1073.                  NsGetDd2Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16);
  1074.                  break;
  1075.            case 2:
  1076.                  NsGetDd3Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16);
  1077.                  break;
  1078.            case 3:
  1079.                  NsGetDd4Lib(NS_YM_DATA,YmLib,NsIec104Struct.DataBuff,16);
  1080.                  break;
  1081.           }
  1082.           for(i=0;i<16;i++)
  1083.           {
  1084.               /* Information Object Address now three byte:  0xc01---0xc80 */
  1085.               NsIec104Struct.SendBuffer[Index++]=0x80+i+start*16+32*(NsIec104Struct.PulseGroup-1);
  1086.               NsIec104Struct.SendBuffer[Index++]=0x0c;
  1087.               NsIec104Struct.SendBuffer[Index++]=0;
  1088.               /* Value  */
  1089.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i];
  1090.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i+1];
  1091.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i+2];
  1092.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[4*i+3];
  1093.               /* sequence number  */
  1094.               NsIec104Struct.SendBuffer[Index++]=start*16+i;
  1095.           }  
  1096.           /* Repair Length */
  1097.           NsIec104Struct.SendBuffer[1]=Index-2;
  1098.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1099.           {
  1100.                perror ("Client FirstWrite Error");
  1101.           }
  1102.      }
  1103. }
  1104. static  void       NsIec104ProcessPulseAll(void)
  1105. {
  1106. unsigned short      General,Index;
  1107. unsigned char       *P,GroupIndex,start;
  1108. unsigned short      i,j,RealNumber;
  1109. unsigned long       InformationObject;
  1110.    GroupIndex=0;
  1111.    NsIec104Struct.NeedSendNumber=0;
  1112.    NsIec104Struct.HasSendNumber=0;
  1113.    NsIec104Struct.LeftSendNumber=0;
  1114.    /*  First Send  Confirm */
  1115.    Index=0;
  1116.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68;
  1117.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e;
  1118.    RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1119.    P=(unsigned char *)&RealNumber;
  1120.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];              /*     send number       */
  1121.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1];
  1122.    NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1123.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1124.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1125.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=101;
  1126.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1;
  1127.    for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1128.        NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x07;
  1129.              
  1130.    for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1131.        NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1132.    for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1133.        NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.InformationObject[i];
  1134.    NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.Qcc;
  1135.    NsIec104Struct.GroupSendLength[GroupIndex]=Index;
  1136.    GroupIndex++;
  1137.    NsIec104Struct.NeedSendNumber++;
  1138.    
  1139.    for(start=0;start<NS_IEC_104_TOTAL_YM_TIMES;start++)           /* total 128 ym send 8 times */            
  1140.    {
  1141.           Index=0;
  1142.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68;
  1143.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x00;
  1144.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1145.           P=(unsigned char *)&RealNumber;
  1146.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];              /*     send number       */
  1147.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1];
  1148.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1149.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1150.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1151.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=15;                            /*    asdu type   */
  1152.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NS_IEC_104_ONCE_YM ;           /*    information object count  */
  1153.            /*  REASONBYTE  now is two */
  1154.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=37  ;          /*    reason  */
  1155.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0;               /*    reason  */
  1156.              
  1157.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1158.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1159.           switch(NsIec104Struct.Table)
  1160.           {
  1161.            case 0:
  1162.                  NsGetDd1Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM);
  1163.                  break;
  1164.            case 1:
  1165.                  NsGetDd2Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM);
  1166.                  break;
  1167.            case 2:
  1168.                  NsGetDd3Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM);
  1169.                  break;
  1170.            case 3:
  1171.                  NsGetDd4Lib(NS_YM_DATA,start*NS_IEC_104_ONCE_YM,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YM);
  1172.                  break;
  1173.           }
  1174.           for(i=0;i<NS_IEC_104_ONCE_YM;i++)
  1175.           {
  1176.               /* Information Object Address now three byte:  0xc01---0xc80 */
  1177.               InformationObject=0xc01+i+start*NS_IEC_104_ONCE_YM;
  1178.               P=(unsigned char *)&InformationObject;
  1179.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];
  1180.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1];
  1181.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[2];
  1182.               /* Value  */
  1183.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i];
  1184.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i+1];
  1185.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i+2];
  1186.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[4*i+3];
  1187.               /* sequence number  */
  1188.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=i;
  1189.           }  
  1190.           /* Repair Length */
  1191.           NsIec104Struct.GroupSendBuffer[GroupIndex][1]=Index-2;
  1192.           NsIec104Struct.GroupSendLength[GroupIndex]=Index;
  1193.           /*
  1194.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength,0x0))<0)
  1195.           {
  1196.                perror ("Client FirstWrite Error");
  1197.           }
  1198.            */
  1199.           GroupIndex++;
  1200.           NsIec104Struct.NeedSendNumber++;
  1201.     }
  1202.     /* Send Pulse End Packet */
  1203.     Index=0;
  1204.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68;
  1205.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e;
  1206.     RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1207.     P=(unsigned char *)&RealNumber;
  1208.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[0];              /*     send number       */
  1209.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=P[1];
  1210.     NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1211.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1212.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1213.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=101;
  1214.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1;
  1215.     for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1216.         NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=10;
  1217.              
  1218.     for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1219.         NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1220.     for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1221.         NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.InformationObject[i];
  1222.     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.Qcc;
  1223.     NsIec104Struct.GroupSendLength[GroupIndex]=Index;
  1224.     /*
  1225.     if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength,0x0)) < 0)
  1226.     {
  1227.                perror ("Client Write Error");
  1228.     }
  1229.     */
  1230. /* mask 2003-01-24
  1231.     GroupIndex++;
  1232.     NsIec104Struct.NeedSendNumber++;
  1233. */
  1234. }
  1235. static  void       NsIec104ProcessPulseData(void)
  1236. {
  1237.      switch(NsIec104Struct.PulseGroup)
  1238.      {
  1239.         case 1:
  1240.         case 2:
  1241.         case 3:
  1242.         case 4:
  1243.                NsIec104ProcessPulseAck();
  1244.                NsIec104ProcessPulseGroup();
  1245.                NsIec104ProcessPulseEnd();
  1246.                break;
  1247.         case 5:
  1248.                NsIec104ProcessPulseAll();
  1249.                break;
  1250.      }
  1251. }
  1252. static  void     NsIec104ProcessTime(void)
  1253. {
  1254. unsigned short   i,Index,RealNumber,Mmsecond;
  1255. unsigned char    *P,Year,Month,Day,Hour,Minute,Second;
  1256. struct tm        NowTime;
  1257.           /*  First Send  Confirm */
  1258.           for(i=0;i<7;i++)
  1259.           {
  1260.                NsIec104Struct.TimeSave[i]=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+i];
  1261.           /*     
  1262.                    printf("n Time=%d",NsIec104Struct.TimeSave[i]);
  1263.             */   
  1264.           }
  1265.           Mmsecond=(NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION]
  1266.                     +256*NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+1])%1000; 
  1267.           Second=(NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION]
  1268.                     +256*NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+1])/1000; 
  1269.           Minute=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+2]&0x3f;
  1270.           Hour=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+3]&0x1f;
  1271.           Day=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+4]&0x1f;
  1272.           Month=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+5]&0x0f;
  1273.           Year=NsIec104Struct.ReceiveBuffer[NS_IEC_104_TIME_POSITION+6]&0x7f;
  1274. /*
  1275.           if(NsIec104Struct.SetTimeFlag)
  1276.           {
  1277.               NowTime.tm_sec   = Second;
  1278.               NowTime.tm_min   = Minute;
  1279.               NowTime.tm_hour  = Hour;
  1280.               NowTime.tm_mday  = Day;
  1281.               NowTime.tm_mon   = Month; 
  1282.               NowTime.tm_year  = Year + 2000; 
  1283.               setvxTime(&NowTime);
  1284.           }
  1285. */
  1286.           /*
  1287.               printf("n Year=%d Month=%d Day=%d Hour=%d Min=%d Sec=%d Msec=%d",
  1288.                          Year,Month,Day,Hour,Minute,Second,Mmsecond);
  1289.            */
  1290.           Index=0;
  1291.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1292.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  1293.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1294.           P=(unsigned char *)&RealNumber;
  1295.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1296.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1297.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1298.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1299.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1300.           NsIec104Struct.SendBuffer[Index++]=103;
  1301.           NsIec104Struct.SendBuffer[Index++]=1;
  1302.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1303.                NsIec104Struct.SendBuffer[Index++]=0x07;
  1304.              
  1305.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1306.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1307.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1308.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1309.           for(i=0;i<7;i++)
  1310.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.TimeSave[i];
  1311.           NsIec104Struct.SendBuffer[1]=Index-2;
  1312.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1313.           {
  1314.                perror ("Client Write Error");
  1315.           }
  1316. }
  1317. static void     NsIec104Interrogation(void)
  1318. {
  1319.      switch(NsIec104Struct.AsduQualifier)
  1320.      {
  1321.          case 20:   /*  All Interrogation */
  1322.                   NsIec104InterrogationAll();
  1323.                   break;
  1324.         
  1325.          default:
  1326.                   NsIec104InterrogationGroup();
  1327.                   break;
  1328.      }
  1329. }
  1330. static void     NsIec104InterrogationGroup(void)
  1331. {
  1332.  unsigned  char  WhichGroup;
  1333.   if(NsIec104Struct.AsduQualifier>=NS_IEC104_GROUP_BEGIN&&NsIec104Struct.AsduQualifier<=NS_IEC104_GROUP_END)
  1334.   {
  1335.      WhichGroup=NsIec104Struct.ReceiveBuffer[NS_IEC_104_GROUP_BYTE];
  1336.      WhichGroup-=21;
  1337.      switch(WhichGroup)
  1338.      {
  1339.          case 0:
  1340.          case 1:
  1341.          case 2:
  1342.          case 3:
  1343.          case 4:
  1344.          case 5:
  1345.          case 6:
  1346.          case 7:
  1347.                  NsIec104ProcessYxGroup(WhichGroup);
  1348.                  break;
  1349.          case 8:
  1350.          case 9:
  1351.          case 10:
  1352.          case 11:
  1353.                  NsIec104ProcessYcGroup(WhichGroup);
  1354.                  break;
  1355.          case 12:             /*  location information  */
  1356.                  NsIec104DefaultGroup(WhichGroup);
  1357.                  break;
  1358.          case 13:             /*  BCD Information  */
  1359.                  NsIec104DefaultGroup(WhichGroup);
  1360.                  break;
  1361.          case 14:             /*  cell state */
  1362.                  NsIec104DefaultGroup(WhichGroup);
  1363.                  break;
  1364.          case 15:             /*  backup use */
  1365.                  NsIec104DefaultGroup(WhichGroup);
  1366.                  break;
  1367.          default:
  1368.                  NsIec104DefaultGroup(WhichGroup);
  1369.                  break;
  1370.      }
  1371.   }
  1372. }
  1373. static void    NsIec104DefaultGroup(unsigned char Group)
  1374. {
  1375. unsigned short   General,Index;
  1376. unsigned char    *P;
  1377. unsigned short    i,j,RealNumber;
  1378.           /*  First Send  Confirm */
  1379.           Index=0;
  1380.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1381.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  1382.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1383.           P=(unsigned char *)&RealNumber;
  1384.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1385.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1386.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1387.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1388.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1389.           NsIec104Struct.SendBuffer[Index++]=100;
  1390.           NsIec104Struct.SendBuffer[Index++]=1;
  1391.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1392.                NsIec104Struct.SendBuffer[Index++]=0x07;
  1393.              
  1394.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1395.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1396.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1397.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1398.           NsIec104Struct.SendBuffer[Index++]=21+Group;
  1399.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1400.           {
  1401.                perror ("Client Write Error");
  1402.           }
  1403.           taskDelay(6);
  1404.           Index=0;
  1405.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1406.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  1407.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1408.           P=(unsigned char *)&RealNumber;
  1409.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1410.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1411.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1412.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1413.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1414.           NsIec104Struct.SendBuffer[Index++]=100;
  1415.           NsIec104Struct.SendBuffer[Index++]=1;
  1416.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1417.                NsIec104Struct.SendBuffer[Index++]=10;
  1418.              
  1419.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1420.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1421.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1422.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1423.           NsIec104Struct.SendBuffer[Index++]=21+Group;
  1424.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0)
  1425.           {
  1426.                perror ("Client Write Error");
  1427.           }
  1428. }
  1429. static void    NsIec104ProcessYxGroup(unsigned char Group)
  1430. {
  1431. unsigned short     General,Index,YxLibIndex;
  1432. unsigned char      *P,start,YxBit,YxByte;
  1433. unsigned short     i,j,g,RealNumber;
  1434.           /*  Once Gonce need send 64 Yx  8 bytes   Group from 0 begin*/
  1435.           /*  First Send  Confirm */
  1436.           Index=0;
  1437.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1438.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  1439.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1440.           P=(unsigned char *)&RealNumber;
  1441.           NsIec104Struct.SendBuffer[Index++]=P[0];                                    /*     send number       */
  1442.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1443.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1444.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];     /*     receive number    */
  1445.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1446.           NsIec104Struct.SendBuffer[Index++]=100;                                    /*      transmit  type    */
  1447.           NsIec104Struct.SendBuffer[Index++]=1;                                      /*      vsq               */ 
  1448.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)                                      /*      reason            */
  1449.                NsIec104Struct.SendBuffer[Index++]=0x07;
  1450.              
  1451.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1452.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1453.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1454.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1455.           NsIec104Struct.SendBuffer[Index++]=21+Group;
  1456.           /* 
  1457.              printf("n YxGroup=%d",Group);
  1458.            */
  1459.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1460.           {
  1461.           perror ("Client Write Error");
  1462.           }
  1463.           for(start=0;start<2;start++)
  1464.           {
  1465.             taskDelay(60);          
  1466.             Index=0;
  1467.             NsIec104Struct.SendBuffer[Index++]=0x68;
  1468.             NsIec104Struct.SendBuffer[Index++]=0x00;
  1469.             RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1470.             P=(unsigned char *)&RealNumber;
  1471.             NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1472.             NsIec104Struct.SendBuffer[Index++]=P[1];
  1473.             NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1474.             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1475.             NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1476.             NsIec104Struct.SendBuffer[Index++]=1;                 /*    asdu type   */
  1477.             NsIec104Struct.SendBuffer[Index++]=64|0x80;           /*    information object d",count  */
  1478.             /*  REASONBYTE  now is two */
  1479.             NsIec104Struct.SendBuffer[Index++]=21+Group;          /*    reason  */
  1480.             NsIec104Struct.SendBuffer[Index++]=0x00;              /*    reason  */
  1481.              
  1482.             for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1483.                NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1484.             /* Information Object Address now three byte*/
  1485.             General=0x01+start*0x40+Group*0x80;
  1486.             P=(unsigned char    *)&General;
  1487.             NsIec104Struct.SendBuffer[Index++]=P[0];
  1488.             NsIec104Struct.SendBuffer[Index++]=P[1];
  1489.             NsIec104Struct.SendBuffer[Index++]=0;
  1490.             YxLibIndex=start*8+Group*16;
  1491.             switch(NsIec104Struct.Table)
  1492.             {
  1493.               case 0:
  1494.                    NsGetDd1Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8);
  1495.                    break;
  1496.               case 1:
  1497.                    NsGetDd2Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8);
  1498.                    break;
  1499.               case 2:
  1500.                    NsGetDd3Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8);
  1501.                    break;
  1502.               case 3:
  1503.                    NsGetDd4Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,8);
  1504.                    break;
  1505.             }
  1506.             for(i=0;i<8;i++)  /* totla 64 yx*/
  1507.             {
  1508.              
  1509.                   YxByte=NsIec104Struct.DataBuff[i]; 
  1510.                   for(g=0;g<8;g++)
  1511.                   {
  1512.                       YxBit=(YxByte>>g)&0x01;
  1513.                       NsIec104Struct.SendBuffer[Index++]=YxBit;
  1514.                   }
  1515.             }
  1516.             /* Repair Length */
  1517.             NsIec104Struct.SendBuffer[1]=Index-2;
  1518.             if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1519.             {
  1520.                perror ("Client FirstWrite Error");
  1521.             }
  1522.          }
  1523.          taskDelay(6);
  1524.          Index=0;
  1525.          NsIec104Struct.SendBuffer[Index++]=0x68;
  1526.          NsIec104Struct.SendBuffer[Index++]=0x0e;
  1527.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1528.           P=(unsigned char *)&RealNumber;
  1529.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1530.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1531.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1532.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1533.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1534.           NsIec104Struct.SendBuffer[Index++]=100;
  1535.           NsIec104Struct.SendBuffer[Index++]=1;
  1536.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1537.                NsIec104Struct.SendBuffer[Index++]=10;
  1538.              
  1539.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1540.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1541.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1542.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1543.           NsIec104Struct.SendBuffer[Index++]=21+Group;
  1544.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0)
  1545.           {
  1546.                perror ("Client Write Error");
  1547.           }
  1548. }
  1549. static void    NsIec104ProcessYcGroup(unsigned char Group)
  1550. {
  1551. unsigned short   General,Index;
  1552. unsigned char    *P;
  1553. unsigned short   i,RealNumber,YcLibIndex;
  1554.           /*  Once Gonce need send 60 Yc  Group Number is 8 9 10 11 begin from 8 */
  1555.           /*  First Send  Confirm */
  1556.           Index=0;
  1557.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1558.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  1559.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1560.           P=(unsigned char *)&RealNumber;
  1561.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1562.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1563.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1564.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1565.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1566.           NsIec104Struct.SendBuffer[Index++]=100;
  1567.           NsIec104Struct.SendBuffer[Index++]=1;
  1568.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1569.                NsIec104Struct.SendBuffer[Index++]=0x07;
  1570.              
  1571.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1572.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1573.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1574.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1575.           NsIec104Struct.SendBuffer[Index++]=21+Group;
  1576.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1577.           {
  1578.                perror ("Client Write Error");
  1579.           }
  1580.           taskDelay(60);          
  1581.           Index=0;
  1582.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1583.           NsIec104Struct.SendBuffer[Index++]=0x00;
  1584.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1585.           P=(unsigned char *)&RealNumber;
  1586.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1587.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1588.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1589.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1590.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1591.           NsIec104Struct.SendBuffer[Index++]=9;                 /*    asdu type   */
  1592.           NsIec104Struct.SendBuffer[Index++]=64|0x80;           /*    information object count  */
  1593.            /*  REASONBYTE  now is two */
  1594.           NsIec104Struct.SendBuffer[Index++]=21+Group;          /*    reason  */
  1595.           NsIec104Struct.SendBuffer[Index++]=0x0;               /*    reason  */
  1596.              
  1597.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1598.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1599.           /* Information Object Address now three byte*/
  1600.           General=0x701+Group*0x80;
  1601.           P=(unsigned char    *)&General;
  1602.           NsIec104Struct.SendBuffer[Index++]=P[0];
  1603.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1604.           NsIec104Struct.SendBuffer[Index++]=0;
  1605.           YcLibIndex=0;
  1606.           switch(NsIec104Struct.Table)
  1607.           {
  1608.                  case 0:
  1609.                       NsGetDd1Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64);
  1610.                       break;
  1611.                  case 1:
  1612.                       NsGetDd2Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64);
  1613.                       break;
  1614.                  case 2:
  1615.                       NsGetDd3Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64);
  1616.                       break;
  1617.                  case 3:
  1618.                       NsGetDd4Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,64);
  1619.                       break;
  1620.           }
  1621.           for(i=0;i<64;i++)
  1622.           {
  1623.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i];
  1624.               NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.DataBuff[2*i+1];
  1625.               NsIec104Struct.SendBuffer[Index++]=0x00;  /*qds*/
  1626.           }
  1627.           /* Repair Length */
  1628.           NsIec104Struct.SendBuffer[1]=Index-2;
  1629.           if((NsIec104Struct.SendLength=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0))<0)
  1630.           {
  1631.                perror ("Client FirstWrite Error");
  1632.           }
  1633.           /*
  1634.             printf("nYcGroupFirst=%d",Group);
  1635.            */
  1636.           taskDelay(6);
  1637.           Index=0;
  1638.           NsIec104Struct.SendBuffer[Index++]=0x68;
  1639.           NsIec104Struct.SendBuffer[Index++]=0x0e;
  1640.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1641.           P=(unsigned char *)&RealNumber;
  1642.           NsIec104Struct.SendBuffer[Index++]=P[0];              /*     send number       */
  1643.           NsIec104Struct.SendBuffer[Index++]=P[1];
  1644.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1645.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1646.           NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1647.           NsIec104Struct.SendBuffer[Index++]=100;
  1648.           NsIec104Struct.SendBuffer[Index++]=1;
  1649.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1650.                NsIec104Struct.SendBuffer[Index++]=10;
  1651.              
  1652.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1653.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.CommonAsduAddress[i];
  1654.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1655.              NsIec104Struct.SendBuffer[Index++]=NsIec104Struct.InformationObject[i];
  1656.           NsIec104Struct.SendBuffer[Index++]=21+Group;
  1657.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],Index,0x0)) < 0)
  1658.           {
  1659.                perror ("Client Write Error");
  1660.           }
  1661.           /*
  1662.             printf("nEndwrite=%d",NsIec104Struct.SendLength);
  1663.            */
  1664. }
  1665. static void    NsIec104ProcessCellGroup(unsigned char Group)
  1666. {
  1667. }
  1668. static void     NsIec104InterrogationAll(void)
  1669. {
  1670. unsigned short    i,Index,start,g;
  1671.          long     ShiftValue;
  1672. unsigned char     *PGive,j,GroupIndex,YxByte,YxBit,*pYc;
  1673. unsigned short    RealNumber,YxLibIndex,YcLibIndex;
  1674. unsigned long     InformationObject;
  1675. unsigned short   sYc;
  1676. float   fYc;
  1677.           /*  First Send  Confirm */
  1678.           GroupIndex=0;
  1679.           NsIec104Struct.NeedSendNumber=0;
  1680.           NsIec104Struct.HasSendNumber=0;
  1681.           NsIec104Struct.LeftSendNumber=0;
  1682.           Index=0;
  1683.           NsIec104Struct.GroupSendBuffer[0][Index++]=0x68;
  1684.           NsIec104Struct.GroupSendBuffer[0][Index++]=0x0e;
  1685.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1686.           PGive=(unsigned char *)&RealNumber;
  1687.           NsIec104Struct.GroupSendBuffer[0][Index++]=PGive[0];              /*     send number       */
  1688.           NsIec104Struct.GroupSendBuffer[0][Index++]=PGive[1];
  1689.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1690.           NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1691.           NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1692.           NsIec104Struct.GroupSendBuffer[0][Index++]=100;
  1693.           NsIec104Struct.GroupSendBuffer[0][Index++]=1;
  1694.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1695.                NsIec104Struct.GroupSendBuffer[0][Index++]=0x07;
  1696.              
  1697.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1698.              NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1699.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1700.              NsIec104Struct.GroupSendBuffer[0][Index++]=NsIec104Struct.InformationObject[i];
  1701.           NsIec104Struct.GroupSendBuffer[0][Index++]=20;
  1702.           NsIec104Struct.GroupSendLength[0]=Index;
  1703. /*
  1704.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[0][0],NsIec104Struct.GroupSendLength[0],0x0))<0)
  1705.           {
  1706.                perror ("Client Write Error");
  1707.           }
  1708. */
  1709.           /*  Second Send  Yx   total send 1024 yx  */
  1710.           GroupIndex++;
  1711.           NsIec104Struct.NeedSendNumber++;
  1712.           for(start=0;start<NS_IEC_104_TOTAL_YX_TIMES;start++)
  1713.           {
  1714.               Index=0;
  1715.     
  1716.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68;
  1717.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e;
  1718.               RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1719.               PGive=(unsigned char *)&RealNumber;
  1720.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[0];              /*     send number       */
  1721.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[1];
  1722.               NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1723.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1724.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1725.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1;      /*    asdu type   */
  1726.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NS_IEC_104_ONCE_YX|0x80;     /*    information object count  */
  1727.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=20;    /*  reason  */
  1728.               NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0;    /*  reason  */
  1729.              
  1730.               for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1731.                   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1732.               InformationObject=0x01+start*NS_IEC_104_ONCE_YX;
  1733.               PGive=(unsigned char *)&InformationObject;
  1734.               for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1735.               {
  1736.                   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[i];
  1737.               }
  1738.               YxLibIndex=start*NS_IEC_104_ONCE_YX_BYTES;
  1739.               switch(NsIec104Struct.Table)
  1740.               {
  1741.                 case 0:
  1742.                        NsGetDd1Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES);
  1743.                        break;
  1744.                 case 1:
  1745.                        NsGetDd2Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES);
  1746.                        break;
  1747.                 case 2:
  1748.                        NsGetDd3Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES);
  1749.                        break;
  1750.                 case 3:
  1751.                        NsGetDd4Lib(NS_YX_DATA,YxLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YX_BYTES);
  1752.                        break;
  1753.               }
  1754.               for(j=0;j<NS_IEC_104_ONCE_YX_BYTES;j++)
  1755.               {
  1756.                
  1757.                   YxByte=NsIec104Struct.DataBuff[j]; 
  1758.                   for(g=0;g<8;g++)
  1759.                   {
  1760.                       YxBit=(YxByte>>g)&0x01;
  1761.                       NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=YxBit;
  1762.                   }
  1763.               }
  1764.                                /*
  1765.                                   Debug Yx Message 
  1766.                                   for(j=0;j<NS_IEC_104_ONCE_YX;j++)
  1767.                                   {
  1768.                                     NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x30;
  1769.                                   }
  1770.                                */
  1771.            
  1772.               /* Repair Length */
  1773.               NsIec104Struct.GroupSendBuffer[GroupIndex][1]=Index-2;
  1774.               NsIec104Struct.GroupSendLength[GroupIndex]=Index;
  1775. /*
  1776.               if((NsIec104Struct.GroupSendLength[GroupIndex] = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength[GroupIndex],0x0)) < 0)
  1777.               {
  1778.                perror ("Client Write Error");
  1779.               }
  1780. */
  1781.               NsIec104Struct.NeedSendNumber++;
  1782.               GroupIndex++;
  1783.           }
  1784.           
  1785.           /*  Third  Send  Yc    */
  1786.           for(start=0;start<NS_IEC_104_TOTAL_YC_TIMES;start++)
  1787.           {
  1788.              Index=0;
  1789.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68;
  1790.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x00;
  1791.              RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1792.              PGive=(unsigned char *)&RealNumber;
  1793.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[0];              /*     send number       */
  1794.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[1];
  1795.              NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1796.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1797.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1798.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=9;                     /* 11->21   sigle yc asdu type   */
  1799.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NS_IEC_104_ONCE_YC|0x80;               /*    information object count  */
  1800.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=20;                    /*    reason  */
  1801.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0;                   /*    reason  */
  1802.              
  1803.              for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1804.                  NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1805.               InformationObject=0x701+start*NS_IEC_104_ONCE_YC;
  1806.               PGive=(unsigned char *)&InformationObject;
  1807.               for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1808.               {
  1809.                   NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[i];
  1810.               }
  1811.               YcLibIndex=start*NS_IEC_104_ONCE_YC;
  1812.               switch(NsIec104Struct.Table)
  1813.               {
  1814.                  case 0:
  1815.                       NsGetDd1Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC);
  1816.                       break;
  1817.                  case 1:
  1818.                       NsGetDd2Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC);
  1819.                       break;
  1820.                  case 2:
  1821.                       NsGetDd3Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC);
  1822.                       break;
  1823.                  case 3:
  1824.                       NsGetDd4Lib(NS_YC_DATA,YcLibIndex,NsIec104Struct.DataBuff,NS_IEC_104_ONCE_YC);
  1825.                       break;
  1826.               }
  1827.               for(j=0;j<NS_IEC_104_ONCE_YC;j++)
  1828.               {
  1829.                  
  1830. /*sYc = *((unsigned short *)&NsIec104Struct.DataBuff[j*2]);
  1831. fYc = sYc * 1.0;
  1832. pYc = (unsigned char *)&fYc;
  1833. NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[0];
  1834.                  NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[1];
  1835. NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[2];
  1836. NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=pYc[3];*/
  1837. NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[2*j];
  1838. NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.DataBuff[2*j + 1];
  1839.                  NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0;                 /*  qds    */
  1840.               }
  1841.               /*
  1842.                   printf("n Yc Send ");
  1843.               */
  1844.               /* Repair Length */
  1845.               NsIec104Struct.GroupSendBuffer[GroupIndex][1]=Index-2;
  1846.               NsIec104Struct.GroupSendLength[GroupIndex]=Index;
  1847. /*
  1848.               if((NsIec104Struct.GroupSendLength[GroupIndex]=send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength[GroupIndex],0x0))<0)
  1849.               {
  1850.                perror ("Client Write Error");
  1851.               }
  1852. */
  1853.               NsIec104Struct.NeedSendNumber++;
  1854.               GroupIndex++;
  1855.           }
  1856.           /*  Fiveth Send  Over  */
  1857.           Index=0;
  1858.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x68;
  1859.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=0x0e;
  1860.           RealNumber=(NsIec104Struct.SendMeNumber<<1);
  1861.           PGive=(unsigned char *)&RealNumber;
  1862.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[0];              /*     send number       */
  1863.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=PGive[1];
  1864.           NsIec104Struct.SendMeNumber=(NsIec104Struct.SendMeNumber+1)%65535;
  1865.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[0];              /*     receive number    */
  1866.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.ReceiveHimNumber[1];
  1867.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=100;
  1868.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=1;
  1869.           for(i=0;i< NS_IEC_104_REASONBYTE;i++)
  1870.                NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=10;
  1871.              
  1872.           for(i=0;i<NS_IEC_104_ASDUADDRESSBYTE;i++)
  1873.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.CommonAsduAddress[i];
  1874.           for(i=0;i<NS_IEC_104_INFORMATIONBYTE;i++)
  1875.              NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=NsIec104Struct.InformationObject[i];
  1876.           NsIec104Struct.GroupSendBuffer[GroupIndex][Index++]=20;
  1877.           NsIec104Struct.GroupSendLength[GroupIndex]=Index;
  1878. /*
  1879.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.GroupSendBuffer[GroupIndex][0],NsIec104Struct.GroupSendLength[GroupIndex],0x0)) < 0)
  1880.           {
  1881.                perror ("Client Write Error");
  1882.           }
  1883. */
  1884.           NsIec104Struct.NeedSendNumber++;
  1885.           GroupIndex++;
  1886. }
  1887. static  void  NsIec104ProcessFormatS(void)
  1888. {
  1889. int Index;
  1890. /*
  1891.        printf("nFormatS");
  1892.        Index=0;
  1893.        NsIec104Struct.SendBuffer[0]=0x68;
  1894.        NsIec104Struct.SendBuffer[1]=0x04;
  1895.        NsIec104Struct.SendBuffer[2]=0x0b;
  1896.        NsIec104Struct.SendBuffer[3]=0x00;
  1897.        NsIec104Struct.SendBuffer[4]=0x00;
  1898.        NsIec104Struct.SendBuffer[5]=0x00;
  1899.        NsIec104Struct.ReceiveHimNumber[0]=NsIec104Struct.ReceiveBuffer[2];
  1900.        NsIec104Struct.ReceiveHimNumber[1]=NsIec104Struct.ReceiveBuffer[3];
  1901.       if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],6,0x0)) < 0)
  1902.       {
  1903.                perror ("Client Write Error");
  1904.       }
  1905.       printf("nwrite=%d",NsIec104Struct.SendLength);
  1906.   */
  1907. }
  1908. static  void  NsIec104ProcessFormatU(void)
  1909. {
  1910. int Index;
  1911. int i;
  1912.        NsIec104Struct.UCommand=NsIec104Struct.ReceiveBuffer[2];
  1913.        Index=0;
  1914.        if(NsIec104Struct.UCommand&NS_IEC_104_STARTDT)
  1915.        {
  1916.           Index=6;
  1917.           NsIec104Struct.SendBuffer[0]=0x68;
  1918.           NsIec104Struct.SendBuffer[1]=0x04;
  1919.           NsIec104Struct.SendBuffer[2]=0x0b;
  1920.           NsIec104Struct.SendBuffer[3]=0x00;
  1921.           NsIec104Struct.SendBuffer[4]=0x00;
  1922.           NsIec104Struct.SendBuffer[5]=0x00;
  1923.           /*
  1924.           printf("nFormatU STARTDT");
  1925.           */
  1926.           NsIec104Struct.SendMeNumber=0x00;
  1927.           NsIec104Struct.ReceiveHimNumber[0]=0x00;              
  1928.           NsIec104Struct.ReceiveHimNumber[1]=0x00;
  1929.           /*NsIec104Struct.NeedSendNumber=0;
  1930.           NsIec104Struct.HasSendNumber=0;
  1931.           NsIec104Struct.LeftSendNumber=0;*/
  1932.        }
  1933.        else 
  1934.        {
  1935.           if(NsIec104Struct.UCommand&NS_IEC_104_STOPDT)
  1936.           {
  1937.                Index=6;
  1938.                NsIec104Struct.SendBuffer[0]=0x68;
  1939.                NsIec104Struct.SendBuffer[1]=0x04;
  1940.                NsIec104Struct.SendBuffer[2]=0x23;
  1941.                NsIec104Struct.SendBuffer[3]=0x00;
  1942.                NsIec104Struct.SendBuffer[4]=0x00;
  1943.                NsIec104Struct.SendBuffer[5]=0x00;
  1944.                NsIec104Struct.SendMeNumber=0x00;
  1945.                NsIec104Struct.ReceiveHimNumber[0]=0x00;              
  1946.                NsIec104Struct.ReceiveHimNumber[1]=0x00;
  1947.                NsIec104Struct.NeedSendNumber=0;
  1948.                NsIec104Struct.HasSendNumber=0;
  1949.                NsIec104Struct.LeftSendNumber=0;
  1950.           }
  1951.           else 
  1952.           {
  1953.                  if(NsIec104Struct.UCommand&NS_IEC_104_TESTFR)
  1954.                  {
  1955.                     Index=6;
  1956.                     NsIec104Struct.SendBuffer[0]=0x68;
  1957.                     NsIec104Struct.SendBuffer[1]=0x04;
  1958.                     NsIec104Struct.SendBuffer[2]=0x83;
  1959.                     NsIec104Struct.SendBuffer[3]=0x00;
  1960.                     NsIec104Struct.SendBuffer[4]=0x00;
  1961.                     NsIec104Struct.SendBuffer[5]=0x00;
  1962.                  }
  1963.           }
  1964.        }
  1965.        if(Index>0)
  1966.        {
  1967.           if((NsIec104Struct.SendLength = send(NsIec104Struct.NsNewSocketId,&NsIec104Struct.SendBuffer[0],6,0x0)) < 0)
  1968.           {
  1969.                perror ("Client Write Error");
  1970.           }
  1971.           /*
  1972.           printf("ns:");
  1973.           for (i = 0; i < 6; i++)
  1974.            printf("%4x",NsIec104Struct.SendBuffer[i]);
  1975.           printf("n");
  1976.           */
  1977.        }
  1978. }
  1979. static  void  NsIec104Reset(void)
  1980. {
  1981. NsIec104Struct.ReceiveLength=0;
  1982. NsIec104Struct.ReceiveIndexLength=0;
  1983. NsIec104Struct.ReceiveIndex=0;
  1984. NsIec104Struct.HasSendNumber=0;
  1985. }
  1986. static  void       NsIec104CycleCount(void)
  1987. {
  1988. unsigned short i;
  1989. unsigned long  Nap,NowSecond;
  1990.   getforsecond(&NowSecond);
  1991.   for(i=0;i<NS_IEC104_USE_CYCLE;i++) 
  1992.   {
  1993. Nap = DiffMsLong(NowSecond,NsIec104Cycle[i].LastTime);
  1994. if(Nap >= NsIec104Cycle[i].TimeNap) 
  1995.         {
  1996. NsIec104Cycle[i].TimeSign= ON;
  1997. getforsecond(&NsIec104Cycle[i].LastTime);
  1998. }
  1999.   }
  2000. }
  2001. /*
  2002. void debug104(void)
  2003. {
  2004. unsigned char Buff[256];
  2005. short i;
  2006.    for(i=0;i<200;i++)
  2007.       Buff[i]=0;
  2008.    for(i=0;i<96;i+=2)
  2009.      Buff[i]=i+100;
  2010.    NsPutDd1Lib(NS_YC_DATA,0,0,&Buff[0],48);
  2011.       for(i=0;i<12;i++)
  2012.          Buff[i]=0x0f;    
  2013.       NsPutDd1Lib(NS_YX_DATA,0,0,&Buff[0],12);
  2014. }
  2015. static void Iec104testCosSoe(void) {
  2016. struct tm NowTime;
  2017. static i = 0;
  2018. static unsigned char st = 0;
  2019. NS_COS_FORMAT CosValue;
  2020. NS_SOE_FORMAT SoeValue;
  2021. getvxTime(&NowTime);
  2022. for(i=0;i<10;i++){
  2023. SoeValue.SendNumber = i;
  2024. SoeValue.KgNumber = i;
  2025. SoeValue.CellNumber = 0;
  2026. SoeValue.RealAddress = 1;
  2027. SoeValue.Type = 2;
  2028. SoeValue.State = st;
  2029. SoeValue.Year = 2003;
  2030. SoeValue.Month = 7;
  2031. SoeValue.Day = 22;
  2032. SoeValue.Hour = 15;
  2033. SoeValue.Minute = 00;
  2034. SoeValue.Second = i;
  2035. SoeValue.Msecond = 888;
  2036. NsPutNetSoeLib(&SoeValue);
  2037. NsPutDd1SoeLib(&SoeValue);
  2038. NsPutDd2SoeLib(&SoeValue);
  2039. NsPutDd3SoeLib(&SoeValue);
  2040. NsPutAllSoeLib(&SoeValue);
  2041. CosValue.SendNumber =i;
  2042. CosValue.KgNumber = i;
  2043. CosValue.CellNumber = 0;
  2044. CosValue.RealAddress = 1;
  2045. CosValue.Type = 2;
  2046. CosValue.State = st;
  2047. NsPutNetCosLib(&CosValue);
  2048. NsPutDd1CosLib(&CosValue);
  2049. NsPutDd2CosLib(&CosValue);
  2050. NsPutDd3CosLib(&CosValue);
  2051. NsPutAllCosLib(&CosValue);
  2052. }
  2053. if (st == 0) st = 1;
  2054. else  st = 0;
  2055. }
  2056. */