'------------------------------------------------------------------
'    Version 1.0.0
'    [10 Aug, 2006] by Bill 
'------------------------------------------------------------------
Attribute VB_Name = "Lib"
 Option Explicit
'------------------------ Error Message ------------------
Global Const NoError = 0
Global Const FunctionError = 1
Global Const PortError = 2
Global Const BaudRateError = 3
Global Const DataError = 4
Global Const StopError = 5
Global Const ParityError = 6
Global Const CheckSumError = 7
Global Const ComPortNotOpen = 8
Global Const SendThreadCreateError = 9
Global Const SendCmdError = 10
Global Const ReadComStatusError = 11
Global Const ResultStrCheckError = 12
Global Const CmdError = 13
Global Const TimeOut = 15
Global Const ModuleIdError = 17
Global Const AdChannelError = 18
Global Const UnderInputRange = 19
Global Const ExceedInputRange = 20
Global Const InvalidateCounterNo = 21
Global Const InvalidateCounterValue = 22

'----------------------  UART.DLL -----------------------------------------------
Declare Function Get_Uart_Version Lib "uart.dll" () As Integer

Declare Function Open_Com Lib "uart.dll" (ByVal port As Integer, ByVal BaudRate As Long, ByVal cData As Byte, ByVal cParity As Byte, ByVal cStop As Byte) As Integer

Declare Function Change_Baudrate Lib "uart.dll" (ByVal port As Integer, ByVal lBaudrate As Long) As Integer

Declare Function Change_Config Lib "uart.dll" (ByVal port As Integer, ByVal BaudRate As Long, ByVal cData As Byte, ByVal cParity As Byte, ByVal cStop As Byte) As Integer

Declare Function Get_Com_Status Lib "uart.dll" (ByVal port As Integer) As Integer

Declare Function Close_Com Lib "uart.dll" (ByVal port As Integer) As Boolean

Declare Function Send_Binary Lib "uart.dll" (ByVal port As Integer, ByRef szBuf As Byte, ByVal length As Integer) As Integer

Declare Function Receive_Binary Lib "uart.dll" (ByVal port As Integer, ByRef szResult As Byte, ByVal TimeOut As Integer, ByVal length As Integer, wT As Integer) As Integer
        
'Receive_Binary(unsigned char cPort, char szResult[], WORD wTimeOut, WORD wLen, WORD *wT)

Declare Function Send_Cmd Lib "uart.dll" (ByVal port As Integer, ByVal Cmd As String, ByVal wChkSum As Integer) As Integer

Declare Function Receive_Cmd Lib "uart.dll" (ByVal port As Integer, ByVal szResult As String, ByVal TimeOut As Integer, ByVal Checksum As Integer, wT As Integer) As Integer


Declare Function Send_Receive_Cmd Lib "uart.dll" (ByVal port As Integer, ByVal szCmd As String, ByVal szResult As String, ByVal TimeOut As Integer, ByVal Checksum As Integer, wT As Integer) As Integer


Declare Function DCON_Write_DO Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
    ByVal iSlot As Integer, ByVal iDO_TotalCh As Integer, ByVal lDO_Value As Long, _
    ByVal iCheckSum As Integer, ByVal iTimeOut As Integer) As Integer
'/****************************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iDO_TotalCh: 1~32. total DO channels
'    lDO_Value: DO value.
'                for example: 0x32==> ch5=1, ch4=1, ch1=1,
'                                     others are 0.
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'    return: 0 = No Error
'            other = error code.
'****************************************************************************/

Declare Function DCON_Write_DO_Bit Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iDO_TotalCh As Integer, _
        ByVal iBitValue As Integer, ByVal iCheckSum As Integer, ByVal iTimeOut As Integer) As Integer
'/***********************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)0
'    iChannel: 0~31
'    iDO_TotalCh: 1~32, Total DO channels
'    iBitValue: 0   = Off
'               <>0 = On
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'    return: 0 = No Error
'            other = error code.
'*************************************************************************/

Declare Function DCON_Read_DI_Counter Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iDI_TotalCh As Integer, ByVal iCheckSum As Integer, _
        ByVal iTimeOut As Integer, ByRef lCounter_Value As Long) As Integer
