Public Class Form1
    Dim devhandle As Short
    Dim btnin As Button() = New Button(31) {}
    Dim btnout As Button() = New Button(31) {}
    Dim isconn As Boolean = False
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rtn As Short = 0
        '创建控制器句柄
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_DevOpenByID("CARD1", devhandle)
        If rtn <> 0 Then
            Return
        End If
        '控制器已连接
        isconn = True
        Timer1.Start()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 26 - 1

            If i < 16 Then
                btnin(i) = New Button()
                btnin(i).Name = "btnI" + i.ToString()
                btnin(i).Size = New Size(26, 26)
                btnin(i).Location = New Point(20 + i * 30, 114)
                btnin(i).Enabled = False
                btnin(i).BackColor = Color.Transparent
                btnin(i).Text = i.ToString()
                Me.Controls.Add(btnin(i))
            Else
                btnin(i) = New Button()
                btnin(i).Name = "btnI" + i.ToString()
                btnin(i).Size = New Size(26, 26)
                btnin(i).Location = New Point(20 + (i - 16) * 30, 146)
                btnin(i).Enabled = False
                btnin(i).BackColor = Color.Transparent
                btnin(i).Text = i.ToString()
                Me.Controls.Add(btnin(i))
            End If
        Next
        For i As Integer = 0 To 16 - 1

            If i < 16 Then
                btnout(i) = New Button()
                btnout(i).Name = "btnO" + i.ToString()
                btnout(i).Size = New Size(26, 26)
                btnout(i).Location = New Point(20 + i * 30, 244)
                btnout(i).Enabled = True
                btnout(i).BackColor = Color.Transparent
                btnout(i).Text = i.ToString()
                Me.Controls.Add(btnout(i))
            Else
                btnout(i) = New Button()
                btnout(i).Name = "btnO" + i.ToString()
                btnout(i).Size = New Size(26, 26)
                btnout(i).Location = New Point(20 + (i - 16) * 30, 276)
                btnout(i).Enabled = True
                btnout(i).BackColor = Color.Transparent
                Me.Controls.Add(btnout(i))
            End If
            AddHandler btnout(i).Click, AddressOf Me.btClick
        Next
    End Sub
    Private Sub btClick(ByVal sender As Object, ByVal e As EventArgs)
        If isconn = False Then
            MessageBox.Show("先初始化控制器")
            Return
        End If
        Dim rtn As UShort
        Dim b As Button
        b = CType(sender, Button)
        If b.BackColor = Color.Green Then
            '置为高电平
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_SetDOBit(devhandle, (Convert.ToInt32(b.Name.Remove(0, 4))), 1)
        Else
            '置为低电平
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_SetDOBit(devhandle, (Convert.ToInt32(b.Name.Remove(0, 4))), 0)
        End If

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim rtn As UShort
        Dim io As Integer = 0
        Dim iou As Integer = 0
        '获取输入状态
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_GetDI(devhandle, 0, io)
        For i As Integer = 0 To 26 - 1
            If (io And (1 << i)) = 0 Then
                '低电平
                btnin(i).BackColor = Color.Green
            Else
                '高电平
                btnin(i).BackColor = Color.Transparent
            End If
        Next
        '获取输出状态
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_GetDO(devhandle, 0, iou)
        For i As Integer = 0 To 16 - 1
            If (iou And (1 << i)) = 0 Then
                '低电平
                btnout(i).BackColor = Color.Green
            Else
                '高电平
                btnout(i).BackColor = Color.Transparent
            End If
        Next
    End Sub
End Class
