Imports System
Imports System.Text
Imports SiiPrinterSdk

Public Class DllFunc

    Public Delegate Sub callbackEventHandler(ByVal msg As String)
    Public Event myCallbackEvent As callbackEventHandler

    Shared indexCapability As UInteger = 0      ' Index of the table for ID of the GetPrinCapability function.
    Shared indexCounter As UInteger = 0         ' Index of the table for ID of the GetCounter function.
    Shared callbackMsgNum As UInteger = 0       ' Message number for the callback

    Private msg As String = ""
    Friend m_StatusAPI As StatusAPI             ' .NET API class

    Public Sub New()

        m_StatusAPI = New StatusAPI()
        AddHandler m_StatusAPI.StatusCallback, AddressOf CBFuncSampProc
    End Sub

    ' Callback status function sample
    Private Sub CBFuncSampProc(ByVal status As ASB)

        msg = String.Format("[{0:D2}] {1}", callbackMsgNum, status)
        RaiseEvent myCallbackEvent(msg)
        callbackMsgNum += 1
    End Sub

    ' Sample of OpenMonPrinter & CloseMonPrinter
    Public Function OpenSamp(ByVal printerName As String) As Boolean

        Dim returnValue As ErrorCode = 0

        If m_StatusAPI.IsValid = True Then
            CloseSamp()
        End If

        returnValue = m_StatusAPI.OpenMonPrinter(OpenType.TYPE_PRINTER, printerName)

        If returnValue = ErrorCode.SUCCESS Then
            Return True
        End If

        Return False
    End Function

    ' Sample of CloseMonPrinter
    Public Sub CloseSamp()

        m_StatusAPI.CloseMonPrinter()
    End Sub

    ' Sample of DirectIOEx
    Public Function DirectIOExSamp() As String

        Dim returnValue As ErrorCode = ErrorCode.SUCCESS
        Dim timeout As Long = 90000
        Dim readFlag As Boolean = True
        Dim readOption As Byte = 0

        Dim readData(256) As Byte
        Dim writeData() As Byte = Encoding.ASCII.GetBytes("Seiko Instruments Inc." + vbCrLf + vbCrLf)

        returnValue = m_StatusAPI.DirectIOEx(writeData, timeout)
        returnValue = m_StatusAPI.DirectIOEx(DevInfo.footerCmd, readData, timeout, readFlag, readOption)

        If returnValue = ErrorCode.SUCCESS Then
            msg = String.Format("[ DirectIOEx ]" + vbCrLf + vbCrLf)
            DispMsg(readData, readData.Length)
        Else
            msg = String.Format("Error ( {0} ) : [ DirectIOEx ]", returnValue)
        End If

        Return msg
    End Function

    ' Sample of SetStatusBack & CancelStatusBack
    Public Sub CallbackSamp(ByVal callbackStart As Boolean)

        If callbackStart Then
            m_StatusAPI.SetStatusBack()
        Else
            m_StatusAPI.CancelStatusBack()
        End If

    End Sub

    ' Sample of GetPrnCapability
    Public Function GetPrnCapabilitySamp() As String

        Dim readData(256) As Byte
        Dim returnValue As ErrorCode

        If indexCapability = DevInfo.tblCapability.Length Then
            indexCapability = 0
        End If

        msg = String.Format("[ GetPrnCapability : {0} ]" + vbCrLf + vbCrLf, DevInfo.tblCapability(indexCapability))
        returnValue = m_StatusAPI.GetPrnCapability(DevInfo.tblCapability(indexCapability), readData)

        If returnValue = ErrorCode.SUCCESS Then
            DispMsg(readData, readData.Length)
        Else
            msg = String.Format("Error ( {0} ) : [ GetPrnCapability : {1} ]", _
                returnValue, DevInfo.tblCapability(indexCapability))
        End If

        indexCapability += 1
        Return msg
    End Function

    ' Sample of GetCounter
    Public Function GetCounterSamp() As String

        Dim readData As Integer
        Dim returnValue As ErrorCode

        If indexCounter = DevInfo.tblCounterId.Length Then
            indexCounter = 0
        End If

        returnValue = m_StatusAPI.GetCounter(DevInfo.tblCounterId(indexCounter), readData)

        If returnValue = 0 Then
            msg = String.Format("[ GetCounter = {0} ]" + vbCrLf + vbCrLf + "{1}", _
                DevInfo.tblCounterId(indexCounter), readData)
        Else
            msg = String.Format("Error ( {0} ) : [ GetCounter = {1} ]", _
                returnValue, DevInfo.tblCounterId(indexCounter))
        End If

        indexCounter += 1
        Return msg
    End Function

    ' Display  
    Public Sub DispMsg(ByVal readData() As Byte, ByVal readLength As UInteger)

        Dim index As UInteger = 0

        If readLength > 0 Then

            While readLength > 0

                Dim tmpMsg As String = ""
                tmpMsg = String.Format("0x{0,2:x2} ", readData(index))
                index += 1
                msg += tmpMsg

                If index Mod 6 = 0 Then
                    msg += vbCrLf
                ElseIf index > 30 Then
                    msg += "...."
                    Exit While
                End If
                readLength -= 1
            End While
        Else
            msg += "NO DATA !!"
        End If

    End Sub

End Class
