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

📄 form1.frm

📁 利用MSCOMM串口通信控件进行异步串行通信的文件传输程序设计
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   5430
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7785
   LinkTopic       =   "Form1"
   ScaleHeight     =   5430
   ScaleWidth      =   7785
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   6000
      TabIndex        =   12
      Top             =   480
      Width           =   1095
   End
   Begin VB.CommandButton Command5 
      Caption         =   "串口设置"
      Height          =   375
      Left            =   3720
      TabIndex        =   10
      Top             =   480
      Width           =   1095
   End
   Begin VB.CommandButton Command2 
      Caption         =   "关闭串口"
      Height          =   375
      Left            =   2280
      TabIndex        =   9
      Top             =   480
      Width           =   1095
   End
   Begin VB.CommandButton Command1 
      Caption         =   "打开串口"
      Height          =   375
      Left            =   720
      TabIndex        =   8
      Top             =   480
      Width           =   975
   End
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   2280
      Top             =   4320
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   840
      Top             =   4320
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
   Begin VB.Frame Frame1 
      Caption         =   "发送信息"
      Height          =   2535
      Left            =   720
      TabIndex        =   0
      Top             =   1560
      Width           =   5535
      Begin VB.CommandButton xSend 
         Caption         =   "传送"
         Height          =   375
         Left            =   480
         TabIndex        =   7
         Top             =   1920
         Width           =   975
      End
      Begin VB.CommandButton save 
         Caption         =   "浏览"
         Height          =   255
         Left            =   4080
         TabIndex        =   6
         Top             =   1080
         Width           =   855
      End
      Begin VB.CommandButton open 
         Caption         =   "浏览"
         Height          =   255
         Left            =   4080
         TabIndex        =   5
         Top             =   480
         Width           =   855
      End
      Begin VB.TextBox txtReceive1 
         Height          =   375
         Left            =   1560
         TabIndex        =   4
         Top             =   1080
         Width           =   2295
      End
      Begin VB.TextBox txtSend1 
         Height          =   375
         Left            =   1560
         TabIndex        =   3
         Top             =   480
         Width           =   2295
      End
      Begin VB.OptionButton Option2 
         Caption         =   "接收文件"
         Height          =   255
         Left            =   360
         TabIndex        =   2
         Top             =   1080
         Width           =   1095
      End
      Begin VB.OptionButton Option1 
         Caption         =   "发送文件"
         Height          =   255
         Left            =   360
         TabIndex        =   1
         Top             =   480
         Width           =   1335
      End
   End
   Begin VB.Label Label2 
      Height          =   255
      Left            =   840
      TabIndex        =   13
      Top             =   1080
      Width           =   1575
   End
   Begin VB.Label Label1 
      Caption         =   "IP"
      Height          =   375
      Left            =   5640
      TabIndex        =   11
      Top             =   600
      Width           =   255
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
  If MSComm1.PortOpen = True Then
       MSComm1.PortOpen = False
  End If
 MSComm1.Settings = "9600,N,8,1"
 MSComm1.RThreshold = 1
 MSComm1.PortOpen = True
  Call ReceiveDisplay("串口已打开", 3)
 
  
End Sub

Private Sub Command2_Click()
     Call CommPortClose
     intCommFlag = 0
End Sub

Private Sub Command5_Click()
  Form2.Show
  
End Sub

Private Sub Form_Load()
If MSComm1.PortOpen = False Then
      MSComm1.PortOpen = True
End If
 ' abc=0
  blnShowFlag = True
  Form1.Height = 7000
  Call SetComm("9600,N,8,1", 2, 4096)  '串口设置
  intinbuffersize = 4096
  intoutbuffersize = 2048
  
  intCommFlag = 1 '初始化系统
  Form1.CommonDialog1.Flags = &H200000 Or &H2
  Form1.CommonDialog1.CancelError = True
  blnFileTransFlag = False  ' 初始化发送接收标志
  intArrayCount = 0
  
  

End Sub

