Imports System.IO.Ports
Public Class Form1
    Dim hPort As IntPtr
    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
                cmbPort.Items.Add(port)
            End If
        Next

        cmbPort.SelectedIndex = 0
        btnClose.Enabled = False
    End Sub

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

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        PACNET.UART.Close(hPort)
        btnOpen.Enabled = True
        btnClose.Enabled = False
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        Dim DIValue As UInteger = 0
        Dim DOValue As UInteger = 0
        Dim DIChannel As Integer = txtDIChannel.Text
        Dim DOChannel As Integer = txtDOChannel.Text
        Dim address As Integer = txtAddress.Text
        Dim iret As Boolean = PACNET.PAC_IO.ReadDIO(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DIChannel, DOChannel, DIValue, DOValue)

        If Not iret Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() & vbLf & "Error Code: 0x" & ec.ToString("X"))
        Else
            txtDIValue.Text = DIValue.ToString("X")
        End If
    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim address As Integer = txtAddress.Text
        Dim DOChannel As Integer = txtDOChannel.Text
        Dim value As UInteger = Convert.ToUInt32(txtDOValue.Text, 16)
        Dim iret As Boolean = PACNET.PAC_IO.WriteDO(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DOChannel, value)
        If (iret = False) Then
            MessageBox.Show("Write data error")
        End If
    End Sub

    Private Sub btnRead_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead_MF.Click
        Dim DIValue As UInteger = 0
        Dim DOValue As UInteger = 0
        Dim DIChannel As Integer = txtDIChannel.Text
        Dim DOChannel As Integer = txtDOChannel.Text
        Dim address As Integer = txtAddress.Text
        Dim iret As Boolean = PACNET.PAC_IO.ReadDIO_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DIChannel, DOChannel, DIValue, DOValue)
        If Not iret Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() & vbLf & "Error Code: 0x" & ec.ToString("X"))
        Else
            txtDIValue.Text = DIValue.ToString("X")
        End If
    End Sub

    Private Sub btnWrite_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite_MF.Click
        Dim address As Integer = txtAddress.Text
        Dim DOChannel As Integer = txtDOChannel.Text
        Dim value As UInteger = Convert.ToUInt32(txtDOValue.Text, 16)
        Dim iret As Boolean = PACNET.PAC_IO.WriteDO_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DOChannel, value)
        If Not iret Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() & vbLf & "Error Code: 0x" & ec.ToString("X"))
        End If
    End Sub
End Class
