⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.vb

📁 上位机与下位机的USB通讯
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.chkP1B1.TabIndex = 6
        Me.chkP1B1.Text = " 1"
        '
        'chkP1B2
        '
        Me.chkP1B2.Appearance = System.Windows.Forms.Appearance.Button
        Me.chkP1B2.Location = New System.Drawing.Point(44, 32)
        Me.chkP1B2.Name = "chkP1B2"
        Me.chkP1B2.Size = New System.Drawing.Size(24, 24)
        Me.chkP1B2.TabIndex = 5
        Me.chkP1B2.Text = " 2"
        '
        'chkP1B3
        '
        Me.chkP1B3.Appearance = System.Windows.Forms.Appearance.Button
        Me.chkP1B3.Location = New System.Drawing.Point(20, 32)
        Me.chkP1B3.Name = "chkP1B3"
        Me.chkP1B3.Size = New System.Drawing.Size(24, 24)
        Me.chkP1B3.TabIndex = 4
        Me.chkP1B3.Text = " 3"
        '
        'cmdExit
        '
        Me.cmdExit.Location = New System.Drawing.Point(488, 8)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.TabIndex = 5
        Me.cmdExit.Text = "Exit"
        '
        'GroupBox5
        '
        Me.GroupBox5.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblAnalog1, Me.Label4, Me.pbAnalog1})
        Me.GroupBox5.Location = New System.Drawing.Point(320, 40)
        Me.GroupBox5.Name = "GroupBox5"
        Me.GroupBox5.Size = New System.Drawing.Size(248, 64)
        Me.GroupBox5.TabIndex = 6
        Me.GroupBox5.TabStop = False
        Me.GroupBox5.Text = "Analog 1"
        '
        'lblAnalog1
        '
        Me.lblAnalog1.Location = New System.Drawing.Point(96, 40)
        Me.lblAnalog1.Name = "lblAnalog1"
        Me.lblAnalog1.Size = New System.Drawing.Size(40, 16)
        Me.lblAnalog1.TabIndex = 3
        Me.lblAnalog1.Text = "0"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 40)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(80, 16)
        Me.Label4.TabIndex = 2
        Me.Label4.Text = "Potentiometer:"
        '
        'pbAnalog1
        '
        Me.pbAnalog1.Location = New System.Drawing.Point(16, 16)
        Me.pbAnalog1.Maximum = 255
        Me.pbAnalog1.Name = "pbAnalog1"
        Me.pbAnalog1.Size = New System.Drawing.Size(216, 16)
        Me.pbAnalog1.Step = 1
        Me.pbAnalog1.TabIndex = 0
        '
        'GroupBox6
        '
        Me.GroupBox6.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblAnalog2, Me.Label3, Me.pbAnalog2})
        Me.GroupBox6.Location = New System.Drawing.Point(320, 112)
        Me.GroupBox6.Name = "GroupBox6"
        Me.GroupBox6.Size = New System.Drawing.Size(248, 64)
        Me.GroupBox6.TabIndex = 7
        Me.GroupBox6.TabStop = False
        Me.GroupBox6.Text = "Analog 2"
        '
        'lblAnalog2
        '
        Me.lblAnalog2.Location = New System.Drawing.Point(96, 40)
        Me.lblAnalog2.Name = "lblAnalog2"
        Me.lblAnalog2.Size = New System.Drawing.Size(40, 16)
        Me.lblAnalog2.TabIndex = 4
        Me.lblAnalog2.Text = "0"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 40)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 16)
        Me.Label3.TabIndex = 1
        Me.Label3.Text = "Temperature:"
        '
        'pbAnalog2
        '
        Me.pbAnalog2.Location = New System.Drawing.Point(16, 16)
        Me.pbAnalog2.Maximum = 255
        Me.pbAnalog2.Name = "pbAnalog2"
        Me.pbAnalog2.Size = New System.Drawing.Size(216, 16)
        Me.pbAnalog2.Step = 1
        Me.pbAnalog2.TabIndex = 0
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 50
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(584, 189)
        Me.ControlBox = False
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox6, Me.GroupBox5, Me.cmdExit, Me.GroupBox4, Me.GroupBox3, Me.GroupBox2, Me.GroupBox1})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmMain"
        Me.Text = "USB Test"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.GroupBox5.ResumeLayout(False)
        Me.GroupBox6.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        'on each timer tick we will send out the data from the
        'program to the board and set the appropriate values to
        'the board then, we will read in the data from the board
        'and set the appropriate values to the program

        'for the buffer, VBIOBuf is for visual basic only, the IntPtr IOBuf
        'is used to copy the VBIOBuf into something readable by c++ unmanaged code
        'Dim IOBufSize As Int32 = 12
        'Dim VBIOBuf(IOBufSize) As Byte
        'Dim IOBuf As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(IOBufSize) * VBIOBuf.Length())
        Dim IOBufSize As Integer = 12
        Dim IOBuf(IOBufSize) As Byte

        'io buffer; bits are defined as follows:
        'VBIOBuf(0) = LED1
        'VBIOBuf(1) = LED2
        'VBIOBuf(2) = Port
        'VBIOBuf(3) = Analog1
        'VBIOBuf(4) = Analog2
        'VBIOBuf(5,6,7) = Unused
        'VBIOBuf(8,9,10,11) = Number Of Interrupts

        'set the first two bytes of the array to the led values
        If chkLED1.Checked() Then
            IOBuf(0) = 1
        Else
            IOBuf(0) = 0
        End If

        If chkLED2.Checked() Then
            IOBuf(1) = 1
        Else
            IOBuf(1) = 0
        End If

        Dim B0, B1, B2, B3
        Dim P1

        'bits for each of the bits represented in port 1
        B0 = chkP1B0.Checked
        B1 = chkP1B1.Checked * 2   'shift left 1
        B2 = chkP1B2.Checked * 4   'shift left 2
        B3 = chkP1B3.Checked * 8   'shift left 3
        P1 = (B0 + B1 + B2 + B3) 'or all of the bits together to get the byte value
        P1 = P1 And &HF          'and then with &h0F to get the low 4 bits
        IOBuf(2) = P1            'set the third element to this value to send out

        Dim BytesSucceed As Integer
        Dim BytesWriteRequest As Integer
        Dim BytesReadRequest As Integer

        BytesSucceed = 0
        BytesWriteRequest = IOBufSize - 4
        BytesReadRequest = IOBufSize - 4

        'send data to the board, use the marshal copy to put it into
        'a buffer that is readable by c++ unmanaged code
        'Marshal.Copy(IOBuf, 0, IOBuf, IOBufSize)
        Status = SI_Write(hUSBDevice, IOBuf(0), BytesWriteRequest, BytesSucceed, 0)

        If (BytesSucceed <> BytesWriteRequest) Or (Status <> SI_SUCCESS) Then
            MsgBox("Error writing to USB. Wrote " + Str(BytesSucceed) + " of " + Str(BytesWriteRequest) + " bytes. Application is aborting. Reset hardware and try again.")
            End
        End If

        'clear out the iobuf and bytessucceed for the next read
        BytesSucceed = 0

        'read data from the board
        Status = SI_Read(hUSBDevice, IOBuf(0), BytesReadRequest, BytesSucceed, 0)

        If (BytesSucceed <> BytesReadRequest) Or (Status <> SI_SUCCESS) Then
            MsgBox("Error writing to USB. Read " + Str(BytesSucceed) + " of " + Str(BytesReadRequest) + " bytes. Application is aborting. Reset hardware and try again.")
            End
        End If

        'take the newly received array and put it back into
        'vb form
        'Marshal.Copy(IOBuf, VBIOBuf, 0, IOBufSize)
        'Marshal.FreeCoTaskMem(IOBuf)

        'the first two elements have the button status
        chkButton1.Checked() = IOBuf(0)
        chkButton2.Checked() = IOBuf(1)

        Dim P0
        P0 = IOBuf(2) And &HF            'and the value with &h0F to get the low 4 bits
        chkP0B0.Checked = P0 And &H1       'check first bit by anding with 1, and if it is true, set the check box
        chkP0B1.Checked = (P0 \ 2) And &H1 'shift right 1, and check if its true, set the check box
        chkP0B2.Checked = (P0 \ 4) And &H1 'shift right 2, and check if its true, set the check box
        chkP0B3.Checked = (P0 \ 8) And &H1 'shift right 3, and check if its true, set the check box

        lblAnalog1.Text = IOBuf(3) 'set each analog label to the corresponding value
        lblAnalog2.Text = IOBuf(4)
        pbAnalog1.Value = IOBuf(3)    'set each analog graphic as well
        pbAnalog2.Value = IOBuf(4)

    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

        'close use device and exit program
        Status = SI_Close(hUSBDevice)
        End

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -