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

📄 comunicate.frm

📁 一个通过串口把PC机与MODEM连接的程序
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   5685
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7080
   FillColor       =   &H00C0C0C0&
   ForeColor       =   &H80000005&
   LinkTopic       =   "Form1"
   ScaleHeight     =   5685
   ScaleWidth      =   7080
   StartUpPosition =   3  'Windows Default
   WhatsThisButton =   -1  'True
   WhatsThisHelp   =   -1  'True
   Begin VB.CommandButton cmd_exit 
      Caption         =   "退出"
      Height          =   735
      Left            =   480
      MaskColor       =   &H00E0E0E0&
      TabIndex        =   14
      Top             =   4200
      Width           =   375
   End
   Begin VB.Frame Frame3 
      Caption         =   "参数设置"
      ForeColor       =   &H80000001&
      Height          =   1335
      Left            =   1320
      TabIndex        =   8
      Top             =   4200
      Width           =   5655
      Begin VB.CommandButton cmd_par_set 
         Caption         =   "确认"
         Height          =   375
         Left            =   4320
         TabIndex        =   13
         Top             =   480
         Width           =   975
      End
      Begin VB.TextBox Text4 
         Height          =   375
         Left            =   3000
         TabIndex        =   12
         Text            =   "1"
         Top             =   480
         Width           =   735
      End
      Begin VB.TextBox Text3 
         Height          =   375
         Left            =   960
         TabIndex        =   10
         Text            =   "9600"
         Top             =   480
         Width           =   615
      End
      Begin VB.Label Label2 
         Caption         =   "通讯端口"
         Height          =   375
         Left            =   2040
         TabIndex        =   11
         Top             =   480
         Width           =   735
      End
      Begin VB.Label Label1 
         Caption         =   "波特率"
         Height          =   375
         Left            =   240
         TabIndex        =   9
         Top             =   480
         Width           =   615
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "接收数据"
      ForeColor       =   &H80000001&
      Height          =   1455
      Left            =   1320
      TabIndex        =   4
      Top             =   2400
      Width           =   5535
      Begin VB.TextBox Text2 
         BeginProperty DataFormat 
            Type            =   0
            Format          =   "0.000E+00"
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
         BeginProperty Font 
            Name            =   "宋体"
            Size            =   12
            Charset         =   134
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   975
         Left            =   1920
         TabIndex        =   7
         Top             =   240
         WhatsThisHelpID =   1
         Width           =   3255
      End
      Begin VB.CommandButton cmd_rx_stop 
         Caption         =   "停止"
         Height          =   375
         Left            =   360
         TabIndex        =   6
         Top             =   960
         Width           =   975
      End
      Begin VB.CommandButton cmd_rx_start 
         Appearance      =   0  'Flat
         BackColor       =   &H8000000A&
         Caption         =   "开始"
         Height          =   375
         Left            =   360
         MaskColor       =   &H000000FF&
         TabIndex        =   5
         Top             =   360
         Width           =   975
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "发送数据"
      ForeColor       =   &H80000001&
      Height          =   1575
      Left            =   1320
      TabIndex        =   0
      Top             =   480
      Width           =   5535
      Begin VB.TextBox Text1 
         Height          =   975
         Left            =   1920
         TabIndex        =   3
         Top             =   360
         Width           =   3255
      End
      Begin VB.CommandButton cmd_tx_stop 
         Caption         =   "停止"
         Height          =   375
         Left            =   360
         TabIndex        =   2
         Top             =   960
         Width           =   975
      End
      Begin VB.CommandButton cmd_tx_start 
         Caption         =   "开始"
         Height          =   375
         Left            =   360
         MaskColor       =   &H00FFFF80&
         TabIndex        =   1
         Top             =   360
         UseMaskColor    =   -1  'True
         Width           =   975
      End
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   240
      Top             =   1920
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
      RThreshold      =   2
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public RX_buffer_high As Variant
Public RX_buffer_low As Variant
Public RX_data As Variant
Public RX_low As Boolean
Public RX_high As Boolean
Public TX_rdy As Boolean
Public RX_rdy As Boolean
Public RX_finish As Boolean