Private Sub MSComm1_OnComm()
        Select Case Form1.MSComm1.CommEvent
           Case comEvReceive
               If intCommFlag = 1 Then
                   Call InPutManager
                   intCommFlag = 1
                End If
        End Select
        
End Sub

Private Sub open_Click()
   On Error GoTo Cancel
   Call Option1_Click
   Form1.CommonDialog1.ShowOpen
   strFileName = Form1.CommonDialog1.FileTitle
   strFileDirectary = Form1.CommonDialog1.FileName
   txtSend1.Text = strFileDirectary
Cancel:
   
End Sub

Private Sub Option1_Click()
      Form1.Option1.Value = True
      blnFileTransFlag = True
      

End Sub

Private Sub Option2_Click()
      Form1.Option2.Value = True
      blnFileTransFlag = False
      
      
End Sub

Public Sub save_Click()
    On Error GoTo Cancel
    Call Option2_Click
    
    Form1.CommonDialog1.ShowSave
    strFileDirectary = Form1.CommonDialog1.FileName
    
    strFileName = Form1.CommonDialog1.FileTitle
    txtReceive1.Text = strFileDirectary
Cancel:
    
End Sub

Private Sub xSend_Click()
   On Error GoTo FileError
     If blnFileTransFlag = True And intCommFlag = 1 Then
      intCommFlag = 0
        Call FileSendManager(0)
      intCommFlag = 1
    End If
FileError:
    Select Case Err.Number
       Case 53
           MsgBox ("文件没有找到")
      End Select
    If intCommFlag > 0 Then
        intCommFlag = 1
    Else
        MsgBox ("串口没打开")
    End If
    
End Sub
'获得串口设置
Public Function GetSettings() As String
    'GetSettings = strcommsettings
  GetSettings = strcommsettings
    
        
End Function
'设置串口参数
Public Function SetComm(strset As String, intport As Long, intoutBuffer As Long)
   strcommsettings = strset
   intcommport = intport
   intoutbuffersize = intoutBuffer
End Function

'获取当前的串口号
Public Function GetCommPort() As Long
    'GetCommPort = incommport
    GetCommPort = incommport
    
End Function

'获取当前发送缓冲区大小
Public Function GetOutBuffer() As Long
' GetOutBuffer = intoutbuffersize
 GetOutBuffer = intoutbuffersize
 
 
End Function
 
 '打开串口
 Public Sub CommPortOpen()
 On Error GoTo PortError
   '设置串口行口号
   MSComm1.CommPort = intcommport
 
   
   If MSComm1.PortOpen = True Then
       MSComm1.PortOpen = False
  End If
  
  '设置波特率,奇偶校验位,数据位和停止位
  MSComm1.Settings = strcommsettings
  ' 设置接收缓冲区字节长度
  MSComm1.InBufferSize = intinbuffersize
  
  
  '清除缓冲区数据
  MSComm1.InBufferCount = 0
  '设置发送缓冲区字节长度
  MSComm1.OutBufferSize = intoutbuffersize
  '清除发送缓冲区数据
  MSComm1.OutBufferCount = 0
  '每次接收到字符即产生onComm 事件
  MSComm1.RThreshold = 1
  MSComm1.Handshaking = comRTSXOnXOff
   Form1.MSComm1.InputLen = 100
   MSComm1.PortOpen = True
PortError:
   Select Case Err.Number
          Case 8005
             MsgBox ("该串口已经被占用,请换其他串口")
    End Select
    
   
 End Sub
 
 '关闭串口
 Public Sub CommPortClose()
       Dim strTemp As String
       
       If MSComm1.PortOpen = True Then
          MSComm1.PortOpen = False
          strTemp = "设置:关闭串行口"
          Call ReceiveDisplay(strTemp, 3)
      Else
          strTemp = "设置:串行口已关闭!"
          Call ReceiveDisplay(strTemp, 3)
     End If
     
 End Sub
 
 Public Sub ReceiveDisplay(strTemp As String, intNum As Long)
 
      Form1.Label2.Caption = strTemp
      
 
 End Sub

⌨️ 快捷键说明

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