/*
XSdemo16: 7000 I/O mapping to modbus memory 

Compiler: BC++ 3.1, 
          Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp)

Compile mode: Large

Project: Client.c
         ..\lib\7186el.Lib
         ..\lib\tcp_dm32.lib
         ..\lib\FW_nnnnn.LIB
         ..\Lib\MBT7@170.Lib  (Ver 2008/01/30)

Module status
===========================  
NO_RESPONSE     -10
READ_TIMEOUT    -20
OVER_TRIGGER    -30
OVER_BUFFER     -50
READ_ERROR      -99
OUTPUT_SUCC       0

 Modbus DI Memory  (10001~10004) i-7044
 --------------------------------------------------
iMemory_DI      
    0         DI0 value
    1         DI1 value
    2         DI2 value
    3         DI2 value
    

 Modbus DO Memory (00001~00008) i-7044
 --------------------------------------------------
iMemory_DO 
    0         DO0 value
    1         DO1 value
    2         DO2 value
    3         DO3 value
    4         DO4 value
    5         DO5 value
    6         DO6 value
    7         DO7 value
    

  Modbus AI Memory (30001~30008) i-7017
 --------------------------------------------------
iMemory_AI
    0         AI0 value
    1         AI1 value
    2         AI2 value
    3         AI3 value
 

 ...
    79       DI  Module status
 ...
    81       AI0 Module status  
    82       AI1 Module status
    83       AI2 Module status
    84       AI3 Module status
    85       AI4 Module status  
    86       AI5 Module status
    87       AI6 Module status
    88       AI7 Module status

...
    91       AO0 Module status  
    92       AO1 Module status
    93       AO2 Module status
    94       AO3 Module status
...
    99       DO  Module status

**************************
   Float         Hex
    +10~0       7FFF~0
    0~-10       8FFF~8000
**************************  

  
  Modbus AO Memory  (40001~40004) i-7024
 --------------------------------------------------
iMemory_AO 
    0         AO0 value
    1         AO1 value
    2         AO2 value
    3         AO3 value
 
**************************
   Float         Hex
    +10~0       7FFF~0
    0~-10       8FFF~8000
************************** 

  
    = end of settings = 
=================================================
                      
    
    Hardware: uPAC-7186EX + 
              7024  (baudrate 115200 bps , Address :1)
                    Type Code 33 (+10V~-10V)
              7017  (baudrate 115200 bps , Address :2)
                    Type Code 08 (+10V~-10V) 
              7044  (baudrate 115200 bps , Address :3)
    
[Jan 24, 2008] by Jency
*/

#include <conio.h>
#include <stdio.h>
#include "..\lib\7186e.h"
#include "..\lib\Tcpip32.h"
#include "..\lib\MFW.h"
#include "..\lib\MBTCP_XS.h"

void UPort10000(int skt, int mode);
void UPort502(int skt, int mode);
void CallBackCom2(pCOMPORT com, int mode);
#define BUFSIZE 1460        /* read/write buffer size */

int iScanCOMPort=1; // of Modbus Kernel
int iReBack=0;
int iSendCmdProcess=0;
int SendCmdProcess(int iProcess);
long Tstart, T;

//============================//
//  The I/O memory mapping    //    
//============================//

unsigned char far iMemory_DI[100];
unsigned char far iMemory_DO[100];
          int far iMemory_AI[100];
          int far iMemory_AO[100];

#define DIch 4
#define DOch 8
#define AIch 8
#define AOch 4

#define NO_RESPONSE     -10
#define READ_TIMEOUT    -20
#define OVER_TRIGGER    -30
#define OVER_BUFFER     -50
#define READ_ERROR      -99
#define OUTPUT_SUCC       0

//============================//
//          TCP Server        //    
//============================//
TCP_SERVER TcpServer={
    10000, /* port */
    -1, /* socket */
    0,  /* state */
    0,  /* connect */
    UPort10000/* CallBackFun */
};

