Imports System.Text                     'Encodingのために定義

Public Class Form1

    Const OUT_PIPE As Integer = 1                                      'USB送信用パイプ番号
    Const IN_PIPE As Integer = 0                                       'USB受信用パイプ番号
    Const USB_RECEIVE_MAX As Integer = 512

    Dim OutDeviceHandle As Integer                             'USB送信用ハンドル
    Dim InDeviceHandle As Integer                              'USB受信用ハンドル

    '    Module1.basで定義
    '    Declare Function CloseHandle Lib "KERNEL32" (ByVal hFile As Integer) As Integer
    '    Declare Function OpenUSBBulkFile Lib "HiUsbL.dll" (ByVal pipeNo As Integer, ByVal ProductID As Integer, ByVal SN As Integer) As Integer
    '    Declare Function ReadUSBBulk Lib "HiUsbL.dll" (ByVal hFile As Integer, ByRef lpBuffer As Byte, ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer) As Integer
    '    Declare Function WriteUSBBulk Lib "HiUsbL.dll" (ByVal hFile As Integer, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    '    Module1.basで定義


    Dim MsgBuf As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '*******************************************************************************
        ' LAN SAMPLE PROGRAM NO.1
        '
        'このプログラムは、本器に接続してコマンドの送受信を行います。
        'コマンドの欄に送信したいコマンドを入力し、コマンド実行ボタンを押すと送信されます。
        '応答があるコマンド（?）が含まれるコマンドの場合は、テキストボックスに応答が表示されます。
        '*******************************************************************************

        If InStr(TextBox3.Text, "?") = 0 Then                   'コマンドに?が含まれない場合は、コマンド送信のみ

            SendMsgCrLf(TextBox3.Text)                          'コマンド送信
            TextBox1.Text = ""

        Else                                                    'コマンドに?が含まれる場合は、コマンド送信と応答受信

            GetMsgCrLf(TextBox3.Text)                           'コマンド送信と応答受信
            TextBox1.Text = MsgBuf

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        OutDeviceHandle = OpenUSBBulkFile(OUT_PIPE, CInt(Val("8423")), CInt(Val(TextSerial.Text)))
        InDeviceHandle = OpenUSBBulkFile(IN_PIPE, CInt(Val("8423")), CInt(Val(TextSerial.Text)))

        TextBox2.Text = "USB接続完了"

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        CloseHandle(OutDeviceHandle)
        CloseHandle(InDeviceHandle)

        TextBox2.Text = "USB切断完了"

    End Sub

    Private Sub SendMsgCrLf(ByVal strMsg As String)

        Dim sendCount As Integer
        Dim ans As Integer

        strMsg = strMsg & vbCrLf
        ans = WriteUSBBulk(OutDeviceHandle, strMsg, Len(strMsg), sendCount)            'ﾒｯｾｰｼﾞ送信

    End Sub

    Private Sub GetMsgCrLf(ByVal strMsg As String)

        Dim sendCount As Integer
        Dim recvCount As Integer
        Dim ans As Integer
        Dim recvBuff(USB_RECEIVE_MAX * 2) As Byte
        Dim i As Integer
        Dim c As Integer

        strMsg = strMsg & vbCrLf
        ans = WriteUSBBulk(OutDeviceHandle, strMsg, Len(strMsg), sendCount)            'ﾒｯｾｰｼﾞ送信

        ans = ReadUSBBulk(InDeviceHandle, recvBuff(0), USB_RECEIVE_MAX, recvCount)           'ﾒｯｾｰｼﾞ受信

        MsgBuf = ""
        For i = 0 To recvCount - 1
            c = recvBuff(i)
            If c > &H1F Then
                MsgBuf = MsgBuf + Chr(c)
            ElseIf Chr(c) = vbCr Then
                Exit For
            End If
        Next

    End Sub

End Class
