Option Strict Off
Option Explicit On 

Imports System
Imports System.Runtime.InteropServices
Imports System.Text

'Namespace InterfaceCorpDllWrap

Public Class IFCGPIB
    Public Const GPIB_SUCCESS_NOT_FOUND_LISTENER As Integer = 11        ' No Listener is found.
    Public Const GPIB_SUCCESS_OK_SEND_STB As Integer = 7                ' The device has already sent a status byte.
    Public Const GPIB_SUCCESS_NOT_EXEC_SPOLL As Integer = 6             ' The Controller-in-charge has not done serial polling yet.
    Public Const GPIB_SUCCESS_NOT_ACTIVE_SRQ As Integer = 5             ' The SRQ line is deasserted.
    Public Const GPIB_SUCCESS_ACTIVE_SRQ As Integer = 4                 ' The SRQ line is asserted.
    Public Const GPIB_SUCCESS_OK_EOI_DETECT As Integer = 2              ' The read operation was successfully completed with detecting the EOI assertion.
    Public Const GPIB_SUCCESS_OK_RECV_DATA_CNT As Integer = 1           ' The read operation was terminated when count of data byte received reaches the specified number. At this point, delimiter characters have not been detected.
    Public Const GPIB_SUCCESS_OK_DELIM_DETECT As Integer = 0            ' The read operation was successfully completed with detecting the specified delimiter.
    Public Const GPIB_SUCCESS As Integer = 0                            ' The process was successfully completed.
    Public Const GPIB_ERROR_ILLEGAL_BOARDNO As Integer = -1             ' The specified board number is invalid.
    Public Const GPIB_ERROR_ILLEGAL_INP_PARAM As Integer = -2           ' The given parameter is invalid.
    Public Const GPIB_ERROR_ILLEGAL_PARAM_NO As Integer = -3            ' The specified parameter number is out of the range.
    Public Const GPIB_ERROR_NOT_USE_SLAVE As Integer = -4               ' The interface is not the Controller-in-charge.
    Public Const GPIB_ERROR_NOT_USE_NONE_CIC As Integer = -4            ' The interface is not a Listener or Talker.
    Public Const GPIB_ERROR_NOT_USE_MASTER As Integer = -5              ' This function API is not available for the interface that is the Master mode.
    Public Const GPIB_ERROR_NOT_USE_CIC As Integer = -5                 ' This function API is not available for the interface that is the Controller-in-charge.
    Public Const GPIB_ERROR_NOT_BOTH_USE As Integer = -6                ' The interface cannot be specified both the Controller-in-charge and a Listener or a Talker at the same time.
    Public Const GPIB_ERROR_WRITE_BUS_COMMAND As Integer = -7           ' Failed to send a GPIB command.
    Public Const GPIB_ERROR_NO_SET_SIGNAL As Integer = -8               ' No event mask is given. Specify one or more events to be detected.
    Public Const GPIB_ERROR_NO_ACTIVE_SRQ As Integer = -9               ' The Controller-in-charge cannot determine a device that requests service at the serial polling.
    Public Const GPIB_ERROR_STB_RECV_TMO As Integer = -10               ' The timeout interval is elapsed before the Controller-in-charge receives a status byte completely.
    Public Const GPIB_ERROR_DATA_RECV As Integer = -12                  ' Failed to read an operation. Asynchronous transfer is in progress on the GPIB.
    Public Const GPIB_ERROR_DATA_SEND As Integer = -13                  ' Failed to write an operation. Asynchronous transfer is in progress on the GPIB.
    Public Const GPIB_ERROR_TRANS_TMO As Integer = -14                  ' The timeout interval is elapsed before the read operation is completed.
    Public Const GPIB_ERROR_WAIT_SIGNAL_TMO As Integer = -15            ' The timeout interval is elapsed before expected GPIB events occur.
    Public Const GPIB_ERROR_IFC_TRANS_EXIT As Integer = -16             ' The I/O operation is aborted by the IFC signal assertion.
    Public Const GPIB_ERROR_NOT_CACS As Integer = -19                   ' The interface can not become Active-Controller.
    Public Const GPIB_ERROR_NOW_BUS_OCCUPATION As Integer = -20         ' Asynchronous transfer is in progress on the GPIB.
    Public Const GPIB_ERROR_NO_SET As Integer = -22                     ' The device driver cannot change configuration parameters.
    Public Const GPIB_ERROR_NOT_START_TIMER As Integer = -24            ' The timer cannot start.
    Public Const GPIB_ERROR_EXIST_START_TIMER As Integer = -25          ' The timer is running.
    Public Const GPIB_ERROR_NOT_TICK_TIMER As Integer = -26             ' The timer is not running.
    Public Const GPIB_ERROR_FILE_ACCESS As Integer = -30                ' Access to the file is denied. Failed to open the file.
    Public Const GPIB_ERROR_SET_CALLBACK_EVENT As Integer = -40         ' Failed to register the callback routine.
    Public Const GPIB_ERROR_KILL_CALLBACK_EVENT As Integer = -41        ' Failed to remove the callback routine.
    Public Const GPIB_ERROR_WAIT_OBJECT_SIGNAL As Integer = -42         ' The event object is enabled.
    Public Const GPIB_ERROR_WAIT_OBJECT_TMO As Integer = -43            ' The timeout interval is elapsed before the event object is signaled.
    Public Const GPIB_ERROR_WAIT_OBJECT_FAILED As Integer = -44         ' An error occurred before the event object is signaled. 
    Public Const GPIB_ERROR_NOT_SET_CALLBACK_EVENT As Integer = -45     ' No event is registered.
    Public Const GPIB_ERROR_NOT_SYS_CONTROLLER As Integer = -50         ' The interface is not System Controller.
    Public Const GPIB_ERROR_NOT_CREATE_EVENT As Integer = -992          ' Failed to create the event.
    Public Const GPIB_ERROR_NOT_CREATE_THREAD As Integer = -993         ' Failed to create the thread.
    Public Const GPIB_ERROR_NOT_INIT_CALL As Integer = -994             ' The function API is not available before the board is initialized properly.
    Public Const GPIB_ERROR_BOARD_CLOSE As Integer = -995               ' Failed to close the board.
    Public Const GPIB_ERROR_NOT_ALLOC_PAGE As Integer = -996            ' The driver software cannot allocate enough memory.
    Public Const GPIB_ERROR_NOT_USE_TIMER As Integer = -997             ' The timer is not successfully configured.
    Public Const GPIB_ERROR_NOT_USE_RESOURCE As Integer = -998          ' Hardware resources such as IRQ level may conflict with the system or other devices.
    Public Const GPIB_ERROR_NOT_BOARD As Integer = -999                 ' The board is not correctly installed or the I/O port address conflicts with the system or other devices.
    Public Const GPIB_ERROR_NOT_SUPPORT As Integer = -2000              ' This API is not supported.

    Delegate Sub PLPSRQCALLBACK(ByVal BoardNo As Integer, ByVal UserData As Integer)

    Declare Function GpibOpen Lib "gpc43042.dll" (ByVal BoardNo As Integer) As Integer

    Declare Function GpibClose Lib "gpc43042.dll" (ByVal BoardNo As Integer) As Integer

    Declare Function GpibSetConfig Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal Param As String) As Integer

    Declare Function GpibGetConfig Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal PrmNo As Integer, ByRef GetPrm As Integer) As Integer

    Declare Function GpibSetIfc Lib "gpc43042.dll" (ByVal BoardNo As Integer) As Integer

    Declare Function GpibSetRen Lib "gpc43042.dll" (ByVal BoardNo As Integer) As Integer

    Declare Function GpibExecDeviceTrigger Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer) As Integer

    Declare Function GpibExecDeviceClear Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer) As Integer

    Declare Function GpibExecSpoll Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByVal StbTbl() As Integer, ByVal StbAdrs() As Integer) As Integer

    Declare Function GpibCheckSrq Lib "gpc43042.dll" (ByVal BoardNo As Integer) As Integer

    Declare Function GpibSend Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByVal Length As Integer, ByRef Buffer As Byte) As Integer

    Declare Function GpibSend Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByVal Length As Integer, ByVal Buffer() As Byte) As Integer

    Declare Function GpibSend Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByVal Length As Integer, ByVal Buffer As String) As Integer

    Declare Function GpibSend Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByVal Length As Integer, ByVal Buffer As IntPtr) As Integer

    Declare Function GpibReceive Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByRef Length As Integer, ByRef Buffer As Byte) As Integer

    Declare Function GpibReceive Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByRef Length As Integer, ByVal Buffer() As Byte) As Integer

    Public Shared Function GpibReceive (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByRef ReadLength As Integer, ByRef Buffer As String) As Integer
        Dim Ret As Integer
        Dim e As ASCIIEncoding = new ASCIIEncoding
        Dim Data() As Byte = New Byte(ReadLength) {}

        Buffer = ""

        Ret = GpibReceive(BoardNo, AdrsTbl, ReadLength, Data)
        If Ret >= 0 Then
            Buffer = e.GetString(Data, 0, ReadLength)
        End If
        GpibReceive = Ret
    End Function

    Declare Function GpibReceive Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal AdrsTbl() As Integer, ByRef Length As Integer, ByVal Buffer As IntPtr) As Integer

    Declare Function GpibSetSrqEvent Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal SrqProc As PLPSRQCALLBACK, ByVal UserData As Integer) As Integer

    Declare Function GpibWaitSrqEvent Lib "gpc43042.dll" (ByVal BoardNo As Integer, ByVal TimeOut As Integer) As Integer

    Declare Function GpibKillSrqEvent Lib "gpc43042.dll" (ByVal BoardNo As Integer) As Integer


End Class