//============================//
//      MBTCP Server          //    
//============================//
TCP_SERVER MBTcp={
    502, /* port */
    -1, /* socket */
    0,  /* state */
    0,  /* connect */
    UPort502/* CallBackFun */
};

//============================//
//          COM port          //    
//============================//
#define BUF2_SIZE 70
char Buf2[BUF2_SIZE]; 
COMPORT MyCom2={
    2,            //int Port; // 0:COM0, 1:COM1,...
    Buf2,         //char *Buf;
    BUF2_SIZE,    //unsigned Bufsize;
    '\r',         //char EndChar; 
    40,           //unsigned TriggerLevel;
    20,           //long Timeout;
    0,            //long StartTimeTicks;
    0,            //long LastTimeTicks;
    100,          //long MasterTimeout;
    0,            //long MasterStartTimeTicks; 
    0,            //unsigned Size; 
    ReadComn_2,   //int (*ReadComn)(unsigned char *buf,int n);         //defined in 7188E.H
    CallBackCom2,     //void (*CallbackFun)(pCOMPORT, int mode);
    0,            //pCOMPORT next;
};

//============================//
//          Ask AI            //    
//============================//
int AskAI(int AIchcount)
{
    int iError;
    char AIvalue[10]={0};

    iSendCmdProcess = iSendCmdProcess++;
    iReBack = 1;
    sprintf(AIvalue,"#02%d",AIchcount);
    iError=SendCmdTo7000(2,AIvalue,0);
    MyCom2.MasterStartTimeTicks=GetTimeTicks();
    return iError;
}

//============================//
//          Read AI           //    
//============================//
void ReadAI(char Buf[])
{
    unsigned long ReBack_value;
    sscanf(Buf+1,"%04X",&ReBack_value);
    iMemory_AI[iSendCmdProcess-3]= ReBack_value & 0xFFFF;
}

//============================//
//          Write AO          //    
//============================//
int WriteAO(int AOchcount)
{
    int iError;
    char AOvalue[24]={0};
    float outvalueAO=0;

    iSendCmdProcess = iSendCmdProcess++;
    iReBack = 1;
    outvalueAO=iMemory_AO[AOchcount]/32767.0*10;
    sprintf(AOvalue,"#01%d%+06.3f",AOchcount,outvalueAO);               
    iError=SendCmdTo7000(2,AOvalue,0);
    MyCom2.MasterStartTimeTicks=GetTimeTicks();
    
    return iError;
}

char *GetModuleName(void)   /* User's program must support this function for UDP search */ 
{
    return "i-7188E-XS" ;
}

char *GetAliasName(void)    /* User's program must support this function for UDP search */ 
{
    return "test1" ;
}

//============================//
//        Init & End          //    
//============================//

void XS_UserInit(int argc,char *argv[])
{
    extern int bAcceptBroadcast;
    extern unsigned long ACKDELAY;  /* vaariable in tcp.c */
    extern long MAXTXTOUT;
    unsigned char MyIp[4];
    void UserLoop(void);
    char version[20];
    int iRet;

    InitLib();
    InstallCom_1(115200,8,0,1);
    InstallCom_2(115200,8,0,1);

/*initial modbus*/  
    iRet=InitModbus(iMemory_DI,iMemory_DO,iMemory_AI,iMemory_AO);
    printCom1("iRet=%d\n",iRet);
    if(iRet==0)
        printCom1("initial modbus success!");
    else
        printCom1("There is an error code %d when initial modbus",iRet);


    Set_NetID(1);
    
    bAcceptBroadcast=0;
    ACKDELAY=200;
    XS_GetVersion(version);
    printCom1("[X-Server library]: version=%s ",version);
    XS_GetLibDate(version);
    printCom1("date=%s\r\n",version);
    GetTcpipLibDate(version);
    printCom1("Tcpip library version:%X, Library Date is %s\r\n",GetTcpipLibVer(),version);
    GetIp(MyIp);
    printCom1("IP=%d.%d.%d.%d\r\n", MyIp[0], MyIp[1], MyIp[2], MyIp[3]);

    XS_AddComPort(&MyCom2);
    
    XS_AddSystemLoopFun(UserLoop);

    XS_AddServer(&TcpServer);
    XS_AddServer(&MBTcp);
    

    /* Add UDP search function */ 
    XS_AddUdpSocket(&XS_UdpSearch);
    bAcceptBroadcast=1;     // allow to receive the UDP broadcast

    EnableWDT();
    MAXTXTOUT=6000UL; 

    XS_AddSystemLoopFun(XS_SocketLoopFun);
    XS_StartSocket();
    
    //initial all channle of DO AO to 0x00 and +00.000V
    SendCmdTo7000(2,"@0300",0);
    SendCmdTo7000(2,"#010+00.000",0);
    SendCmdTo7000(2,"#011+00.000",0);
    SendCmdTo7000(2,"#012+00.000",0);
    SendCmdTo7000(2,"#013+00.000",0);
}

