Imports System.Text                     'Encoding

Public Class Form1


    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
        '
        ' This program connects to LR8400 and do the sending and receiving of a command.  
        ' When push a command execute button and input the command 
        ' that wants to transmit it to the column of a command it is transmitted.  
        ' The answer is displayed to a text box, in the case of the command that is included (? ).  
        '*******************************************************************************

        If InStr(TextBox3.Text, "?") = 0 Then                   '

            SendMsgCrLf(TextBox3.Text)                          'Send command
            TextBox1.Text = ""

        Else                                                    '

            GetMsgCrLf(TextBox3.Text)                           'Send command and receive query
            TextBox1.Text = MsgBuf

        End If

    End Sub

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

        SerialPort1.PortName = TextCom.Text                     'port set
        SerialPort1.Open()                                      'connect

        TextBox2.Text = "USB connect"

    End Sub

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

        SerialPort1.Close()                                     'dis connect

        TextBox2.Text = "USB dis connect"

    End Sub

    Private Sub SendMsgCrLf(ByVal strMsg As String)

        strMsg = strMsg & vbCrLf
        SerialPort1.WriteLine(strMsg)                           'send message

    End Sub

    Private Sub GetMsgCrLf(ByVal strMsg As String)

        strMsg = strMsg & vbCrLf
        SerialPort1.WriteLine(strMsg)                          'send message

        Dim Check As Integer
        MsgBuf = Nothing
        Do                                                     'wait for seceive
            Check = SerialPort1.ReadByte()
            If Chr(Check) = vbLf Then
                Exit Do
            ElseIf Chr(Check) = vbCr Then
            Else
                MsgBuf = MsgBuf & Chr(Check)
            End If
        Loop

        MsgBuf = MsgBuf & vbCrLf

    End Sub

End Class
