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

📄 form1.frm

📁 USB接口的上位机程序
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "PC端USB测试程序"
   ClientHeight    =   3645
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6390
   LinkTopic       =   "Form1"
   ScaleHeight     =   3645
   ScaleWidth      =   6390
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command2 
      Caption         =   "退出"
      Height          =   495
      Left            =   3480
      TabIndex        =   5
      Top             =   2640
      Width           =   1335
   End
   Begin VB.TextBox Text2 
      Height          =   1575
      Left            =   3480
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   3
      Top             =   840
      Width           =   2415
   End
   Begin VB.TextBox Text1 
      Height          =   1575
      Left            =   360
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Top             =   840
      Width           =   2415
   End
   Begin VB.CommandButton Command1 
      Caption         =   "发送"
      Height          =   495
      Left            =   360
      TabIndex        =   0
      Top             =   2640
      Width           =   1215
   End
   Begin VB.Label Label2 
      Caption         =   "接收数据窗口"
      Height          =   255
      Left            =   3600
      TabIndex        =   4
      Top             =   360
      Width           =   1455
   End
   Begin VB.Label Label1 
      Caption         =   "发送数据窗口"
      Height          =   255
      Left            =   480
      TabIndex        =   2
      Top             =   360
      Width           =   1455
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function ReadData Lib "EasyUSB.dll" (ByVal pipenum As Long, ByRef recbuffer As Byte, ByVal len1 As Long, ByVal waittime As Long) As Long
Private Declare Function WriteData Lib "EasyUSB.dll" (ByVal pipenum As Long, ByRef sendbuffer As Byte, ByVal len1 As Long, ByVal waittime As Long) As Long

Private Sub Command1_Click()
Dim str1, str2 As String
Dim sendbuf(15) As Byte
Dim recbuf(15) As Byte
Dim i, j, datalen, sendlen As Integer
Dim lenght As Long
Dim temp As Variant
str1 = Text1.Text
datalen = Len(Text1.Text)
Text2.Text = ""
    For i = 1 To datalen Step 16
        str2 = Mid(str1, i, 16)          '发送缓冲区
        If Len(str2) > 16 Then
            sendlen = 16                    '发送帧长度
        Else
            sendlen = Len(str2)
        End If
        For j = 1 To sendlen
            temp = Mid(str2, j, 1)
            sendbuf(j - 1) = Asc(temp)
        Next j
        lenght = WriteData(1, sendbuf(0), sendlen, 1000)
        '1为端点0的输入控制索引
        'sendbuf为发送缓冲区
        'sendlen为发送帧长度
        '1000表示等待1000毫秒
        If lenght = -1 Then                 '若成功则返回实际发送的长度,失败则返回-1
            MsgBox ("发送数据错误!")
            Exit Sub
        End If
        lenght = ReadData(0, recbuf(0), sendlen, 1000)
        If lenght = -1 Then                 '若成功则返回实际接收的长度,失败则返回-1
            MsgBox ("接收数据错误!")
            Exit Sub
        End If
        For j = 1 To sendlen
            Text2.Text = Text2.Text + Chr(recbuf(j - 1)) '把接收到的数据放到text2中
        Next j
    Next i
End Sub

Private Sub Command2_Click()
End
End Sub

⌨️ 快捷键说明

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