void XS_UserEnd(void)
{
    XS_StopSocket();
    DisableWDT();
    XS_RemoveComPort(&MyCom2);
    RestoreCom_2();
    RestoreCom_1();
}

//============================//
//          User Loop         //    
//============================//

unsigned status_8019as=0;
int Socket0=-1; 
extern int errno;
long st, old_dt;

void UserLoop(void)
{   
    long tmpt;
    int i, err, key=0;

    RefreshWDT();

    if(IsCom(1))
    {
        key=ReadCom(1);
        switch(key)
        {
            case 27:
                QuitMain=1;
                break;
        }
    }
    
    errno=SendCmdProcess(iSendCmdProcess);

#if 1
    Show5DigitLedSeg(1,103);//--->'P'
    Show5DigitLedSeg(2,119);//--->'R'
    Show5DigitLedSeg(3,126);//--->'O'
    Show5DigitLedSeg(4,95);//--->'G'
    Show5DigitLedSeg(5,119);//--->'R'
#endif
}

char buf[BUFSIZE],SendBack[BUFSIZE+2];

// TCP call back fun
void UPort10000( int skt ,int mode)
{ 
    int i, cc, err, Sendlen;
    
    if(!mode)
    {
        Socket0=skt;
        Sendlen=sprintf(SendBack, "Welcome connect to port %u\r\nThere are %d clients connect to this port\r\n",
                        TcpServer.port,TcpServer.connect);
        err= XS_WriteSocket(skt,SendBack,Sendlen);
            
    }
    else 
    {
        err = readsocket( skt, buf, sizeof(buf)-1);
        if (err <= 0) 
        {              /* error or disconnected by remote side */
            XS_CloseSocket(skt);
        }//if (err <= 0) 
        else 
        {
            if(err==4 || err==5)
            {
                if(!strncmp(buf,"quit",4)) 
                {
                    QuitMain=1;
                }
            }
        }
    }
}

// MBTCP call back fun
void UPort502(int skt, int mode)
{
    int iByte;
    char sInputBuffer_P502[256];
    TCPREADDATA pTCP_Data_Pointer;
     
    if(mode==0)    //First time connected from remote clients.
    {     
        Delay(1);
        Port502Start(skt);
    }  
    else
    {
        Delay(1);
        iByte=readsocket(skt, sInputBuffer_P502, sizeof(sInputBuffer_P502)-1);

        if(iByte>0)
        {
            pTCP_Data_Pointer.Socket=skt;
            pTCP_Data_Pointer.ReadUartChar=sInputBuffer_P502;
            pTCP_Data_Pointer.Length=iByte;
            
            VcomCmdModbus(&pTCP_Data_Pointer);
            RefreshWDT();
        }
        else
            XS_CloseSocket(skt);
    }
}

