Imports System.IO.Ports
Public Class Form1
    Private hPort As IntPtr

    Public Sub New()
        InitializeComponent()
        Dim ports As String() = SerialPort.GetPortNames()
        Array.Sort(ports)

        For Each port As String In ports
            If port <> "COM1" Then
                cmbPort.Items.Add(port)
            End If
        Next

        cmbPort.SelectedIndex = 0
    End Sub
    Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click
        'Open com port
        hPort = PACNET.UART.Open(cmbPort.Text + "," + txtBaurdRate.Text)
        If hPort = CType(-1, IntPtr) Then

            MessageBox.Show("open com error,error code " & PACNET.ErrHandling.GetLastError())
        Else
            btnOpenPort.Enabled = False
            btnClosePort.Enabled = True
        End If
    End Sub

    Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosePort.Click
        'Close com port
        PACNET.UART.Close(hPort)
        btnOpenPort.Enabled = True
        btnClosePort.Enabled = False
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim iRet As Boolean
        Dim result As Byte() = New Byte(19) {}

        'Send command
        iRet = PACNET.UART.SendCmd(hPort, PACNET.MISC.AnsiString(txtSendValue.Text), result)
        If iRet = False Then
            MessageBox.Show("send error")
        Else
            'Show Response to TextBox
            txtResponseValue.Text = PACNET.MISC.WideString(result)
        End If
    End Sub
End Class
