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

📄 form1.frm

📁 双机的串口通讯
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1 
   Caption         =   "串口通讯"
   ClientHeight    =   4905
   ClientLeft      =   6180
   ClientTop       =   4740
   ClientWidth     =   6915
   LinkTopic       =   "Form1"
   ScaleHeight     =   4905
   ScaleWidth      =   6915
   Begin VB.TextBox Text4 
      Height          =   375
      Left            =   2040
      MaxLength       =   12
      TabIndex        =   11
      Top             =   3480
      Width           =   2535
   End
   Begin VB.TextBox Text3 
      Height          =   375
      Left            =   2040
      MaxLength       =   12
      MultiLine       =   -1  'True
      TabIndex        =   10
      Top             =   2880
      Width           =   2535
   End
   Begin VB.CommandButton Command6 
      Caption         =   "关闭"
      Height          =   375
      Left            =   4920
      TabIndex        =   7
      Top             =   4320
      Width           =   975
   End
   Begin VB.CommandButton Command5 
      Caption         =   "设置"
      Height          =   375
      Left            =   3240
      TabIndex        =   6
      Top             =   4320
      Width           =   975
   End
   Begin VB.CommandButton Command4 
      Caption         =   "连接"
      Height          =   375
      Left            =   1440
      TabIndex        =   5
      Top             =   4320
      Width           =   975
   End
   Begin VB.CommandButton Command2 
      Caption         =   "发送"
      Height          =   375
      Left            =   4920
      TabIndex        =   4
      Top             =   3480
      Width           =   1215
   End
   Begin VB.CommandButton Command1 
      Caption         =   "发送"
      Height          =   375
      Left            =   4920
      TabIndex        =   3
      Top             =   2880
      Width           =   1215
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   6240
      Top             =   3240
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
      RThreshold      =   10
      SThreshold      =   10
   End
   Begin VB.Frame Frame1 
      Caption         =   "COM1"
      Height          =   2535
      Left            =   240
      TabIndex        =   0
      Top             =   120
      Width           =   6255
      Begin VB.TextBox Text2 
         Height          =   1695
         Left            =   3360
         MaxLength       =   500
         MultiLine       =   -1  'True
         TabIndex        =   9
         Top             =   600
         Width           =   2535
      End
      Begin VB.TextBox Text1 
         Height          =   1695
         Left            =   360
         MaxLength       =   500
         MultiLine       =   -1  'True
         TabIndex        =   8
         Top             =   600
         Width           =   2535
      End
      Begin VB.Label Label3 
         Caption         =   "接受区"
         Height          =   255
         Left            =   4200
         TabIndex        =   13
         Top             =   240
         Width           =   735
      End
      Begin VB.Label Label2 
         Caption         =   "发送区"
         Height          =   255
         Left            =   1200
         TabIndex        =   12
         Top             =   240
         Width           =   735
      End
   End
   Begin VB.Label Label1 
      Caption         =   "十六进制字符串"
      Height          =   255
      Index           =   1
      Left            =   240
      TabIndex        =   2
      Top             =   3480
      Width           =   1455
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      Caption         =   "字符串输入"
      Height          =   255
      Index           =   0
      Left            =   240
      TabIndex        =   1
      Top             =   2880
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim buf As String
Private Sub Command1_Click()
If Not MSComm1.PortOpen = True Then
MsgBox ("请先连接")
Exit Sub
End If

If Text3.Text = "" Then
MsgBox ("请输入字符串")
Else: Text1.Text = Text1.Text + "Send:" + Trim(Text3.Text)
  MSComm1.Output = Trim(Text3.Text)
   Text3.Text = ""
   MSComm1.OutBufferCount = 0 '清空输出寄存器
End If
     
End Sub

Private Sub Command2_Click()
If Not MSComm1.PortOpen = True Then
MsgBox ("请先连接")
Exit Sub
End If
If Text4.Text = "" Then
MsgBox ("请输入十六进制字符串")
ElseIf Asc(Text4.Text) < Asc("0") Or (Asc(Text4.Text) > Asc("9") And Asc(Text4.Text) < Asc("A")) Then
      MsgBox ("这不是一个合法的十六进制字符")
      Text4.Text = ""
 ElseIf Asc(Text4.Text) > Asc("F") And Asc(Text4.Text) < Asc("a") Then
      MsgBox ("这不是一个合法的十六进制字符")
      Text4.Text = ""
 ElseIf Asc(Text4.Text) > Asc("f") Then
      MsgBox ("这不是一个合法的十六进制字符")
      Text4.Text = ""

Else: Text1.Text = Text1.Text + "(Hex)Send:" + Trim(Text4.Text)
MSComm1.Output = Trim(Text4.Text)
Text4.Text = ""
MSComm1.OutBufferCount = 0 '清空输出寄存器
End If
End Sub

Private Sub Command4_Click()
If Not MSComm1.PortOpen = True Then
MSComm1.PortOpen = True
End If


End Sub

Private Sub Command5_Click()
Form2.Show
End Sub

Private Sub Command6_Click()
Unload Me

End Sub


Private Sub Form_Load()

With MSComm1
.CommPort = 1 '使用COM2
.Settings = "9600,N,8,1" '设置通信口参数
.InBufferSize = 40
'设置MSComm1接收缓冲区为40字节
.OutBufferSize = 2
'设置MSComm1发送缓冲区为2字节
'.InputMode = comInputModeBinary
'设置接收数据模式为 2进制形式
.InputLen = 20
'设置Input 次从接收缓冲读取字节数为1
.SThreshold = 20
'设置Output 次从发送缓冲读取字节数为1
InBufferCount = 0 '清除接收缓冲区
.OutBufferCount = 0 '清除发送缓冲区
.RThreshold = 1
'设置接收个字节产生OnComm事件

End With
End Sub





Private Sub MSComm1_OnComm()

    buf = MSComm1.Input
     Select Case MSComm1.CommEvent
            Case comEvReceive   ' 收到 RThreshold # of
                 Text2.Text = Text2.Text + buf
                 MSComm1.InBufferCount = 0 '清空输入寄存器
            Case comEvSend  ' 传输缓冲区有 Sthreshold 个字符                            '
    End Select


End Sub

⌨️ 快捷键说明

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