// Send Comand Process
int SendCmdProcess(int iProcess)
{
    int iError,i;
    char DOvalue[10]={0};
    unsigned char outvalueDO;

    switch(iProcess)
    {
        case 0: //read DIO , --->@AA
            if(iReBack == 0)
            {   
                iSendCmdProcess = 1;
                iReBack = 1;
                iError=SendCmdTo7000(2,"@03",0);
                MyCom2.MasterStartTimeTicks=GetTimeTicks();
            }
            break;
        case 1: //write DO, -->#AA00(Data)
            if(iReBack == 0)
            {   
                iSendCmdProcess = 2;
                iReBack = 1;
                outvalueDO=0;
                for(i=0;i<DOch;i++)
                {
                    if( (iMemory_DO[i] & 0x01)!=0)
                        outvalueDO+=(1<<i);
                }
                sprintf(DOvalue,"@03%02X",outvalueDO);
                iError=SendCmdTo7000(2,DOvalue,0);
                MyCom2.MasterStartTimeTicks=GetTimeTicks();
            }
            break;
        case 2: // read AI, --->#AA0
            
            if(iReBack == 0)
                iError=AskAI(0);
            break;

        case 3: // read AI, --->#AA1
            
            if(iReBack == 0)
                iError=AskAI(1);
            break;
        case 4: // read AI, --->#AA2
            
            if(iReBack == 0)
                iError=AskAI(2);
            break;
        case 5: // read AI, --->#AA3
            if(iReBack == 0)
                iError=AskAI(3);
            break;
        case 6: // read AI, --->#AA4
            
            if(iReBack == 0)
                iError=AskAI(4);
            break;
        case 7: // read AI, --->#AA5
            if(iReBack == 0)
                iError=AskAI(5);
            break;
        case 8: // read AI, --->#AA6
            if(iReBack == 0)
                iError=AskAI(6);
            break;
        case 9: // read AI, --->#AA7
            if(iReBack == 0)
                iError=AskAI(7);
            break;
        case 10: //write AO, ---->#AA0(Data) 
            if(iReBack == 0)
                iError=WriteAO(0);
            break;
        case 11: //write AO, ---->#AA1(Data) 
            if(iReBack == 0)
                iError=WriteAO(1);
            break;
        case 12: //write AO, ---->#AA2(Data) 
            if(iReBack == 0)
                iError=WriteAO(2);
            break;
        case 13: //write AO, ---->#AA3(Data) 
            if(iReBack == 0)
                iError=WriteAO(3);
            break;
        default:
            if(iReBack == 0)
            {
                iSendCmdProcess=0;
                iError=0;
            }
            break;
    }
    return iError;
}

