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

📄 form1.frm

📁 串口通信范例1Visual Basic源码
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4770
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7185
   LinkTopic       =   "Form1"
   ScaleHeight     =   4770
   ScaleWidth      =   7185
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command3 
      Caption         =   "确定修改波特率"
      Height          =   1095
      Left            =   2760
      TabIndex        =   8
      Top             =   3600
      Width           =   1335
   End
   Begin VB.TextBox Text3 
      Height          =   615
      Left            =   1320
      TabIndex        =   6
      Text            =   "Text3"
      Top             =   3720
      Width           =   1215
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   1200
      Top             =   0
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
   Begin VB.CommandButton Command2 
      Caption         =   "退出"
      Height          =   1095
      Left            =   5640
      TabIndex        =   3
      Top             =   3600
      Width           =   1455
   End
   Begin VB.CommandButton Command1 
      Caption         =   "发送"
      Height          =   1095
      Left            =   4080
      TabIndex        =   2
      Top             =   3600
      Width           =   1575
   End
   Begin VB.TextBox Text2 
      Height          =   1215
      Left            =   2280
      TabIndex        =   1
      Text            =   "Text2"
      Top             =   1800
      Width           =   4815
   End
   Begin VB.TextBox Text1 
      Height          =   1215
      Left            =   2280
      TabIndex        =   0
      Text            =   "Text1"
      Top             =   480
      Width           =   4815
   End
   Begin VB.Label Label8 
      Caption         =   "端口号"
      Height          =   255
      Left            =   2760
      TabIndex        =   13
      Top             =   3120
      Width           =   735
   End
   Begin VB.Label Label7 
      Caption         =   "波特率"
      Height          =   255
      Left            =   360
      TabIndex        =   12
      Top             =   3120
      Width           =   855
   End
   Begin VB.Label Label6 
      Height          =   255
      Left            =   4680
      TabIndex        =   11
      Top             =   3120
      Width           =   975
   End
   Begin VB.Label Label5 
      Height          =   255
      Left            =   3600
      TabIndex        =   10
      Top             =   3120
      Width           =   735
   End
   Begin VB.Label Label4 
      Height          =   255
      Left            =   1440
      TabIndex        =   9
      Top             =   3120
      Width           =   975
   End
   Begin VB.Label Label3 
      Caption         =   "修改后波特率"
      Height          =   615
      Left            =   360
      TabIndex        =   7
      Top             =   3720
      Width           =   975
   End
   Begin VB.Label Label2 
      Caption         =   "接收数据"
      Height          =   1095
      Left            =   360
      TabIndex        =   5
      Top             =   1800
      Width           =   1695
   End
   Begin VB.Label Label1 
      Caption         =   "发送数据"
      Height          =   975
      Left            =   360
      TabIndex        =   4
      Top             =   600
      Width           =   1695
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()

'...发送数据
If Form1.MSComm1.PortOpen = False Then Form1.MSComm1.PortOpen = True

MSComm1.OutBufferCount = 0 '清空发送缓冲区
'...MSComm1.InBufferCount = 0 '清空接收缓冲区

MSComm1.Output = Text1.Text '...发送数据

End Sub
Private Sub Command2_Click()

'...退出

Unload Me

End Sub

Private Sub Command3_Click()
If Form1.MSComm1.PortOpen = True Then Form1.MSComm1.PortOpen = False
Form1.MSComm1.Settings = Text3.Text
 Form1.Label4.Caption = Form1.MSComm1.Settings
 Form1.MSComm1.CommPort = GetSetting(App.Title, "Properties", "CommPort", 1)
 Form1.Label5.Caption = Form1.MSComm1.CommPort
 Form1.MSComm1.Handshaking = GetSetting(App.Title, "Properties", "Handshaking", 0)
 Form1.Label6.Caption = Form1.MSComm1.Handshaking

End Sub

Private Sub Form_Load()

'...初始化

MSComm1.CommPort = 1 '...使用Com1口

MSComm1.Settings = "9600,n,8,1" '...设置通讯参数
MSComm1.InBufferSize = 40
MSComm1.InputLen = 1
'MSComm1.PortOpen = True '...打开串口
'MSComm1.InputLen = 0 'Input将读取接收缓冲区的全部内容

'MSComm1.PortOpen = 0 '打开通信口

'MSComm1.InBufferCount = 0 '清除发送缓冲区数据

'MSComm1.OutBufferCount = 0 '清除接收缓冲区数据
MSComm1.RThreshold = 1  '设置接收一个字节就产生OnComm事件

Text3.Text = ""
Text2.Text = ""
Text1.Text = ""
End Sub

Private Sub MSComm1_OnComm()

Select Case MSComm1.CommEvent
Case comEvReceive '...接收字符数据
'Dim Buffer As Variant
MSComm1.InputLen = 0
'...当InputMode 属性值为0(文本模式)时,变量中含String型数据。
'...当InputMode属性值为1(二进制模式)时,变量中含Byte型数组数据。
MSComm1.InputMode = comInputModeBinary
Text2.Text = MSComm1.Input   '...接收二进制数据
'Text2 = Text2 + Buffer
MSComm1.InputMode = comInputModeText
Text2.Text = MSComm1.Input
'Text2 = Text2 + Buffer
MSComm1.InBufferCount = 0 '...清空输入寄存器
Case Else
End Select
End Sub

⌨️ 快捷键说明

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