Public Class Form1
    Private hPort As IntPtr
    Private Shared pwmStartBit As Byte() = New Byte(7) {}
    Private Shared Byactivated As Boolean = False
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim connStr As String = "COM" + txtPort.Text + ":," + txtBaudrate.Text
        'Step1:Open com port
        hPort = PACNET.UART.Open(connStr)
        If hPort = CType(-1, IntPtr) Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() + vbLf & "Error Code: 0x" + ec.ToString("X"))
            'this.Text = pacPWM.LibVersion().ToString();
        Else
        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)
    End Sub

    Private Sub btnSavePWM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePWM.Click
        Dim ret As Boolean = PACNET.PWM.SavePWMConfig(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)))
        If Not ret 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 ConfigPWMSettings(ByVal sender As Object, ByVal e As EventArgs) Handles btnPWM7.Click, btnPWM6.Click, btnPWM5.Click, btnPWM4.Click, btnPWM3.Click, btnPWM2.Click, btnPWM1.Click, btnPWM0.Click
        Dim hzArr As TextBox() = New TextBox() {txtHz0, txtHz1, txtHz2, txtHz3, txtHz4, txtHz5, _
         txtHz6, txtHz7}
        Dim dutyArr As TextBox() = New TextBox() {txtDuty0, txtDuty1, txtDuty2, txtDuty3, txtDuty4, txtDuty5, _
         txtDuty6, txtDuty7}
        Dim modeArr As ComboBox() = New ComboBox() {cboMode0, cboMode1, cboMode2, cboMode3, cboMode4, cboMode5, _
         cboMode6, cboMode7}
        Dim cntArr As TextBox() = New TextBox() {txtCnt0, txtCnt1, txtCnt2, txtCnt3, txtCnt4, txtCnt5, _
         txtCnt6, txtCnt7}

        Dim chIndex As Int16 = 0
        chIndex = Int16.Parse(DirectCast(sender, Button).Tag.ToString())
        Dim ret As Boolean = False

        ret = PACNET.PWM.SetPWMFrequency(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, Convert.ToUInt32(hzArr(chIndex).Text))

        If Not ret Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() + vbLf & "Error Code: 0x" + ec.ToString("X"))
        Else
            ret = PACNET.PWM.SetPWMDuty(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, Convert.ToSingle(dutyArr(chIndex).Text))
            ret = PACNET.PWM.SetPWMPulseCount(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, Convert.ToInt32(cntArr(chIndex).Text))

            ret = PACNET.PWM.SetPWMMode(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, CType(modeArr(chIndex).SelectedIndex, Int16))

            GetPWMConfig(chIndex)
        End If
    End Sub

    Private Sub GetPWMConfig(ByVal chIndex As Int16)
        Dim hzArr As TextBox() = New TextBox() {txtHz0, txtHz1, txtHz2, txtHz3, txtHz4, txtHz5, _
         txtHz6, txtHz7}
        Dim dutyArr As TextBox() = New TextBox() {txtDuty0, txtDuty1, txtDuty2, txtDuty3, txtDuty4, txtDuty5, _
         txtDuty6, txtDuty7}
        Dim modeArr As ComboBox() = New ComboBox() {cboMode0, cboMode1, cboMode2, cboMode3, cboMode4, cboMode5, _
         cboMode6, cboMode7}
        Dim cntArr As TextBox() = New TextBox() {txtCnt0, txtCnt1, txtCnt2, txtCnt3, txtCnt4, txtCnt5, _
         txtCnt6, txtCnt7}
        Dim freq As UInt32 = 0

        Dim ret As Boolean = False

        ret = PACNET.PWM.GetPWMFrequency(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, freq)
        If Not ret Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() + vbLf & "Error Code: 0x" + ec.ToString("X"))
        Else
            hzArr(chIndex).Text = freq.ToString()
            Dim duty As Single = 0
            ret = PACNET.PWM.GetPWMDuty(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, duty)
            dutyArr(chIndex).Text = [String].Format("{0:00.0}", duty)
            Dim cnt As Int32 = 0
            ret = PACNET.PWM.GetPWMPulseCount(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, cnt)
            cntArr(chIndex).Text = cnt.ToString()

            Dim mode As Integer = 0
            ret = PACNET.PWM.GetPWMMode(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, mode)
            modeArr(chIndex).SelectedIndex = mode
        End If
    End Sub
    Private Sub EnablePWM(ByVal sender As Object, ByVal e As EventArgs) Handles ckEnPWM7.CheckStateChanged, ckEnPWM6.CheckStateChanged, ckEnPWM5.CheckStateChanged, ckEnPWM4.CheckStateChanged, ckEnPWM3.CheckStateChanged, ckEnPWM2.CheckStateChanged, ckEnPWM1.CheckStateChanged, ckEnPWM0.CheckStateChanged
        If Byactivated Then
            Dim chIndex As Int16 = Int16.Parse(DirectCast(sender, CheckBox).Tag.ToString())
            Dim ret As Boolean = False
            Dim enBox As CheckBox = DirectCast(sender, CheckBox)
            Dim status As Int16 = 0

            If enBox.Checked Then
                pwmStartBit(chIndex) = 1
            Else
                pwmStartBit(chIndex) = 0
            End If

            Dim i As Integer = 0
            While i < 8
                status += CType(pwmStartBit(i) << i, Int16)
                System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
            End While
            ret = PACNET.PWM.SetPWMStart(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), status)
            If Not ret 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 If
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        ReadPWM_DIStatus()
    End Sub
    Private Sub ReadPWM_DIStatus()
        Dim lbDI As Label() = {lbDI0, lbDI1, lbDI2, lbDI3, lbDI4, lbDI5, _
         lbDI6, lbDI7}
        Dim lbPWM As Label() = {pwm0, pwm1, pwm2, pwm3, pwm4, pwm5, _
         pwm6, pwm7}
        Dim enPWM As CheckBox() = {ckEnPWM0, ckEnPWM1, ckEnPWM2, ckEnPWM3, ckEnPWM4, ckEnPWM5, _
         ckEnPWM6, ckEnPWM7}

        Dim pwmBit As Byte() = New Byte(7) {}
        Dim diBit As Byte() = New Byte(7) {}


        Dim ret As Boolean = False

        ret = PACNET.PWM.GetPWMDIOStatus(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), pwmBit, diBit)
        If Not ret Then
            Dim ec As UInteger = PACNET.ErrHandling.GetLastError()
            MessageBox.Show(CType(ec, PACNET.ErrCode).ToString() + vbLf & "Error Code: 0x" + ec.ToString("X"))
        Else
            Dim i As Integer = 0
            While i < 8
                If pwmBit(i) = 1 Then
                    lbPWM(i).Text = "ON"
                    lbPWM(i).BackColor = Color.Red
                    Byactivated = False
                    enPWM(i).Checked = True
                    Byactivated = True
                Else
                    lbPWM(i).Text = "OFF"
                    lbPWM(i).BackColor = Color.Gray
                    Byactivated = False
                    enPWM(i).Checked = False
                    Byactivated = True
                End If

                If diBit(i) = 1 Then
                    lbDI(i).Text = "ON"
                    lbDI(i).BackColor = Color.Green
                Else
                    lbDI(i).Text = "OFF"
                    lbDI(i).BackColor = Color.Gray
                End If
                System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
            End While
        End If
    End Sub

    Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Byactivated = True
    End Sub
End Class
