/*
    XDemo15: Reads channel value from 7017 or 7018

    Compiler: BC++ 3.1
    Compile mode: large
    Project: user.c
             v7000.c
             vModbus.c           [after vcom3002.lib]
             ..\Lib\7186EL.Lib
             ..\Lib\tcp_dm32.lib
             ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version.

    19n -> read a value from channel n of 7017 or 7018.

    This demo shows how to send command to read channel value
    from 7017 or 7018 which is connected to 7188E's COM2.

    Hardware: uPAC-7186EX + 7017/7018 whose address is 01,
                                      baudrate is 115200,
                                      checksum is disable.

    [Dec 18, 2008] by Liam
*/
 
#include <string.h>
#include <stdio.h>
#include "..\lib\7186e.h"
#include "..\lib\Tcpip32.h"
#include "..\lib\vxcomm.h"


struct data {
    char buf[8];    // +01.234  7 data chars + 1 zero char = 8 chars
} Value[8];         // 8 channels


char str[80];

void UserCount(void)
{
    /*
    User's timer trigger function.
    Please refer to XDemo04 for detail description.
    Please refer to XDemo09 for example code.
    */

    int i,iPointer;
    int iChannel;
    char str[80];    // string recived from 7018 or 7017 maximun length is 80 chars

    SendCmdTo7000(2, "#01", 0);   // reads all values of all channels
    ReceiveResponseFrom7000(2, str, 1000, 0);

    /*
        Response string like 
        ">+00.000+01.000+02.000+03.000+04.000+05.000+06.000+07.000"
        str[0] is '>'. The data of values starts at 1. 
    */
    iPointer=1; 
    for(iChannel=0; iChannel<=7; iChannel++) //Reads out value of each channel.
    {
        for(i=0; i<7; i++)   //one channel has 7 characters.
        {
            Value[iChannel].buf[i]=str[iPointer++];
        }
        Value[iChannel].buf[7]=0;   // add the zero end to the string.
        printCom1("Channel %d : %s \n\r", iChannel, Value[iChannel].buf);
    }
}

void UserInit(void)
{
    /*
    Initialize user's program.
    Please refer to XDemo04 for detail description.
    Please refer to XDemo09 for example code.
    */  
    
    /*
    All of the user.c on XServer demo program needn't to call Installcom() function.
    Becuase they have installed When the Xserver was initial.
    If you want to change the baud rate.data format, 
    please use the function SetBaudrate().SetDataFormat().
    */
    InitLib();
    
    SetBaudrate1(115200);
    SetBaudrate2(115200);
    AddUserTimerFunction(UserCount, 1000); // call UserCount every 1000 ms
}

void UserLoopFun(void)
{
    /*
    VxComm.exe will call this function every scan time
    Please refer to XDemo11 for Real-time I/O control
    */    
}

int UserCmd(unsigned char *Cmd,unsigned char *Response)
{
    /*  
    Xserver executes this function when received a package 
    form TCP port 10000 and the first two bytes are "19".
    
    Funtion of Xserver, Please refer to XDemo04 for detail description.
    */  

    int i;

    if (Cmd[0])   // Not Null command
    {
        sscanf(Cmd, "%d", &i);              // read channel value
        sprintf(Response, "Channel %d: %s \n\r", i, Value[i].buf);
        return 1;           // return OK
    }
    
    return 0;               // return error
}

int VcomUserBinaryCmd(TCPREADDATA *p)
{
    /* 
    VXCOMM.EXE 2.6.12(04,Sep,2001) or later will support this function.

    Xserver executes this function when received a package 
    form TCP port 10000 and the first two bytes are "23".
    
    Please refer to XDemo04 for detail description.
    Please refer to XDemo23 for example code.
    */
    return 1;   /* any value will be accept */
}

int VcomCmdUser(TCPREADDATA *p)  
{ 
    /*
    VCOM3005 (Feb,22,2002) or later will call this function for PortUser.
    When packets received by TCP PORT PortUser(user defined) 
    of 7188E/8000E, Xserver will call this function.

    Please refer to XDemo04 for detail description.
    */
    VcomSendSocket(p->Socket,p->ReadUartChar,p->Length);
    return 1;   /* any value will be accept */  
}

void PortUserStart(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E via the user's 
    defined port(PortUser), the Xserver calls the function once.

    Please refer to XDemo04 for detail description.
    */
    
    skt=skt; //do nothing
}
  
void Port9999Start(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E TCP port 9999, 
    the Xserver calls the function once.
 
    Please refer to XDemo04 for detail description.
    */
    
    skt=skt; //do nothing
}
 
void Port502Start(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E TCP port 502, 
    the Xserver calls the function once.
    
    Please refer to XDemo04 for detail description.
    */
    
    skt=skt; //do nothing
}
