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

📄 pc&mcs51.frm

📁 PC机与单片机之间通过串口进行通信 已测试通过
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form COMForm 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "PC机与单片机串口通信"
   ClientHeight    =   2235
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6330
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2235
   ScaleWidth      =   6330
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1 
      Caption         =   "发送"
      Height          =   375
      Left            =   1080
      TabIndex        =   5
      Top             =   1560
      Width           =   855
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   2880
      Top             =   1440
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
      InputMode       =   1
   End
   Begin VB.CommandButton Cmdquit 
      Caption         =   "关闭程序"
      Height          =   435
      Left            =   3960
      TabIndex        =   2
      Top             =   1560
      Width           =   1035
   End
   Begin VB.TextBox TextReceive 
      Height          =   795
      Left            =   3270
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Top             =   480
      Width           =   3015
   End
   Begin VB.TextBox Textsend 
      Height          =   795
      Left            =   30
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   0
      Top             =   450
      Width           =   3015
   End
   Begin VB.Label Label2 
      Caption         =   "显示接收字符区:"
      Height          =   195
      Left            =   3300
      TabIndex        =   4
      Top             =   180
      Width           =   2415
   End
   Begin VB.Label Label1 
      Caption         =   "输入发送字符区:"
      Height          =   195
      Left            =   60
      TabIndex        =   3
      Top             =   180
      Width           =   1695
   End
End
Attribute VB_Name = "COMForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub Command1_Click()
Dim a(1) As Byte
b = Textsend.Text
a(1) = Val("&H" & b)                'a(1)=cbyte("&H" & b)  串口MSComm1.Output发出去的是十进制
MSComm1.Output = a                  '所以要先将十六进制改为十进制再进行发送
End Sub
' 初始化程序:主要完成对串口的设置,包括选择串口、设置波特率、设置数据格式、打开串口等。
' 注意:在程序开始之前,必须清空发送和接收缓冲区,以免出错。
Private Sub Form_Load()
  MSComm1.InputMode = 1             ' 只与接收有关,1代表二进制收,0代表字符收
  MSComm1.Settings = "9600,n,8,1"   ' 设置波特率和发送字符格式
  MSComm1.CommPort = 1              ' 设置通讯串口
  MSComm1.InputLen = 0              ' 设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
  MSComm1.InBufferSize = 0          ' 设置接收缓冲区512Byte
  MSComm1.InBufferCount = 0
  MSComm1.OutBufferSize = 0         ' 设置发送缓冲区512Byte
  MSComm1.OutBufferCount = 0
  MSComm1.RThreshold = 1            ' 每个字符到接收缓冲区都触发接收事件
  MSComm1.SThreshold = 1            '
  MSComm1.PortOpen = True           ' 打开串口
End Sub
Private Sub MSComm1_OnComm()
Dim s() As Byte                                 '定义数组时若定义了下标,就不能对它进行赋值
  If (MSComm1.CommEvent = comEvReceive) Then
  s = MSComm1.Input                             'MSComm1.Input会对收到的二进制(十六进制)自动转为十进制 并插入1个0
  TextReceive.Text = Hex(s(1))                  ' Right("0" & Hex(s(i)), 2)
  End If
End Sub
 '关闭串口,关闭程序
Private Sub Cmdquit_Click()
   MSComm1.PortOpen = False
   Unload Me
End Sub

'可以发送并接收00~ff的数据
'VB采用的是Unicode来存储和操作字符串,Unicode是全部用两个字节表示一个字符的
'Unicode为了保持与ASCii的兼容,仅将其每个码的字节数变为两个,增加的字节以零填入

⌨️ 快捷键说明

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