'/*******************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iChannel: 0~15
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'    *lCounter_Value: counter value.
'
'    return: 0 = No Error
'            other = error code.
'**********************************************************************/

Declare Function DCON_Clear_DI_Counter Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iDI_TotalCh As Integer, _
        ByVal iCheckSum As Integer, ByVal iTimeOut As Integer) As Integer
'/******************************************************************
'iComPort: 1 ~ 255
'iAddress: 0 ~ 255: the RS-485 Address of module
'iSlot: 0~7: for 8K and 87K series on i-8000 system
'        -1: 7K and 87K (on 87K IO Expansion bus)
'iChannel: 0~15
'iDI_TotalCh: 1~32. total DI channels
'iCheckSum: 0: disabled
'1:            Enabled
'iTimeOut: timeout value to wait response. unit= ms.
'
'return: 0 = No Error
'        other = error code.
'******************************************************************/

Declare Function DCON_Write_AO Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iAO_TotalCh As Integer, _
        ByVal fValue As Single, ByVal iCheckSum As Integer, ByVal iTimeOut As Integer) As Integer
'/***************************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iChannel: 0~3
'    iAO_TotalCh: 1~4. total AO channels
'fValue:     AO Value
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'    return: 0 = No Error
'            other = error code.
'*****************************************************************************/

Declare Function DCON_Read_AO Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iAO_TotalCh As Integer, _
        ByVal iCheckSum As Integer, ByVal iTimeOut As Integer, ByRef fValue As Single) As Integer
'/***********************************************************************************
'   iComPort: 1 ~ 255
'   iAddress: 0 ~ 255: the RS-485 Address of module
'   iSlot: 0~7: for 8K and 87K series on i-8000 system
'           -1: 7K and 87K (on 87K IO Expansion bus)
'   iAO_TotalCh: 1~4. total AO channels,used to define command and response
'
'   iCheckSum: 0: disabled
'1:               Enabled
'   iTimeOut: timeout value to wait response. unit= ms.
'
'   iDataFormat: 0: Engineering format
'1:                  Percent Format
'2:                  Hex Format
'
'iValue:    Hex Format
'fValue:    Float Format
'
'   return: 0 = No Error
'           15= timeout (No response)
'
'
'    Note: user can readback AO output in either float value or hex format ,
'          according to the data format of module.
'          but user must declare both the variable in function
'    for example:
'    float fVal;
'    int iVal,format;
'    short ret;
'    ....
'    format=0;
'    fVal=5.0;
'    iVal=0;
'    ret= DCON_Read_AO(1,1,-1,0,4,0,300,format,&fVal,&iVal)as integer
'
'    //then the current AO value will be fVal, the iVal will get the default 0
'**********************************************************************************/

Declare Function DCON_Read_AI Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iAI_TotalCh As Integer, _
        ByVal iCheckSum As Integer, ByVal iTimeOut As Integer, ByVal iDataFormat As Integer, _
        ByRef fValue As Single, ByRef iValue As Integer) As Integer
'/*****************************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: for 7K and 87K (on 87K IO Expansion bus)
'    iChannel: 0~7
'    iAI_TotalCh: 1~ N, total AI channels
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'    *fValue:the AI value (engineering , % or ohmn data type).
'
'    return: 0 = No Error
'            other = error code.
'******************************************************************************/

Declare Function DCON_Read_Counter Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iCheckSum As Integer, _
        ByVal iTimeOut As Integer, ByRef lCounter_Value As Long) As Integer
'/**********************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iChannel: 0~15
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'    *lCounter_Value: counter value.
'
'    return: 0 = No Error
'            other = error code.
'***********************************************************************/

Declare Function DCON_Clear_Counter Lib "DCON_PC.dll" (ByVal iComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iCheckSum As Integer, _
        ByVal iTimeOut As Integer) As Integer
'/*********************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iChannel: 0~15
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'    return: 0 = No Error
'            other = error code.
'***********************************************************************/

Declare Function DCON_Read_DIO Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iDI_TotalCh As Integer, ByVal iDO_TotalCh As Integer, _
        ByVal iCheckSum As Integer, ByVal iTimeOut As Integer, ByRef lDI_Value As Long, _
        ByRef lDO_Value As Long, ByRef cDI_BitValue As Byte, ByRef cDO_BitValue As Byte) As Integer