// Callback function
void CallBackCom2(pCOMPORT com, int mode)
{ 
    int i,key;
    unsigned long ReBack_value;
   
    switch(mode)
    {
        case 0: // 超過 Timeout 時間沒再收到字元 
//          printCom1("[timeout!!]\r");
            switch( iSendCmdProcess)
            {
                case 0:                 //initial 
                    break;
                case 1:                 //4DI for i-7044
                    iMemory_AI[79]=READ_TIMEOUT;
                    break;
                case 2:                 //8DO for i-7044
                        iMemory_AI[99]=READ_TIMEOUT;    
                        break;
                case 3:                 //8AI for i-7017
                    iMemory_AI[81]=READ_TIMEOUT;
                    break;
                case 4:                 //8AI for i-7017
                    iMemory_AI[82]=READ_TIMEOUT;
                    break;
                    case 5:                 //8AI for i-7017
                    iMemory_AI[83]=READ_TIMEOUT;
                    break;
                case 6:                 //8AI for i-7017
                    iMemory_AI[84]=READ_TIMEOUT;
                    break;
                case 7:                 //8AI for i-7017
                    iMemory_AI[85]=READ_TIMEOUT;
                    break;
                case 8:                 //8AI for i-7017
                    iMemory_AI[86]=READ_TIMEOUT;
                    break;
                case 9:                 //8AI for i-7017
                    iMemory_AI[87]=READ_TIMEOUT;
                    break;
                case 10:                    //8AI for i-7017
                    iMemory_AI[88]=READ_TIMEOUT;
                    break;
                case 11:                    //4AO for i-7024
                    iMemory_AI[91]=READ_TIMEOUT;
                    break;
                case 12:                    //4AO for i-7024
                    iMemory_AI[92]=READ_TIMEOUT;
                    break;
                case 13:                    //4AO for i-7024
                    iMemory_AI[93]=READ_TIMEOUT;
                    break;
                case 14:                    //4AO for i-7024
                    iMemory_AI[94]=READ_TIMEOUT;
                    break;
                default:
                    break;
            
            }//switch(  iSendCmdProcess)*/
            
            break; 

        case 1: // 收到 EndChar 
    
            switch( iSendCmdProcess)
            {
                case 0:                 //initial 
                    break;
                case 1:                 //4DI for i-7044
                    if(com->Buf[0]=='>')
                    {
                        sscanf(com->Buf+1,"%04X",&ReBack_value);
                        
                        for(i=0;i<DIch;i++)
                        {
                            if( (ReBack_value & 0x01)!=0)
                                iMemory_DI[i]= 1;
                            else
                                iMemory_DI[i]= 0;
                            ReBack_value >>= 1;
                        }
                    }
                    iMemory_AI[79]=OUTPUT_SUCC;
                    break;
                case 2:                 //8DO for i-7044
                    if(com->Buf[0]=='>')
                        iMemory_AI[99]=OUTPUT_SUCC; 
                        break;
                case 3:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[81]=OUTPUT_SUCC;
                    break;
                case 4:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[82]=OUTPUT_SUCC;
                    break;
                case 5:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[83]=OUTPUT_SUCC;
                    break;
                case 6:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[84]=OUTPUT_SUCC;
                    break;
                case 7:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[85]=OUTPUT_SUCC;
                    break;
                case 8:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[86]=OUTPUT_SUCC;
                    break;
                case 9:                 //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[87]=OUTPUT_SUCC;
                    break;
                case 10:                    //8AI for i-7017
                    if(com->Buf[0]=='>')
                        ReadAI(com->Buf);
                    iMemory_AI[88]=OUTPUT_SUCC;
                    break;
                case 11:                    //4AO for i-7024
                    if(com->Buf[0]=='>')
                        iMemory_AI[91]=OUTPUT_SUCC;
                    break;
                case 12:                    //4AO for i-7024
                    if(com->Buf[0]=='>')
                        iMemory_AI[92]=OUTPUT_SUCC;
                    break;
                case 13:                    //4AO for i-7024
                    if(com->Buf[0]=='>')
                        iMemory_AI[93]=OUTPUT_SUCC;
                    break;
                case 14:                    //4AO for i-7024
                    if(com->Buf[0]=='>')
                        iMemory_AI[94]=OUTPUT_SUCC;
                    break;
                default:
                    break;
            
            }//switch(  iSendCmdProcess)*/

            break; 
        case 2: // 收到字元個數到達 TriggerLevel 
//          printCom1("[up to trigger level]\r\n");
            switch( iSendCmdProcess)
            {
                
                case 0:                 //initial 
                    break;

                case 1:                 //4DI for i-7044
                    iMemory_AI[79]=OVER_TRIGGER;
                    break;
                case 2:                 //8DO for i-7044
                        iMemory_AI[99]=OVER_TRIGGER;    
                        break;
                case 3:                 //8AI for i-7017
                    iMemory_AI[81]=OVER_TRIGGER;
                    break;
                case 4:                 //8AI for i-7017
                    iMemory_AI[82]=OVER_TRIGGER;
                    break;
                case 5:                 //8AI for i-7017
                    iMemory_AI[83]=OVER_TRIGGER;
                    break;
                case 6:                 //8AI for i-7017
                    iMemory_AI[84]=OVER_TRIGGER;
                    break;
                case 7:                 //8AI for i-7017
                    iMemory_AI[85]=OVER_TRIGGER;
                    break;
                case 8:                 //8AI for i-7017
                    iMemory_AI[86]=OVER_TRIGGER;
                    break;
                case 9:                 //8AI for i-7017
                    iMemory_AI[87]=OVER_TRIGGER;
                    break;
                case 10:                    //8AI for i-7017
                    iMemory_AI[88]=OVER_TRIGGER;
                    break;
                case 11:                    //4AO for i-7024
                    iMemory_AI[91]=OVER_TRIGGER;
                    break;
                case 12:                    //4AO for i-7024
                    iMemory_AI[92]=OVER_TRIGGER;
                    break;
                case 13:                    //4AO for i-7024
                    iMemory_AI[93]=OVER_TRIGGER;
                    break;
                case 14:                    //4AO for i-7024
                    iMemory_AI[94]=OVER_TRIGGER;
                    break;
                default:
                    break;
            
            }//switch(  iSendCmdProcess)*/
            
            break; 
        case 3: // buffer 已經滿了 
//          printCom1("[buffer is full]\r\n");
            switch( iSendCmdProcess)
            {
                
                case 0:                 //initial 
                    break;

                case 1:                 //4DI for i-7044
                    iMemory_AI[79]=OVER_BUFFER;
                    break;
                case 2:                 //8DO for i-7044
                        iMemory_AI[99]=OVER_BUFFER; 
                        break;
                case 3:                 //8AI for i-7017
                    iMemory_AI[81]=OVER_BUFFER;
                    break;
                case 4:                 //8AI for i-7017
                    iMemory_AI[82]=OVER_BUFFER;
                    break;
                case 5:                 //8AI for i-7017
                    iMemory_AI[83]=OVER_BUFFER;
                    break;
                case 6:                 //8AI for i-7017
                    iMemory_AI[84]=OVER_BUFFER;
                    break;
                case 7:                 //8AI for i-7017
                    iMemory_AI[85]=OVER_BUFFER;
                    break;
                case 8:                 //8AI for i-7017
                    iMemory_AI[86]=OVER_BUFFER;
                    break;
                case 9:                 //8AI for i-7017
                    iMemory_AI[87]=OVER_BUFFER;
                    break;
                case 10:                    //8AI for i-7017
                    iMemory_AI[88]=OVER_BUFFER;
                    break;
                case 11:                    //4AO for i-7024
                    iMemory_AI[91]=OVER_BUFFER;
                    break;
                case 12:                    //4AO for i-7024
                    iMemory_AI[92]=OVER_BUFFER;
                    break;
                case 13:                    //4AO for i-7024
                    iMemory_AI[93]=OVER_BUFFER;
                    break;
                case 14:                    //4AO for i-7024
                    iMemory_AI[94]=OVER_BUFFER;
                    break;
                default:
                    break;
            
            }//switch(  iSendCmdProcess)*/

            break; 
        case 4: // 超過Mastertimeout沒有回應
//          printCom1("[MasterTimeout!!]  \r\n");
            switch( iSendCmdProcess)
            {
                
                case 0:                 //initial 
                    break;
                case 1:                 //4DI for i-7044
                    iMemory_AI[79]=NO_RESPONSE;
                    break;
                case 2:                 //8DO for i-7044
                        iMemory_AI[99]=NO_RESPONSE; 
                        break;
                case 3:                 //8AI for i-7017
                    iMemory_AI[81]=NO_RESPONSE;
                    break;
                case 4:                 //8AI for i-7017
                    iMemory_AI[82]=NO_RESPONSE;
                    break;
                case 5:                 //8AI for i-7017
                    iMemory_AI[83]=NO_RESPONSE;
                    break;
                case 6:                 //8AI for i-7017
                    iMemory_AI[84]=NO_RESPONSE;
                    break;
                case 7:                 //8AI for i-7017
                    iMemory_AI[85]=NO_RESPONSE;
                    break;
                case 8:                 //8AI for i-7017
                    iMemory_AI[86]=NO_RESPONSE;
                    break;
                case 9:                 //8AI for i-7017
                    iMemory_AI[87]=NO_RESPONSE;
                    break;
                case 10:                    //8AI for i-7017
                    iMemory_AI[88]=NO_RESPONSE;
                    break;
                case 11:                    //4AO for i-7024
                    iMemory_AI[91]=NO_RESPONSE;
                    break;
                case 12:                    //4AO for i-7024
                    iMemory_AI[92]=NO_RESPONSE;
                    break;
                case 13:                    //4AO for i-7024
                    iMemory_AI[93]=NO_RESPONSE;
                    break;
                case 14:                    //4AO for i-7024
                    iMemory_AI[94]=NO_RESPONSE;
                    break;
                default:
                    break;
            }
            
            break;
        default:
//          printCom1("[Error mode %d]\n\r",mode);
            break;  
    }

    com->Size=0;    
    iReBack=0;
}

