Imports System.IO.Ports
Public Class Form1
    Public Sub New()
        InitializeComponent()

        'Load com port data to comobox Object
        Dim ports As String() = SerialPort.GetPortNames()
        Array.Sort(ports)

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

        'Set default com port number is '1'
        cmbComPort.SelectedIndex = 0
    End Sub

    Private hPort As IntPtr
    Private Sub btnOpenCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCom.Click
        'Open port
        hPort = PACNET.UART.Open((cmbComPort.Text + ",") + txtBaudRate.Text)
        If hPort = CType(-1, IntPtr) Then
            MessageBox.Show("open com error,error code " & PACNET.ErrHandling.GetLastError())
        Else
            btnCloseCom.Enabled = True
            btnOpenCom.Enabled = False
        End If
    End Sub

    Private Sub btnCloseCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseCom.Click
        'Close port
        PACNET.UART.Close(hPort)
        btnCloseCom.Enabled = False
        btnOpenCom.Enabled = True
    End Sub

    Private Sub btnWriteData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteData.Click
        Dim iRet As Boolean

        'Write AO data
        iRet = PACNET.PAC_IO.WriteAO(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)), Convert.ToInt32(txtWriteChannel.Text), Convert.ToInt32(txtChannel.Text), Convert.ToSingle(txtWriteData.Text))

        If iRet = False Then
            MessageBox.Show("Write data error")
        End If
    End Sub
End Class
