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("COM" + 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 btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        Dim DIValue As UInteger = 0
        Dim address As Integer = txtAddress.Text
        Dim channel As Integer = txtChannel.Text
        Dim iret As Boolean = PACNET.PAC_IO.ReadDI(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), channel, DIValue)
        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
            txtRead.Text = DIValue.ToString("X")
        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 address As Integer = txtAddress.Text
        Dim channel As Integer = txtChannel.Text
        Dim iret As Boolean = PACNET.PAC_IO.ReadDI_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), channel, DIValue)
        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
            txtRead.Text = DIValue.ToString("X")
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Dim DIValue As UInteger = 0
        Dim address As Integer = txtAddress.Text
        Dim channel As Integer = txtChannel.Text
        Dim DIcurchannel As Integer = txtDICNTChannel.Text
        Dim iret As Boolean = PACNET.PAC_IO.ClearDICNT(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DIcurchannel, channel)
        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
            txtRead.Text = ""
        End If
    End Sub

    Private Sub btnReadCnt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadCnt.Click
        Dim DIValue As UInteger = 0
        Dim address As Integer = txtAddress.Text
        Dim channel As Integer = txtChannel.Text
        Dim DIcurchannel As Integer = txtDICNTChannel.Text
        Dim iret As Boolean = PACNET.PAC_IO.ReadDICNT(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DIcurchannel, channel, DIValue)
        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
            txtRead.Text = DIValue.ToString()
        End If
    End Sub

    Private Sub btnClear_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear_MF.Click
        Dim DIValue As UInteger = 0
        Dim address As Integer = txtAddress.Text
        Dim channel As Integer = txtChannel.Text
        Dim DIcurchannel As Integer = txtDICNTChannel.Text
        Dim iret As Boolean = PACNET.PAC_IO.ClearDICNT_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DIcurchannel, channel)
        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
            txtRead.Text = ""
        End If
    End Sub

    Private Sub btnReadCnt_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadCnt_MF.Click
        Dim DIValue As UInteger = 0
        Dim address As Integer = txtAddress.Text
        Dim channel As Integer = txtChannel.Text
        Dim DIcurchannel As Integer = txtDICNTChannel.Text
        Dim iret As Boolean = PACNET.PAC_IO.ReadDICNT_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(address), DIcurchannel, channel, DIValue)
        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
            txtRead.Text = DIValue.ToString()
        End If
    End Sub
End Class
