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
                cmbCOMPort.Items.Add(port)
            End If
        Next

        cmbCOMPort.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(cmbCOMPort.Text + "," + cmbBaudRate.Text)
        btnOpen.Enabled = False
        btnClose.Enabled = True
        panel1.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
        panel1.Enabled = False
    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        GetTxtValue()

        'Write AO value
        Dim iRet As Boolean = PACNET.PAC_IO.WriteAO(hPort, iAddress, iCurrentCh, iTotalCh, fValue)

        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
    Private iAddress As Integer
    Private iCurrentCh As Integer
    Private iTotalCh As Integer
    Private fValue As Single


    Private Sub GetTxtValue()
        iAddress = PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text))

        'Get slot  
        iCurrentCh = Convert.ToInt32(txtCurrentCH.Text)
        iTotalCh = Convert.ToInt32(txtTotalCH.Text)
        fValue = Convert.ToSingle(txtValue.Text)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbCOMPort.SelectedIndex = 3
        cmbBaudRate.SelectedIndex = 1
        panel1.Enabled = False
        btnClose.Enabled = False
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        GetTxtValue()

        Dim data As Single = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadAO(hPort, iAddress, iCurrentCh, iTotalCh, data)

        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

        txtRead.Text = "Read AO: " + data.ToString()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        GetTxtValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteAO(hPort, iAddress, iCurrentCh, iTotalCh, 0.0F)

        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

        txtValue.Text = "0"
    End Sub

    Private Sub btnCfgWDT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCfgWDT.Click
        GetTxtValue()

        Dim WTDTimeout As Integer = 0

        WTDTimeout = Convert.ToInt32(txtbWTDTimeout.Text)

        Dim iRet As Boolean = PACNET.PAC_IO.SetModuleWDTConfig(hPort, iAddress, 1, WTDTimeout, 0)
        '87k module without WTDOverwrite
        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

    Private Sub btnGetWDT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetWDT.Click
        GetTxtValue()

        'pac_GetModuleWDTConfig
        Dim WTDOverwrite As Integer = 0
        Dim WTDTimeout As Integer = 0
        Dim ebStatus As Short = 0

        Dim iRet As Boolean = PACNET.PAC_IO.GetModuleWDTConfig(hPort, iAddress, ebStatus, WTDTimeout, WTDOverwrite)

        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

        txtbWTDTimeout.Text = WTDTimeout.ToString()
    End Sub

    Private Sub btnRestWDT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestWDT.Click
        GetTxtValue()

        Dim iRet As Boolean = PACNET.PAC_IO.ResetModuleWDT(hPort, iAddress)

        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

    Private Sub btnRefreshWDT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefreshWDT.Click
        GetTxtValue()

        Dim iRet As Boolean = PACNET.PAC_IO.RefreshModuleWDT(hPort, iAddress)

        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

    Private Sub btnReadPower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadPower.Click
        GetTxtValue()
        Dim data As Single = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadModulePowerOnValueAO(hPort, iAddress, iCurrentCh, iTotalCh, data)

        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

        txtRead.Text = "Read PowerOn ValueAO: " + data.ToString()
    End Sub

    Private Sub btnWritePower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWritePower.Click
        GetTxtValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteModulePowerOnValueAO(hPort, iAddress, iCurrentCh, iTotalCh, fValue)

        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

    Private Sub btnReadSafe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSafe.Click
        GetTxtValue()
        Dim data As Single = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadModuleSafeValueAO(hPort, iAddress, iCurrentCh, iTotalCh, data)

        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

        txtRead.Text = "Read Safe ValueAO: " + data.ToString()
    End Sub

    Private Sub btnWriteSafe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteSafe.Click
        GetTxtValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteModuleSafeValueAO(hPort, iAddress, iCurrentCh, iTotalCh, fValue)

        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
