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

📄 frmmain.frm

📁 这是一本学习串口编程喝计算机监控的好书里面是用VB开发的源代码
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmMain 
   Caption         =   "串口数据收发测试"
   ClientHeight    =   3510
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   6075
   Icon            =   "frmMain.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   3510
   ScaleWidth      =   6075
   StartUpPosition =   2  'CenterScreen
   Begin VB.Frame Frame2 
      Caption         =   "结尾码"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1095
      Left            =   3120
      TabIndex        =   12
      Top             =   2280
      Width           =   2775
      Begin VB.OptionButton optEnd 
         Caption         =   "CRLF"
         Height          =   255
         Index           =   2
         Left            =   1800
         TabIndex        =   20
         Top             =   480
         Width           =   735
      End
      Begin VB.OptionButton optEnd 
         Caption         =   "CR"
         Height          =   255
         Index           =   1
         Left            =   1080
         TabIndex        =   19
         Top             =   480
         Width           =   615
      End
      Begin VB.OptionButton optEnd 
         Caption         =   "None"
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   18
         Top             =   480
         Value           =   -1  'True
         Width           =   735
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "校验码"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1095
      Left            =   240
      TabIndex        =   11
      Top             =   2280
      Width           =   2775
      Begin VB.OptionButton optBCC 
         Caption         =   "CheckSum"
         Height          =   255
         Index           =   4
         Left            =   1080
         TabIndex        =   17
         Top             =   720
         Width           =   1335
      End
      Begin VB.OptionButton optBCC 
         Caption         =   "CRC"
         Height          =   255
         Index           =   3
         Left            =   240
         TabIndex        =   16
         Top             =   720
         Width           =   855
      End
      Begin VB.OptionButton optBCC 
         Caption         =   "Xor"
         Height          =   255
         Index           =   1
         Left            =   1080
         TabIndex        =   15
         Top             =   240
         Width           =   735
      End
      Begin VB.OptionButton optBCC 
         Caption         =   "Add"
         Height          =   255
         Index           =   2
         Left            =   1800
         TabIndex        =   14
         Top             =   240
         Width           =   735
      End
      Begin VB.OptionButton optBCC 
         Caption         =   "None"
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   13
         Top             =   240
         Value           =   -1  'True
         Width           =   855
      End
   End
   Begin VB.Timer timer_COMM 
      Enabled         =   0   'False
      Interval        =   500
      Left            =   720
      Top             =   1320
   End
   Begin VB.ComboBox comType 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   360
      Left            =   960
      TabIndex        =   9
      Text            =   "Char"
      Top             =   1800
      Width           =   855
   End
   Begin VB.TextBox txtCOM 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   2280
      MaxLength       =   1
      TabIndex        =   7
      Text            =   "1"
      Top             =   1680
      Width           =   375
   End
   Begin VB.CommandButton cmdClose 
      Caption         =   "关闭"
      Enabled         =   0   'False
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   3720
      TabIndex        =   6
      Top             =   1800
      Width           =   855
   End
   Begin VB.CommandButton cmdOpen 
      Caption         =   "打开"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   2760
      TabIndex        =   5
      Top             =   1800
      Width           =   855
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   2160
      Top             =   120
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
      RThreshold      =   1
      InputMode       =   1
   End
   Begin VB.TextBox txtSend 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   960
      TabIndex        =   2
      Top             =   1080
      Width           =   4935
   End
   Begin VB.CommandButton cmdSend 
      Caption         =   "发送"
      Enabled         =   0   'False
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   5040
      TabIndex        =   1
      Top             =   1800
      Width           =   855
   End
   Begin VB.TextBox txtReceive 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   960
      TabIndex        =   0
      Top             =   360
      Width           =   4935
   End
   Begin VB.Label Label4 
      Caption         =   "类型"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   240
      TabIndex        =   10
      Top             =   1920
      Width           =   495
   End
   Begin VB.Label Label3 
      Caption         =   "COM"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   1920
      TabIndex        =   8
      Top             =   1920
      Width           =   375
   End
   Begin VB.Label Label2 
      Caption         =   "接收"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   240
      TabIndex        =   4
      Top             =   480
      Width           =   495
   End
   Begin VB.Label Label1 
      Caption         =   "发送"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   240
      TabIndex        =   3
      Top             =   1320
      Width           =   495
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim bStartComm As Boolean
Dim strInputAll As String
Dim strRecHexChars As String
Dim nDisplayMode As Integer
Dim nParity As Integer
Dim nEndMark As Integer

Private Sub cmdClose_Click()
    MSComm1.PortOpen = False
    cmdClose.Enabled = False
    cmdOpen.Enabled = True
    cmdSend.Enabled = False
End Sub

Private Sub cmdOpen_Click()
    MSComm1.CommPort = Val(txtCOM.Text)
    MSComm1.PortOpen = True
    cmdOpen.Enabled = False
    cmdClose.Enabled = True
    cmdSend.Enabled = True
End Sub

Private Sub cmdSend_Click()
    Dim strHexChars As String
    
    If nDisplayMode = 0 Then
        strHexChars = StringToHexChars(txtSend.Text)
        strHexChars = GetFullPackage(strHexChars, nParity, nEndMark)
        MSComm1.Output = HexCharsToString(strHexChars)
    Else
        strHexChars = GetFullPackage(txtSend.Text, nParity, nEndMark)
        MSComm1.Output = HexCharsToVariant(strHexChars)
    End If
End Sub

Private Sub comType_Click()
    If comType.Text = "Char" Then
        nDisplayMode = 0
    Else
        nDisplayMode = 1
    End If
End Sub

Private Sub Form_Load()
    comType.AddItem "Char"
    comType.AddItem "Hex"
End Sub

Private Sub MSComm1_OnComm()
    Dim vInBuffer As Variant
      
    Select Case MSComm1.CommEvent
        'Events
        Case comEvReceive
            vInBuffer = MSComm1.Input
            If bStartComm = False Then
                bStartComm = True
                strInputAll = ""
                strRecHexChars = ""
            End If
            strRecHexChars = strRecHexChars + VariantToHexChars(vInBuffer)
            strInputAll = HexCharsToString(strRecHexChars)
            If bStartComm = True Then
                timer_COMM.Enabled = False
                timer_COMM.Enabled = True
            End If
        Case comEvSend
        Case comEvCTS
        Case comEvDSR
        Case comEvCD
        Case comEvRing
                
        ' Errors
        Case comEventBreak
        Case comEventCDTO
        Case comEventCTSTO
        Case comEventDSRTO
        Case comEventFrame
        Case comEventOverrun
        Case comEventRxOver
        Case comEventRxParity
        Case comEventTxFull
        Case comEventDCB
    End Select
End Sub

Private Sub optBCC_Click(Index As Integer)
    nParity = Index
End Sub

Private Sub optEnd_Click(Index As Integer)
    nEndMark = Index
End Sub

Private Sub timer_COMM_Timer()
    timer_COMM.Enabled = False
    bStartComm = False
    If CheckPackage(strRecHexChars, nParity, nEndMark) = False Then
        MsgBox "数据包检查错误!", vbCritical + vbOKOnly, "警告"
    End If
    If nDisplayMode = 0 Then
        txtReceive.Text = strInputAll
    Else
        txtReceive.Text = strRecHexChars
    End If
End Sub

⌨️ 快捷键说明

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