📄 通信测试_文本.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "RS232的文本数据通信(070120001 李忠良)"
ClientHeight = 7230
ClientLeft = 1065
ClientTop = 945
ClientWidth = 10035
LinkTopic = "Form1"
ScaleHeight = 7230
ScaleWidth = 10035
Begin MSCommLib.MSComm Comm1
Left = 360
Top = 240
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
InputLen = 1
RThreshold = 1
RTSEnable = -1 'True
End
Begin VB.Frame Frame1
Caption = "串口通信参数设置"
Height = 3015
Left = 720
TabIndex = 8
Top = 2400
Width = 4935
Begin VB.CommandButton Command4
Caption = "修改"
Height = 495
Left = 2760
TabIndex = 18
Top = 2040
Width = 1575
End
Begin VB.ComboBox Combo3
Enabled = 0 'False
Height = 300
Left = 2880
TabIndex = 14
Text = "N,无校验"
Top = 1320
Width = 1455
End
Begin VB.ComboBox Combo2
Enabled = 0 'False
Height = 300
Left = 2880
TabIndex = 13
Text = "1200"
Top = 840
Width = 1455
End
Begin VB.ComboBox Combo1
Enabled = 0 'False
Height = 300
Left = 2880
TabIndex = 12
Text = "1"
Top = 360
Width = 1455
End
Begin VB.TextBox Baud_Text
Height = 375
Left = 1080
TabIndex = 11
Text = "1200"
Top = 840
Width = 1095
End
Begin VB.TextBox Port_Text
Height = 375
Left = 1080
TabIndex = 10
Text = "1"
Top = 360
Width = 1095
End
Begin VB.TextBox Verify_Text
Height = 390
Left = 1080
TabIndex = 9
Text = "N,无校验"
Top = 1320
Width = 1095
End
Begin VB.Line Line1
X1 = 2520
X2 = 2520
Y1 = 240
Y2 = 2880
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "校验位"
ForeColor = &H00C00000&
Height = 180
Index = 2
Left = 240
TabIndex = 17
Top = 1440
Width = 540
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "波特率"
ForeColor = &H00C00000&
Height = 180
Index = 1
Left = 240
TabIndex = 16
Top = 840
Width = 540
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "串口"
ForeColor = &H00C00000&
Height = 180
Index = 3
Left = 240
TabIndex = 15
Top = 360
Width = 360
End
End
Begin VB.ListBox List1
Columns = 13
ForeColor = &H8000000D&
Height = 4380
Left = 5760
TabIndex = 6
Top = 1800
Width = 3855
End
Begin VB.TextBox txtSend
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 495
Left = 840
TabIndex = 3
Text = "3FF"
Top = 1440
Width = 2775
End
Begin VB.CommandButton Command1
Caption = "清空接收区"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 5880
TabIndex = 2
Top = 6240
Width = 1695
End
Begin VB.CommandButton CmdExit
Cancel = -1 'True
Caption = "结束"
BeginProperty Font
Name = "宋体"
Size = 21.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1095
Left = 1440
Style = 1 'Graphical
TabIndex = 1
Top = 5760
Width = 3135
End
Begin VB.CommandButton CmdSend
Caption = "传送数据"
BeginProperty Font
Name = "新宋体"
Size = 9.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 3840
Style = 1 'Graphical
TabIndex = 0
Top = 1320
Width = 1695
End
Begin VB.Label Label5
AutoSize = -1 'True
Caption = "文本格式通信"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 495
Left = 3960
TabIndex = 7
Top = 240
Width = 2970
End
Begin VB.Label Label4
AutoSize = -1 'True
Caption = "接收数据个数:"
BeginProperty Font
Name = "宋体"
Size = 15.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 315
Left = 5880
TabIndex = 5
Top = 1200
Width = 2055
End
Begin VB.Label Label3
AutoSize = -1 'True
Caption = "0"
BeginProperty Font
Name = "宋体"
Size = 15.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 315
Left = 8520
TabIndex = 4
Top = 1200
Width = 165
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Sj(300) As Integer
Dim Nn As Integer
Sub Port_init()
Dim Port_num As Integer
Dim Baud_rate As String
Dim Verify As String
Dim A As String
On Error GoTo Port_err
Nn = 0
If Comm1.PortOpen = True Then Comm1.PortOpen = False
Open "D:\VB_TEST\SET.TXT" For Input As #1
Input #1, Port_num
Port_Text.Text = Port_num
Input #1, Baud_rate
Baud_Text.Text = Baud_rate
Line Input #1, Verify
Verify_Text.Text = Verify
Comm1.CommPort = Port_num
A = Baud_rate & "," & Left(Verify, 1) + ",8,1"
Comm1.Settings = A
Close #1
If Comm1.PortOpen = False Then Comm1.PortOpen = True
Exit Sub
Port_err:
If Comm1.PortOpen = True Then Comm1.PortOpen = False
MsgBox ("选择的串中不存在!")
Command4.Caption = "确认"
Combo1.Enabled = True
Combo2.Enabled = True
Combo3.Enabled = True
End Sub
Sub Set_change()
Port_Text.Text = Combo1.Text
Baud_Text.Text = Combo2.Text
Verify_Text.Text = Combo3.Text
Open "D:\vb_test\set.txt" For Output As #1
Print #1, Port_Text.Text
Print #1, Baud_Text.Text
Print #1, Verify_Text.Text
Close #1
Call Port_init
End Sub
Sub Delay(Dt)
Dim T1, Xx
T1 = Timer
Do While Timer - T1 < Dt
Xx = DoEvents()
Loop
End Sub
Private Sub CmdExit_Click()
End
End Sub
Private Sub CmdSend_Click()
Dim N As Integer, I As Integer
Frame1.Enabled = False
N = Len(txtSend.Text)
For I = 1 To N
Comm1.Output = Mid(txtSend.Text, I, 1)
Call Delay(0.01)
Next I
Frame1.Enabled = True
End Sub
Private Sub Comm1_OnComm()
Dim Buf$
Select Case Comm1.CommEvent
Case comEvReceive ' 收到数据
Do While (Comm1.InBufferCount > 0)
Buf = Comm1.Input
Nn = Nn + 1
If Nn > 256 Then
Nn = 1
List1.Clear
End If
Label3.Caption = Nn
Sj(Nn) = Val(Buf)
List1.AddItem Buf
Loop
End Select
Form1.Refresh
End Sub
Private Sub Command1_Click()
Nn = 0
Label3 = Nn
List1.Clear
End Sub
Private Sub Command4_Click()
If Command4.Caption = "修改" Then
Command4.Caption = "确认"
Combo1.Enabled = True
Combo2.Enabled = True
Combo3.Enabled = True
Else
Command4.Caption = "修改"
Combo1.Enabled = False
Combo2.Enabled = False
Combo3.Enabled = False
Call Set_change
End If
End Sub
Private Sub Form_Load()
Call Port_init
End Sub
Private Sub List1_Click()
End Sub
Private Sub txtSend_Change()
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -