📄 frmmain.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmMain
Caption = "串口数据收发测试"
ClientHeight = 2385
ClientLeft = 60
ClientTop = 450
ClientWidth = 6000
Icon = "frmMain.frx":0000
LinkTopic = "Form1"
ScaleHeight = 2385
ScaleWidth = 6000
StartUpPosition = 2 'CenterScreen
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
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()
If nDisplayMode = 0 Then
MSComm1.Output = txtSend.Text
Else
MSComm1.Output = HexCharsToVariant(txtSend.Text)
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 timer_COMM_Timer()
timer_COMM.Enabled = False
bStartComm = False
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 + -