'/*********************************************************************************
'   iComPort: 1 ~ 255
'   iAddress: 0 ~ 255: the RS-485 Address of module
'   iSlot: 0~7: for 8K and 87K series on i-8000 system
'           -1: 7K and 87K (on 87K IO Expansion bus)
'   iDI_TotalCh: 0~32. total DI channels,used to define the input bytes
'   iDO_TotalCh: 0~32. total DO channels,used to define the output bytes
'   iCheckSum: 0: disabled
'1:               Enabled
'   iTimeOut: timeout value to wait response. unit= ms.
'   *lDI_Value: DI value.
'               for example: 0x32==> ch5=1, ch4=1, ch1=1,
'                                    others are 0.
'   *lDO_Value: DO read back value.
'               for example: 0x32==> ch5=on, ch4=on, ch1=on,
'                                    others are off.
'   *cDI_BitValue: DI value.
'                  for example: {0,1,0,0,1,1}
'                  ==> bit0=0,bit1=1,bit2=0,bit3=0,bit4=1,bit5=1.
'   *cDO_BitValue: DO read back value.
'                  for example: {0,1,0,0,1,1}
'                  ==> bit0=0,bit1=1,bit2=0,bit3=0,bit4=1,bit5=1.
'
'    return: 0 = No Error
'            15= timeout (No response)
'**********************************************************************************/

Declare Function DCON_READ_DIO_Latch Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iDI_TotalCh As Integer, ByVal iDO_TotalCh As Integer, _
        ByVal iLatchType As Integer, ByVal iCheckSum As Integer, ByVal iTimeOut As Integer, _
        ByRef lDI_Latch_Value As Long, ByRef lDO_Latch_Value As Long, _
        ByRef cDI_Latch_BitValue As Byte, ByRef cDO_Latch_BitValue As Byte) As Integer
'/***************************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iDI_TotalCh: 0~32. total DI channels,used to define input bytes
'    iDO_TotalCh: 0~32. total DO channels,used to define the output bytes
'    iLatchType: 0 = Logic Low Latched
'                1 = Logic High Latched
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'    *lDI_Latch_Value: DI latch value.
'                for example: 0x32==> ch5=1, ch4=1, ch1=1,
'                                     others are 0.
'    *lDO_Latch_Value: DO read back latch value.
'                for example: 0x32==> ch5=on, ch4=on, ch1=on,
'                                     others are off.
'    *cDI_Latch_BitValue: DI latch value.
'                   for example: {0,1,0,0,1,1}
'                   ==> bit0=0,bit1=1,bit2=0,bit3=0,bit4=1,bit5=1.
'    *cDO_Latch_BitValue: DO read back latch value.
'                   for example: {0,1,0,0,1,1}
'                   ==> bit0=0,bit1=1,bit2=0,bit3=0,bit4=1,bit5=1.
'
'    return: 0 = No Error
'            15= timeout (No response)
'****************************************************************************/

Declare Function DCON_Clear_DIO_Latch Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iCheckSum As Integer, ByVal iTimeOut As Integer) As Integer
'/*******************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'
'    return: 0 = No Error
'            15= timeout (No response)
'
'
'**********************************************************************/

Declare Function DCON_Read_Overflow Lib "DCON_PC.dll" (ByVal cComPort As Byte, ByVal iAddress As Integer, _
        ByVal iSlot As Integer, ByVal iChannel As Integer, ByVal iCheckSum As Integer, _
        ByVal iTimeOut As Integer, ByRef iOverflow As Long) As Integer
'/*******************************************************************
'    iComPort: 1 ~ 255
'    iAddress: 0 ~ 255: the RS-485 Address of module
'    iSlot: 0~7: for 8K and 87K series on i-8000 system
'            -1: 7K and 87K (on 87K IO Expansion bus)
'    iCheckSum: 0: disabled
'1:                Enabled
'    iTimeOut: timeout value to wait response. unit= ms.
'
'
'    return: 0 = No Error
'            15= timeout (No response)
'
'
'**********************************************************************/