void Modbus_Request_Event(char* CommandData, int* CommandLength)
{
    int iAO=0, i;
    int iRef=0, iref=0;
    char DOvalue[10]={0}, AOvalue[24]={0};
    float outvalueAO=0;

    switch(CommandData[7])
    {
        case 0x05://<---Write signsle DO
            iReBack = 1;
//          printCom1("Write signsle DO\r\n");
            iRef=(int)((CommandData[8]<<8)+(CommandData[9]&0x00FF));
            sprintf(DOvalue,"#031%d%02d",iRef,CommandData[10]&0x01);
//          printCom1("%s\r\n",DOvalue);
            SendCmdTo7000(2, DOvalue, 0);
            MyCom2.MasterStartTimeTicks=GetTimeTicks();            
            break;
            
        case 0x06://<---Write signsle AO
            iReBack = 1;
//          printCom1("Write signsle AO\r\n");
            iRef=(int)((CommandData[8]<<8)+(CommandData[9]&0x00FF));            
            iAO=(int)((CommandData[10]<<8)+(CommandData[11]&0x00FF));
            outvalueAO=iAO/32767.0*10;
            sprintf(AOvalue, "#01%d%+06.3f", iRef, outvalueAO);                
//          printCom1("write %d AO=%s\r\n",iRef,AOvalue);
            SendCmdTo7000(2, AOvalue, 0);
            MyCom2.MasterStartTimeTicks=GetTimeTicks();
            break;
            
        case 0x0F://<--multiple DO
            iReBack = 1;
//          printCom1("multiple DO\r\n");
            iref=(int)((CommandData[10]<<8)+(CommandData[11]&0x00FF));
            if(iref==1)
            {
                iRef=(int)((CommandData[8]<<8)+(CommandData[9]&0x00FF));
                sprintf(DOvalue,"#031%d%02d", iRef, CommandData[13]&0x01);            
            }
            else
            {
                sprintf(DOvalue, "@03%02X", CommandData[13]&0xFF);
            }
//          printCom1("%s\r\n",DOvalue);
            SendCmdTo7000(2, DOvalue,0);
            MyCom2.MasterStartTimeTicks=GetTimeTicks();            
            break;
            
        case 0x10://multiple AO
            iReBack=1;

            iref=(int)((CommandData[10]<<8)+(CommandData[11]&0x00FF));
            if(iref==1)
            {
                iRef=(int)((CommandData[8]<<8)+(CommandData[9]&0x00FF));
                iAO=(int)((CommandData[13]<<8)+(CommandData[14]&0x00FF));
                outvalueAO=iAO/32767.0*10;
                sprintf(AOvalue, "#01%d%+06.3f", iRef, outvalueAO);            
                SendCmdTo7000(2, AOvalue, 0);
            }
            else
            {
                for(i=0; i<iref; i++)
                {
                    iAO=(int)((CommandData[13+i*2]<<8)+(CommandData[14+i*2]&0x00FF));
                    outvalueAO=(int)iAO/32767.0*10;  
                    sprintf(AOvalue, "#01%d%+06.3f", i, outvalueAO);
                    SendCmdTo7000(2, AOvalue, 0);
                }
            }
            
            MyCom2.MasterStartTimeTicks=GetTimeTicks();
            
            break;
    }
}

void Modbus_Response_Event(char* ResponseData,int* ResponseLength)
{
    
}

//============================//
//        Main Program        //    
//============================//
void main(int argc,char *argv[])
{
    XS_main(argc,argv); /* call the XS library main function. */
}
