📄 串口数据传输.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "串口数据传输"
ClientHeight = 2865
ClientLeft = 60
ClientTop = 510
ClientWidth = 7545
LinkTopic = "Form1"
ScaleHeight = 2865
ScaleWidth = 7545
StartUpPosition = 3 '窗口缺省
Begin MSCommLib.MSComm MSComm1
Left = 6000
Top = 480
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
End
Begin VB.TextBox Text2
Height = 495
Left = 1920
TabIndex = 3
Top = 1200
Width = 3375
End
Begin VB.TextBox Text1
Height = 495
Left = 1920
TabIndex = 2
Top = 360
Width = 2415
End
Begin VB.CommandButton Command2
Caption = "关闭窗口"
Height = 495
Left = 4320
TabIndex = 1
Top = 2040
Width = 1455
End
Begin VB.CommandButton Command1
Caption = "发送"
Height = 495
Left = 1440
TabIndex = 0
Top = 2040
Width = 1335
End
Begin VB.Label Label2
Caption = "接收数据"
Height = 255
Left = 360
TabIndex = 5
Top = 1320
Width = 1215
End
Begin VB.Label Label1
Caption = "发送数据"
Height = 255
Left = 360
TabIndex = 4
Top = 480
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'本例中在发送数据栏中输入一个0-255之间的整数,并单击"发送"按钮,单片机将接收到这个数据并显示该数据,
'然后作除2处理,结果再经串口返回到PC机端.
Private Sub Command1_Click()
Dim Number As Integer '定义要发送的变量
Dim OutByte(0) As Byte '定义发送的字节数组
Number = Val(Text1.Text) '类型转换
OutByte(0) = CByte(Number) '转换为二进制
MSComm1.OutBuffer = 0 '清空发送缓冲区
MSComm1.Output = OutByte '发送数据
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False '关闭串口
Unload Form1 '卸载窗体
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1 '使用1号串口
MSComm1.Settings = "9600,N,8,1" '设置初始化参数
MSComm1.PortOpen = True '打开串口
End Sub
Private Sub MSComm1_OnComm()
Dim InData As Variant '定义变体变量
Dim Arr(0) As Byte '定义接收字节数组
Select Case MSComm1.Input
Case comEvReceive '触发接收事件
InData = MSComm1.Input '接收数据
Arr(0) = AscB(InData) '类型转换
Text2.Text = Arr(0) '显示接收数据
MSComm1.InBufferCount = 0 '清空接收缓冲区
End Select
End Sub
Private Sub Text1_Change()
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -