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

📄 mainform.frm

📁 上位机与下位机的USB通讯
💻 FRM
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "Comdlg32.ocx"
Begin VB.Form frmMain 
   Caption         =   "USB File Transfer"
   ClientHeight    =   3255
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   8160
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3255
   ScaleWidth      =   8160
   StartUpPosition =   3  'Windows Default
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   7320
      Top             =   1080
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.CommandButton cmdBrowseReceive 
      Caption         =   "Browse"
      Height          =   375
      Left            =   6600
      TabIndex        =   11
      Top             =   2520
      Width           =   1215
   End
   Begin VB.CommandButton cmdBrowseTransfer 
      Caption         =   "Browse"
      Height          =   375
      Left            =   6600
      TabIndex        =   10
      Top             =   1680
      Width           =   1215
   End
   Begin VB.CommandButton cmdReceive 
      Caption         =   "Receive Data"
      Height          =   375
      Left            =   360
      TabIndex        =   7
      Top             =   2520
      Width           =   1335
   End
   Begin VB.CommandButton cmdTransfer 
      Caption         =   "Transfer Data"
      Height          =   375
      Left            =   360
      TabIndex        =   6
      Top             =   1680
      Width           =   1335
   End
   Begin VB.TextBox txtReceive 
      Height          =   285
      Left            =   1920
      TabIndex        =   5
      Top             =   2520
      Width           =   4575
   End
   Begin VB.TextBox txtTransfer 
      Height          =   285
      Left            =   1920
      TabIndex        =   4
      Top             =   1680
      Width           =   4575
   End
   Begin VB.CommandButton cmdClose 
      Caption         =   "Close"
      Height          =   375
      Left            =   6480
      TabIndex        =   3
      Top             =   240
      Width           =   1335
   End
   Begin VB.CommandButton cmdUpdateDeviceList 
      Caption         =   "Update Device List"
      Height          =   375
      Left            =   3240
      TabIndex        =   2
      Top             =   720
      Width           =   1575
   End
   Begin VB.ComboBox cmbDevice 
      Height          =   315
      Left            =   720
      TabIndex        =   1
      Top             =   720
      Width           =   2295
   End
   Begin VB.Label Label3 
      Caption         =   "Receive File Name:"
      Height          =   255
      Left            =   1920
      TabIndex        =   9
      Top             =   2160
      Width           =   2295
   End
   Begin VB.Label Label2 
      Caption         =   "Transfer File Name:"
      Height          =   255
      Left            =   1920
      TabIndex        =   8
      Top             =   1320
      Width           =   2055
   End
   Begin VB.Label Label1 
      Caption         =   "Select Device:"
      Height          =   255
      Left            =   720
      TabIndex        =   0
      Top             =   360
      Width           =   1335
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub cmdBrowseReceive_Click()

    'select the file you want to receive into
    On Error GoTo ErrHandler
    
    CommonDialog1.ShowOpen
    txtReceive.Text = CommonDialog1.FileName
    Exit Sub
    
ErrHandler:
    Exit Sub
    
End Sub

Private Sub cmdBrowseTransfer_Click()

    'select the file you want to send
    On Error GoTo ErrHandler
    
    CommonDialog1.ShowOpen
    txtTransfer.Text = CommonDialog1.FileName
    Exit Sub
    
ErrHandler:
    Exit Sub
    
End Sub

Private Sub cmdClose_Click()
    
    Unload frmMain
    
End Sub

Private Sub cmdReceive_Click()

    Dim readTO As Long
    Dim writeTO As Long
        
    'make sure we have devices connected, then open
    'the selected device
    If cmbDevice.ListIndex >= 0 Then
        Status = SI_GetTimeouts(readTO, writeTO)
        Status = SI_SetTimeouts(1000, 1000)
        
        Status = SI_Open(cmbDevice.ListIndex, hUSBDevice)
    
        'if we can connect to the device, then receive the file
        If Status = SI_SUCCESS Then
            ReadFileData

            SI_Close (hUSBDevice)
            
            Status = SI_SetTimeouts(readTO, writeTO)

            hUSBDevice = INVALID_HANDLE_VALUE
        End If
    End If
    
End Sub

Private Sub cmdTransfer_Click()
    
    Dim readTO As Long
    Dim writeTO As Long
        
    'make sure we have devices connected, then open
    'the selected device
    If cmbDevice.ListIndex >= 0 Then
        Status = SI_Open(cmbDevice.ListIndex, hUSBDevice)

        'if we can connect to the device, then send it the file
        'then close the connection to the device
        If Status = SI_SUCCESS Then
            Status = SI_GetTimeouts(readTO, writeTO)
            Status = SI_SetTimeouts(1000, 1000)
        
            WriteFileData

            Status = SI_SetTimeouts(readTO, writeTO)

            SI_Close (hUSBDevice)

            hUSBDevice = INVALID_HANDLE_VALUE
        End If
    End If
    
End Sub

Private Sub cmdUpdateDeviceList_Click()

    'device number, device string, and temp var newdevstring
    Dim DevNum As Long
    Dim DevStr(SI_MAX_DEVICE_STRLEN) As Byte
    Dim NewDevStr As String
    Dim i As Integer
    
    'clear the combo box
    cmbDevice.Clear
    
    'determine how many devices are hooked up
    Status = SI_GetNumDevices(DevNum)
    
    'if we find a device, obtain the name of each device
    'and convert the string to a vb string to add to the
    'combo list
    If Status = SI_SUCCESS Then
        For i = 0 To (DevNum - 1)
            Status = SI_GetProductString(i, DevStr(0), SI_RETURN_SERIAL_NUMBER)
            NewDevStr = ConvertToVBString(DevStr)
            cmbDevice.AddItem NewDevStr, i
        Next
        
        cmbDevice.ListIndex = 0 'then set combo list to first item
    Else
        cmbDevice.ListIndex = -1 'otherwise set list to -1
    End If
    
    
End Sub


Private Sub Form_Load()

    'on open, update the device list
    cmdUpdateDeviceList_Click
    
End Sub

⌨️ 快捷键说明

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