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 iAddress As Integer
    Private iTotalCh As Integer
    Private lDO_Value As UInteger


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

        'Get slot  
        lDO_Value = Convert.ToUInt32(txtValue.Text, 16)
        'Get DO Value
        iTotalCh = Convert.ToInt32(txtChannels.Text)
        'Get Total Channels
    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 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 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
        GetTextValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteDO(hPort, iAddress, iTotalCh, lDO_Value)
        'Write DO 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

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

        Dim data As UInteger = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadDO(hPort, iAddress, 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 = "ReadDO(HEX): " + data.ToString("x")
    End Sub

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

        Dim iRet As Boolean = PACNET.PAC_IO.WriteDO(hPort, iAddress, iTotalCh, 0)

        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
        GetTextValue()

        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
        GetTextValue()

        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        
        GetTextValue()

        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
        GetTextValue()

        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
        GetTextValue()

        Dim lValue As Long = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadModulePowerOnValueDO(hPort, iAddress, iTotalCh, lValue)

        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 ValueDO(HEX): " + lValue.ToString("x")
    End Sub

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

        Dim iRet As Boolean = PACNET.PAC_IO.WriteModulePowerOnValueDO(hPort, iAddress, iTotalCh, lDO_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

    Private Sub btnReadSafe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSafe.Click
        GetTextValue()

        Dim lValue As Long = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadModuleSafeValueDO(hPort, iAddress, iTotalCh, lValue)

        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 ValueDO(HEX): " + lValue.ToString("x")
    End Sub

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

        Dim iRet As Boolean = PACNET.PAC_IO.WriteModuleSafeValueDO(hPort, iAddress, iTotalCh, lDO_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

    Private Sub btnWrite_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite_MF.Click
        GetTextValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteDO_MF(hPort, iAddress, iTotalCh, lDO_Value)
        'Write DO 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

    Private Sub btnClear_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear_MF.Click
        GetTextValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteDO_MF(hPort, iAddress, iTotalCh, 0)

        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 btnRead_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead_MF.Click
        GetTextValue()

        Dim data As UInteger = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadDO_MF(hPort, iAddress, 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 = "ReadDO(HEX): " + data.ToString("x")
    End Sub

    Private Sub btnReadPower_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadPower_MF.Click
        GetTextValue()

        Dim lValue As Long = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadModulePowerOnValueDO_MF(hPort, iAddress, iTotalCh, lValue)

        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 ValueDO(HEX): " + lValue.ToString("x")
    End Sub

    Private Sub btnWritePower_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWritePower_MF.Click
        GetTextValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteModulePowerOnValueDO_MF(hPort, iAddress, iTotalCh, lDO_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

    Private Sub btnReadSafe_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSafe_MF.Click
        GetTextValue()

        Dim lValue As Long = 0

        Dim iRet As Boolean = PACNET.PAC_IO.ReadModuleSafeValueDO_MF(hPort, iAddress, iTotalCh, lValue)

        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 ValueDO(HEX): " + lValue.ToString("x")
    End Sub

    Private Sub btnWriteSafe_MF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteSafe_MF.Click
        GetTextValue()

        Dim iRet As Boolean = PACNET.PAC_IO.WriteModuleSafeValueDO_MF(hPort, iAddress, iTotalCh, lDO_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
