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

📄 seport.frm

📁 串口通讯
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "mscomm32.ocx"
Begin VB.Form Form1 
   Caption         =   "串口通信"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7020
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   7020
   StartUpPosition =   3  '窗口缺省
   Begin VB.Timer Timer1 
      Left            =   9960
      Top             =   3360
   End
   Begin VB.ComboBox cos_bit 
      Height          =   300
      ItemData        =   "seport.frx":0000
      Left            =   1200
      List            =   "seport.frx":0002
      TabIndex        =   13
      Text            =   "1"
      Top             =   2050
      Width           =   1000
   End
   Begin VB.ComboBox cod_bit 
      Height          =   300
      ItemData        =   "seport.frx":0004
      Left            =   1200
      List            =   "seport.frx":0006
      TabIndex        =   12
      Text            =   "8"
      Top             =   1650
      Width           =   1000
   End
   Begin VB.ComboBox coc_bit 
      Height          =   300
      ItemData        =   "seport.frx":0008
      Left            =   1200
      List            =   "seport.frx":000A
      TabIndex        =   11
      Text            =   "N"
      Top             =   1250
      Width           =   1000
   End
   Begin VB.ComboBox cob_rate 
      Height          =   300
      ItemData        =   "seport.frx":000C
      Left            =   1200
      List            =   "seport.frx":000E
      TabIndex        =   10
      Text            =   "4800"
      Top             =   850
      Width           =   1000
   End
   Begin VB.ComboBox cos_port 
      Height          =   300
      ItemData        =   "seport.frx":0010
      Left            =   1200
      List            =   "seport.frx":0012
      TabIndex        =   9
      Text            =   "1"
      Top             =   450
      Width           =   1000
   End
   Begin VB.CommandButton bt_clear 
      Caption         =   "清空数据"
      Height          =   495
      Left            =   240
      TabIndex        =   3
      Top             =   4200
      Width           =   1215
   End
   Begin VB.CommandButton bt_rece 
      Caption         =   "开始接收"
      Height          =   495
      Left            =   240
      TabIndex        =   2
      Top             =   3480
      Width           =   1215
   End
   Begin VB.TextBox data 
      Height          =   4455
      Left            =   2520
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Top             =   240
      Width           =   7095
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   9960
      Top             =   4080
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
   Begin VB.CommandButton btse_port 
      Caption         =   "打开串口"
      Height          =   495
      Left            =   240
      TabIndex        =   0
      Top             =   2760
      Width           =   1215
   End
   Begin VB.Label las_bit 
      Caption         =   "停止位"
      Height          =   300
      Left            =   200
      TabIndex        =   8
      Top             =   2050
      Width           =   800
   End
   Begin VB.Label lad_bit 
      Caption         =   "数据位"
      Height          =   300
      Left            =   200
      TabIndex        =   7
      Top             =   1650
      Width           =   800
   End
   Begin VB.Label lac_bit 
      Caption         =   "校验位"
      Height          =   300
      Left            =   200
      TabIndex        =   6
      Top             =   1250
      Width           =   800
   End
   Begin VB.Label lab_rate 
      Caption         =   "波特率"
      Height          =   300
      Left            =   200
      TabIndex        =   5
      Top             =   850
      Width           =   800
   End
   Begin VB.Label las_port 
      Caption         =   "串口号"
      Height          =   300
      Index           =   0
      Left            =   200
      TabIndex        =   4
      Top             =   450
      Width           =   800
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim buffer As String
Private Sub cob_rate_Change()   '设置波特率
MSComm1.Settings = cob_rate.Text & "," & coc_bit.Text & "," & cod_bit.Text & "," & cos_bit.Text '设置波特率
End Sub

Private Sub coc_bit_Change()     '设置校验位
MSComm1.Settings = cob_rate.Text & "," & coc_bit.Text & "," & cod_bit.Text & "," & cos_bit.Text '设置波特率
End Sub

Private Sub cod_bit_Change()       '设置数据位
MSComm1.Settings = cob_rate.Text & "," & coc_bit.Text & "," & cod_bit.Text & "," & cos_bit.Text '设置波特率
End Sub

Private Sub cos_bit_Change()        '设置停止位
MSComm1.Settings = cob_rate.Text & "," & coc_bit.Text & "," & cod_bit.Text & "," & cos_bit.Text '设置波特率
End Sub

Private Sub cos_port_Change()
MSComm1.PortOpen = False
MSComm1.CommPort = cos_port.Text '设置通信口
MSComm1.PortOpen = True
End Sub

Private Sub Form_Load()
cos_port.AddItem "1"
cos_port.AddItem "2"
cos_port.AddItem "3"
cos_port.AddItem "4"
cob_rate.AddItem "2400"
cob_rate.AddItem "4800"
cob_rate.AddItem "9600"
cob_rate.AddItem "14400"
coc_bit.AddItem "E"
coc_bit.AddItem "M"
coc_bit.AddItem "N"
coc_bit.AddItem "O"
coc_bit.AddItem "S"
cod_bit.AddItem "5"
cod_bit.AddItem "6"
cod_bit.AddItem "7"
cod_bit.AddItem "8"
cos_bit.AddItem "1"
cos_bit.AddItem "1.5"
cos_bit.AddItem "2"
MSComm1.CommPort = cos_port.Text '设置通信口
MSComm1.Settings = cob_rate.Text & "," & coc_bit.Text & "," & cod_bit.Text & "," & cos_bit.Text '设置波特率
End Sub

Private Sub btse_port_Click()
If btse_port.Caption = "打开串口" Then
       MSComm1.PortOpen = True
       Timer1.Enabled = True
       btse_port.Caption = "关闭串口"
ElseIf btse_port.Caption = "关闭串口" Then
       MSComm1.PortOpen = False
       Timer1.Enabled = False
       btse_port.Caption = "打开串口"
End If
End Sub

Private Sub bt_clear_Click()
Data.Text = ""     '清空数据
MSComm1.InBufferCount = 0   '清空缓存区
End Sub

Private Sub bt_rece_Click()
If bt_rece.Caption = "开始接收" Then
   Timer1.Enabled = True
   Timer1.Interval = 1000
   bt_rece.Caption = "停止接收"
ElseIf bt_rece.Caption = "停止接收" Then
   Timer1.Enabled = False
   bt_rece.Caption = "开始接收"
   End If
End Sub

Private Sub MSComm1_OnComm()
MSComm1.InBufferCount = 0          '清空缓冲区
MSComm1.InputLen = 0       '读取整个缓冲区中的内容
End Sub

Private Sub Timer1_Timer()
buffer = MSComm1.Input
Data.Text = Data.Text + buffer
If buffer = "0" Then
Data.Text = "接收数据失败"
End If
End Sub

⌨️ 快捷键说明

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