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

📄 10.txt

📁 介绍VB里的各种控件的使用方法,窗口控制,图像编程以及OCX等内容,还提供了一个API集供参考.
💻 TXT
字号:
MsComm 控件的文字传输范例 

MsComm 文字传输


假如您对 MSComm 的基本属性还不了解, 建议请先至MSComm 基本介绍一文中详读

表单画面

' 表单部分
'-------------------------------------------------------------
Form.Name = "Form1"
Caption = "Form1"
CommandButton.Name = Command2
Caption = "停止测试"
CommandButton.Name = Command1
Caption = "开始测试"
TextBox.Name = Text1
MSComm.Name = MSComm1
DTREnable = -1'True
Label.Name = Label1
AutoSize = -1'True
Caption = "欲送出的资料 - 按下[Enter]送出"
Label.Name = Label3
BorderStyle = 1'单线固定
Label.Name = Label2
AutoSize = -1'True
Caption = "折返显示"

' 程式部分
'-------------------------------------------------------------
Option Explicit
' 设一个此表单的区域变数,来作终止读取通讯埠的指标
Dim bStop As Boolean
' 设一个区域变数,来作读取通讯埠的暂存区
Dim InString As String

Private Sub Command1_Click()
bStop = False
Text1.SetFocus
With MSComm1
' 设定通讯埠号,可依照您的需求更改
.CommPort = 2
' 设定传输速率等,可依照您的需求更改
.Settings = "14400,N,8,1"
' 将通讯埠打开
.PortOpen = True
End With
' 假如使用者未按下「停止测试」钮(bStop = False), 则继续读取
Do While Not bStop
If MSComm1.InBufferCount Then
' 通讯埠中假如有资料的话, 则读取进来
InString = InString & MSComm1.Input
' 如果资料中有 Chr(13) 和 Chr(10) 的话, 则显示出来
If InStr(InString, vbCrLf) Then
Label3.Caption = Label3.Caption & InString
InString = ""
' 暂时把系统资源让出来给其它的程式使用
DoEvents
End If
End If
DoEvents
Loop
End Sub

Private Sub Command2_Click()
' 按下「停止测试」钮时, 把 bStop 设为「真」(True) 
' 如此程式才会停止等待(或读取)通讯埠所收到的资料 
bStop = True
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
bStop = True
' 把通讯埠关闭, 才不会影响其它程式的使用通讯埠 
MSComm1.PortOpen = False
End
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
' 当按下 [Enter] 时, 把 Text1 文字框中的资料送至通讯埠
If KeyAscii = 13 Then
MSComm1.Output = Text1.Text & vbCrLf
Text1.Text = ""
KeyAscii = 0
End If
End Sub


 

⌨️ 快捷键说明

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