/*
    XDemo16: Using COM ports to communicate with devices

    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.

    19Cmd -> send command string to COM port.
    
    This demo shows how to use COM port to communicate with the i7000, 87K series devices or 
    other series product without using the "SendCmdTo7000" and "ReceiveResponseFrom7000" function.
    In this case, the command and the response of these devices are not all terminated by 0x0d. 
    Please refer to the user's manual to modify the terminal char. 

    Hardware: uPAC-7186EX + i-7000/87K series module or ther provider's product
              whose baudrate is 9600 and 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"

#define terminal_char 0x0D  //depends on the terminal character of the device.

unsigned char InBuf[120];   //store data received from COM ports
unsigned char OutBuf[60];   //store command string that will be sent to COM ports

int Receive_Str(void)
{
    //receives one string that with a terminal character 0x0D.
    unsigned char data;
    int Index,tt;

    Index=0;
    tt=0;
    for(;;)
    {
        if(IsCom2())    //check COM2
        {
            data=ReadCom2();    //read data from COM2
            if(data==terminal_char)     //the terminal char is 0x0D
            {
                InBuf[Index]=0;
                return 0;       //receive data ok
            }
            else
                InBuf[Index++]=data;
        }
        tt++;
        if(tt>=30000)
            return -1;  //receive data timeout
    }
}


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

void UserInit(void)
{
    /*
    Initialize user's program.
    Please refer to XDemo04 for detail description.
    Please refer to XDemo09 for example code.
    */  

    /*
    All of the program user.c needn't function Installcom().
    Becuase they have install When the Xserver initialize.
    If you want to change the baud rate.data format, 
    please using the function SetBaudrate().SetDataFormat().
    */
    InitLib();

    SetBaudrate(2, 9600);
}

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,iRet;

    if (Cmd[0])   // Not Null command
    {
        strcpy(OutBuf,Cmd);
        i=strlen(OutBuf);
        OutBuf[i]=terminal_char;    //add the terminal char

        //OutBuf[i]=0x0d;           //add the terminal char
        ToCom2Bufn(OutBuf,i+1); //send i+1 bytes to COM2 of the 7188E
        iRet=Receive_Str();     //receive string from COM2 of the 7188E
        
        if(iRet==0) 
            strcpy(Response,InBuf);
        else
            strcpy(Response, "Receive timeout!");
            
        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
}
