www.pudn.com > 89c52-comunicate-VB.rar > Form1.frm


VERSION 5.00 
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX" 
Begin VB.Form Form1  
   Caption         =   "Form1" 
   ClientHeight    =   3285 
   ClientLeft      =   7725 
   ClientTop       =   5715 
   ClientWidth     =   5865 
   LinkTopic       =   "Form1" 
   ScaleHeight     =   3285 
   ScaleWidth      =   5865 
   Begin VB.CommandButton Command2  
      Caption         =   "清除" 
      Height          =   375 
      Left            =   120 
      TabIndex        =   5 
      Top             =   1680 
      Width           =   495 
   End 
   Begin VB.TextBox Text2  
      Appearance      =   0  'Flat 
      Height          =   1695 
      Left            =   720 
      MultiLine       =   -1  'True 
      ScrollBars      =   2  'Vertical 
      TabIndex        =   4 
      Top             =   1080 
      Width           =   4815 
   End 
   Begin MSCommLib.MSComm MSComm1  
      Left            =   0 
      Top             =   2280 
      _ExtentX        =   1005 
      _ExtentY        =   1005 
      _Version        =   393216 
      DTREnable       =   0   'False 
      InBufferSize    =   1 
      InputLen        =   1 
      OutBufferSize   =   1 
      RThreshold      =   1 
      BaudRate        =   4800 
      SThreshold      =   1 
      InputMode       =   1 
   End 
   Begin VB.TextBox Text1  
      Appearance      =   0  'Flat 
      Height          =   270 
      Left            =   1560 
      MaxLength       =   2 
      TabIndex        =   1 
      Text            =   "ff" 
      Top             =   480 
      Width           =   375 
   End 
   Begin VB.CommandButton Command1  
      Caption         =   "发送" 
      Height          =   375 
      Left            =   2160 
      TabIndex        =   0 
      Top             =   480 
      Width           =   1095 
   End 
   Begin VB.Label Label2  
      Caption         =   "接收:" 
      Height          =   255 
      Left            =   120 
      TabIndex        =   3 
      Top             =   1080 
      Width           =   615 
   End 
   Begin VB.Label Label1  
      Caption         =   "发送(十六进制):" 
      Height          =   255 
      Left            =   120 
      TabIndex        =   2 
      Top             =   480 
      Width           =   1575 
   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() '点击按钮1(发送按钮)触发 
 
Dim aaa(0) As Byte '定义一个字节型一维数组aaa, 
aaa(0) = "&h" + Text1.Text '组合16进制以便发送 
MSComm1.OutBufferCount = 0 '清空输出寄存器 
MSComm1.Output = aaa '发送 
 
End Sub 
 
 
Private Sub Command2_Click() '点击按钮2(清屏)时触发 
Text2.Text = "" '清屏 
End Sub 
 
Private Sub Form_Load() '加载窗体时触发 
 
MSComm1.CommPort = 1 '用Com1口 
MSComm1.Settings = "4800,n,8,1" '通讯参数 波特率 奇偶校验 数据位 停止位 
MSComm1.PortOpen = True '开串口 
 
End Sub 
 
 
 
Private Sub MSComm1_OnComm() '有数据传送时触发 
 
Select Case MSComm1.CommEvent 'CommEvent属性:返回最近的通讯事件或错误。通过对它具体属性值的查询,我们就可以获得通讯事件和通 讯错误的完整信息。当其值是comEvReceive时表示接收到数据。 
Case comEvReceive '有接收事件 
 
 
 
Dim a As Variant '定义1个可变类型 
Dim b(0) As Byte '字节数组 
 
a = MSComm1.Input '接收到的数据存放到a里 
b(0) = AscB(a) ' 返回a的值  asc()返回字符串第一个字母的值 
 
 
Text2.Text = Text2.Text & " " & Hex(b(0)) '组合显示&   并且转换成16进制hex() 
 
MSComm1.InBufferCount = 0 '...清空输入寄存器 
 
End Select 
 
End Sub