Private Sub cmd_exit_Click()
Form1.Hide
End Sub

Private Sub cmd_rx_start_Click()
If (Form1.MSComm1.PortOpen = False) Then                '打开串口
Form1.MSComm1.PortOpen = True
End If
Form1.cmd_rx_start.UseMaskColor = True
Form1.cmd_rx_start.MaskColor = RGB(255, 0, 255)
If (MSComm1.InBufferCount > 0) Then
'Form1.Text2.Text = RX_buffer_high & RX_buffer_low
'Form1.Text2.Text = Val("&H " & RX_buffer_low)
Form1.Text2.Enabled = True
Form1.Text2.Appearance = Ture
RX_rdy = False
RX_data = MSComm1.Input
'Text2.Text = Hex(RX_data(0)) & Hex(RX_data(1))
Text2.Text = (256 * Val(RX_data(0)) + Val(RX_data(1))) * 1200 / 4096
End If
 
 

End Sub

Private Sub cmd_rx_stop_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False                '关闭串口
Text2.Text = ""
End If

End Sub


Private Sub cmd_tx_start_Click()

If (Form1.MSComm1.PortOpen = False) Then                 '打开串口
Form1.MSComm1.PortOpen = True
End If
If (TX_rdy) Then
Form1.MSComm1.Output = Text1.Text      '写发送缓冲区
TX_rdy = False
End If

End Sub

Private Sub cmd_tx_stop_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False                '关闭串口
End If
End Sub
Private Sub cmd_par_set_Click()

If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False                '关闭串口
End If
MSComm1.Settings = Text3.Text & "N,8,1"
MSComm1.CommPort = CInt(Text4.Text)
If (Form1.MSComm1.PortOpen = False) Then                 '打开串口
Form1.MSComm1.PortOpen = True
End If
End Sub


Private Sub Form_Load()
    Form1.MSComm1.CommPort = 1
    Form1.MSComm1.InputMode = comInputModeBinary
    Form1.MSComm1.Settings = "4800,n,8,1"         '9600波特率,无校验,8位数据位,1位停止位
    Form1.MSComm1.InBufferSize = 256              '设置接收缓冲区为256字节
    Form1.MSComm1.OutBufferSize = 256             '设置发送缓冲区为256字节
    Form1.MSComm1.InputLen = 2                    '读取接收缓冲区的所有字符

    Form1.MSComm1.RThreshold = 1                 '发送缓冲区空触发发送事件
    Form1.MSComm1.SThreshold = 1                 '每一个字符到接收缓冲区都触发接收事件

    
    Form1.MSComm1.NullDiscard = False             '允许发送0字符
    Form1.MSComm1.OutBufferCount = 0              '清空发送缓冲区
    Form1.MSComm1.InBufferCount = 0               '清空接收缓冲区
    Form1.MSComm1.PortOpen = True
    
    
    TX_rdy = True
    RX_low = False
    RX_high = True
    RX_rdy = True
    RX_finish = False
    
    
    
End Sub

Public Sub MSComm1_OnComm()

Select Case MSComm1.CommEvent

Case comEVRecive
If (RX_low = True) Then
RX_buffer_low = MSComm1.Input                     '接收数据并清空接收缓冲区
'Text2.Text = CStr(RX_buffer_low)      '清空接收缓冲区
RX_low = False
RX_high = True
'RX_finish = True
ElseIf (RX_high = True) Then
RX_buffer_high = MSComm1.Input
'Text2.Text = Text2.Text + CStr(RX_buffer_high)
Form1.MSComm1.InBufferCount = 0
RX_low = True
RX_high = False
'RX_finish = True
End If

    
 'Case comEvSend
 
 'MSComm1.Output = CByte(Text1.Text)
 Case comEvSend
 
 TX_rdy = True
 
 
 End Select
 
End Sub



Private Sub Text3_Change()
Form1.MSComm1.InBufferCount = 0

End Sub

⌨️ 快捷键说明

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