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

📄 mscomm.frm

📁 串口通信相关程序源代码
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1 
   Caption         =   "MSComm-VB"
   ClientHeight    =   5430
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7290
   LinkTopic       =   "Form1"
   ScaleHeight     =   5430
   ScaleWidth      =   7290
   StartUpPosition =   3  '窗口缺省
   Begin VB.CheckBox Check1 
      Caption         =   "二进制模式"
      Height          =   255
      Left            =   240
      TabIndex        =   11
      Top             =   2400
      Width           =   1335
   End
   Begin VB.Timer Timer1 
      Interval        =   300
      Left            =   5880
      Top             =   3480
   End
   Begin VB.CommandButton Command2 
      Caption         =   "发送"
      Enabled         =   0   'False
      Height          =   375
      Left            =   2760
      TabIndex        =   5
      Top             =   480
      Width           =   975
   End
   Begin VB.TextBox Text3 
      Height          =   375
      Left            =   240
      TabIndex        =   4
      Text            =   "9600,n,8,1"
      Top             =   1080
      Width           =   1455
   End
   Begin VB.TextBox Text2 
      Height          =   2175
      Left            =   240
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   3
      Top             =   3120
      Width           =   5175
   End
   Begin VB.TextBox Text1 
      Height          =   1815
      Left            =   2760
      MultiLine       =   -1  'True
      ScrollBars      =   3  'Both
      TabIndex        =   2
      Text            =   "MSComm.frx":0000
      Top             =   840
      Width           =   4335
   End
   Begin VB.CommandButton Command1 
      Caption         =   "运行串口"
      Height          =   375
      Left            =   240
      TabIndex        =   1
      Top             =   1560
      Width           =   1095
   End
   Begin VB.ComboBox Combo1 
      Height          =   300
      ItemData        =   "MSComm.frx":0019
      Left            =   240
      List            =   "MSComm.frx":0029
      TabIndex        =   0
      Text            =   "Com1"
      Top             =   360
      Width           =   1095
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   2040
      Top             =   2400
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
      RThreshold      =   1
   End
   Begin VB.Label Label5 
      Caption         =   "DCB设置"
      Height          =   255
      Left            =   240
      TabIndex        =   10
      Top             =   840
      Width           =   1215
   End
   Begin VB.Label Label4 
      Caption         =   "串口号"
      Height          =   255
      Left            =   240
      TabIndex        =   9
      Top             =   120
      Width           =   975
   End
   Begin VB.Label Label3 
      Caption         =   "字符数"
      Height          =   255
      Left            =   3840
      TabIndex        =   8
      Top             =   600
      Width           =   3255
   End
   Begin VB.Label Label2 
      Caption         =   "Label2"
      Height          =   255
      Left            =   1440
      TabIndex        =   7
      Top             =   360
      Width           =   1215
   End
   Begin VB.Label Label1 
      Caption         =   "接收"
      Height          =   255
      Left            =   240
      TabIndex        =   6
      Top             =   2760
      Width           =   3255
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim nn, mm, kk As Integer                               '定义计数器公用变量

Private Sub Combo1_Change()

End Sub

Private Sub Command1_Click()                         '运行串口按钮
If MSComm1.CommID <= 0 Then                          '判断串口是否在运行
   MSComm1.Settings = Text3.Text                     '设置串口参数
   MSComm1.CommPort = Str(Combo1.ListIndex + 1)      '指定串口号
   MSComm1.PortOpen = True                           '运行指定串口
Else
   MSComm1.PortOpen = False                          '终止串口
End If
   Label2.Caption = MSComm1.CommID                   '显示串口句柄
If MSComm1.CommID <= 0 Then                          '判断串口是否在运行
   Command1.Caption = "运行串口"                     '按钮上显示可以运行
   Command2.Enabled = False                          '发送按钮不允许使用
   Timer1.Enabled = False                            '不允许时钟
Else
   Command1.Caption = "终止串口"                     '按钮上显示可以终止
   Command2.Enabled = True                           '发送按钮允许使用
   Timer1.Enabled = True                             '允许时钟
End If
   
End Sub


Private Sub Form_Load()                                 '程序开始执行
   Timer1.Interval = 1000                               '设置读时钟
   Timer1.Enabled = False                               '不允许时钟
   MSComm1.Settings = "9600,n,8,1"
   MSComm1.RThreshold = 1                               '允许OnComm()事件
   MSComm1.CommPort = "com4"
   MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()                            'OnComm()事件
   Dim ss, ww As String                                 '定义临时变量
   Dim i As Integer
   
   Select Case MSComm1.CommEvent                        'Select Case 语句分解事件
                                                        '实际只使用了收到字符事件
     ' 错误事件
      Case comEventBreak                                ' 收到 Break。
      Case comEventCDTO                                 ' CD (RLSD) 超时。
      Case comEventCTSTO                                ' CTS Timeout。
      Case comEventDSRTO                                ' DSR Timeout。
      Case comEventFrame                                ' Framing Error
      Case comEventOverrun                              '数据丢失。
      Case comEventRxOver                               '接收缓冲区溢出。
      Case comEventRxParity                             ' Parity 错误。
      Case comEventTxFull                               '传输缓冲区已满。
      Case comEventDCB                                  '获取 DCB 时意外错误
    ' 状态事件
      Case comEvCD                                      ' CD 线状态变化。
      Case comEvCTS                                     ' CTS 线状态变化。
      Case comEvDSR                                     ' DSR 线状态变化。
      Case comEvRing                                    ' Ring Indicator 变化。
      Case comEvReceive                                 ' 收到字符
        If Check1.Value = 0 Then                  '根据Check1.Value设置读取模式
           MSComm1.InputMode = 0                  '字符读取模式
        Else
           MSComm1.InputMode = 1                  '二进读取制模式
        End If
           ss = MSComm1.Input                                '先放入临时变量
           nn = nn + Len(ss)                                 '计算字符长度
           kk = kk + LenB(ss)                                '计算字节长度
        Text2.Text = Text2.Text & ss                         '加入Text2中
           ww = ""                 ' 后面的这一部分主要用来显示字符串内码
           For i = 1 To LenB(ss)                      '将解析码合并到ww中
              ww = ww + Hex(AscB(MidB(ss, i, 1))) + " "
           Next
           Text2.Text = Text2.Text & ww               '将解析码加入Text2中
        Label1.Caption = "收到字符" & nn & "   "             '显示接收情况
        Label1.Caption = Label1.Caption & "字节" & kk        '显示接收情况
        
    '  字符事件
      Case comEvSend                          ' 传输缓冲区有 Sthreshold 个字符
      Case comEvEOF                           ' 输入数据流中发现 EOF 字符
   End Select
End Sub

Private Sub Timer1_Timer()                        '时钟事件
   ss = MSComm1.Input                             '先放入临时变量
   nn = nn + Len(ss)                              '计算字符长度
   kk = kk + LenB(ss)                             '计算字节长度
   
   If Len(ss) > 0 Then                            '判断是否收到字符
      Text2.Text = Text2.Text & ss                '加入Text2中
   End If
   Label1.Caption = "收到字符" & nn & "   "        '显示接收情况
   Label1.Caption = Label1.Caption & "字节" & kk   '显示接收情况

End Sub

⌨️ 快捷键说明

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