
Imports System.IO.Ports
Public Class Form1
    Private hPort As IntPtr
    Public Sub New()
        InitializeComponent()
        'Load com port data to comobox Object
        LoadComPort()
        btnClose.Enabled = False
    End Sub

    Private Sub LoadComPort()
        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 btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        hPort = PACNET.UART.Open(cmbPort.Text + "," + txtBuadRate.Text)
        btnOpen.Enabled = False
        btnClose.Enabled = True
    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 btnReadCnt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadCnt.Click
        Dim address As Integer
        Dim IDValue As UInteger = 0
        Dim iRet As Boolean

        'Get slot data
        address = Convert.ToInt16(txtAddress.Text)

        'Read data
        iRet = PACNET.PAC_IO.ReadCNT(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), Convert.ToInt16(txtChannel.Text), IDValue)

        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 = Convert.ToString(IDValue)
        End If
    End Sub

    Private Sub btnClearCnt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearCnt.Click
        Dim address As Integer
        Dim iRet As Boolean

        'Get slot data
        address = Convert.ToInt16(txtAddress.Text)

        'Clear data
        iRet = PACNET.PAC_IO.ClearCNT(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), Convert.ToInt16(txtChannel.Text))

        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
            MessageBox.Show("Clear Success")
        End If
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        Dim address As Integer
        Dim IDValue As Integer = 0
        Dim iRet As Boolean

        'Get slot data
        address = Convert.ToInt16(txtAddress.Text)

        'Read data
        iRet = PACNET.PAC_IO.ReadCNTOverflow(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), Convert.ToInt16(txtChannel.Text), IDValue)

        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 = Convert.ToString(IDValue)
        End If

        PACNET.UART.Close(hPort)
    End Sub
End Class
