VERSION 5.00 Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX" Begin VB.Form frmMain Caption = "MsComm" ClientHeight = 3195 ClientLeft = 60 ClientTop = 345 ClientWidth = 4680 BeginProperty Font Name = "宋体" Size = 9 Charset = 134 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty LinkTopic = "Form1" ScaleHeight = 3195 ScaleWidth = 4680 StartUpPosition = 3 'Windows Default Begin VB.CommandButton Command4 Height = 375 Left = 3240 TabIndex = 3 Top = 1560 Width = 1335 End Begin MSCommLib.MSComm MSComm2 Left = 840 Top = 0 _ExtentX = 1005 _ExtentY = 1005 _Version = 393216 DTREnable = -1 'True End Begin VB.CommandButton Command3 Caption = "打开端口" Height = 375 Left = 3240 TabIndex = 2 Top = 1080 Width = 1335 End Begin VB.CommandButton Command2 Caption = "发送数据" Height = 375 Left = 3240 TabIndex = 1 Top = 600 Width = 1335 End Begin VB.CommandButton Command1 Caption = "准备接收" Height = 375 Left = 3240 TabIndex = 0 Top = 120 Width = 1335 End Begin MSCommLib.MSComm MSComm1 Left = 120 Top = 0 _ExtentX = 1005 _ExtentY = 1005 _Version = 393216 DTREnable = -1 'True End End Attribute VB_Name = "frmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Dim isReceive As BooleanPrivate Sub Command1_Click() ''On Error Resume Next MSComm1.InBufferCount = 0 ''清缓冲区 MSComm1.InputLen = 1 ''设置触发的阀值 isReceive = False Print "正在接收数据..." Do While Not isReceive Me.Caption = "正在接收数据..." DoEvents Loop
End SubPrivate Sub Command2_Click() ''On Error Resume Next Dim bBuffer(0 To 7) As Byte Dim I As Integer Dim sTemp As String bBuffer(0) = &HFE bBuffer(1) = &H45 bBuffer(2) = &H10 bBuffer(3) = &H6A bBuffer(4) = &H6A bBuffer(5) = &H6A bBuffer(6) = &HA1 bBuffer(7) = &H80 sTemp = "发送数据:" For I = 0 To 7 sTemp = sTemp & Hex(bBuffer(I)) & Space(1) Next I Print sTemp MSComm2.Output = bBuffer
End SubPrivate Sub Command3_Click() On Error Resume Next MSComm1.CommPort = 1 ''COM1 MSComm1.Settings = "9600,n,8,1" If Not MSComm1.PortOpen Then ''打开串口 MSComm1.PortOpen = True End If If Err Then Err.Clear MsgBox "COM1 cannot be opened!", vbCritical Else Command1.Enabled = True Print "COM1 Success!" End If MSComm2.CommPort = 2 ''COM2 MSComm2.Settings = "9600,n,8,1" If Not MSComm2.PortOpen Then ''打开串口 MSComm2.PortOpen = True End If If Err Then Err.Clear MsgBox "COM2 cannot be opened!", vbCritical Else Command2.Enabled = True Print "COM2 Success!" End IfEnd SubPrivate Sub Command4_Click() Unload Me End SubPrivate Sub Form_Load() Command1.Caption = "准备接收" Command2.Caption = "发送数据" Command3.Caption = "打开端口" Command4.Caption = "退出" Command1.Enabled = False Command2.Enabled = False End SubPrivate Sub Form_Unload(Cancel As Integer) If MSComm1.PortOpen Then ''关闭串口 MSComm1.PortOpen = False End If If MSComm2.PortOpen Then ''关闭串口 MSComm2.PortOpen = False End If End End SubPrivate Sub MSComm1_OnComm() Dim bBuffer(0 To 7) As Byte Dim I As Integer Dim sTemp As String
Select Case MSComm1.CommEvent Case comEvReceive bBuffer = MSComm1.Input Me.Caption = "收到数据" sTemp = "接收数据:" For I = 0 To 7 sTemp = sTemp & Hex(bBuffer(I)) & Space(1) Next I Print sTemp isReceive = True Case Else MsgBox "Other!", vbInformation End Select
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmMain
Caption = "MsComm"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
BeginProperty Font
Name = "宋体"
Size = 9
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command4
Height = 375
Left = 3240
TabIndex = 3
Top = 1560
Width = 1335
End
Begin MSCommLib.MSComm MSComm2
Left = 840
Top = 0
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.CommandButton Command3
Caption = "打开端口"
Height = 375
Left = 3240
TabIndex = 2
Top = 1080
Width = 1335
End
Begin VB.CommandButton Command2
Caption = "发送数据"
Height = 375
Left = 3240
TabIndex = 1
Top = 600
Width = 1335
End
Begin VB.CommandButton Command1
Caption = "准备接收"
Height = 375
Left = 3240
TabIndex = 0
Top = 120
Width = 1335
End
Begin MSCommLib.MSComm MSComm1
Left = 120
Top = 0
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim isReceive As BooleanPrivate Sub Command1_Click()
''On Error Resume Next
MSComm1.InBufferCount = 0 ''清缓冲区
MSComm1.InputLen = 1 ''设置触发的阀值
isReceive = False
Print "正在接收数据..."
Do While Not isReceive
Me.Caption = "正在接收数据..."
DoEvents
Loop
End SubPrivate Sub Command2_Click()
''On Error Resume Next Dim bBuffer(0 To 7) As Byte
Dim I As Integer
Dim sTemp As String bBuffer(0) = &HFE
bBuffer(1) = &H45
bBuffer(2) = &H10
bBuffer(3) = &H6A
bBuffer(4) = &H6A
bBuffer(5) = &H6A
bBuffer(6) = &HA1
bBuffer(7) = &H80
sTemp = "发送数据:"
For I = 0 To 7
sTemp = sTemp & Hex(bBuffer(I)) & Space(1)
Next I
Print sTemp
MSComm2.Output = bBuffer
End SubPrivate Sub Command3_Click()
On Error Resume Next
MSComm1.CommPort = 1 ''COM1
MSComm1.Settings = "9600,n,8,1"
If Not MSComm1.PortOpen Then ''打开串口
MSComm1.PortOpen = True
End If
If Err Then
Err.Clear
MsgBox "COM1 cannot be opened!", vbCritical
Else
Command1.Enabled = True
Print "COM1 Success!"
End If MSComm2.CommPort = 2 ''COM2
MSComm2.Settings = "9600,n,8,1"
If Not MSComm2.PortOpen Then ''打开串口
MSComm2.PortOpen = True
End If
If Err Then
Err.Clear
MsgBox "COM2 cannot be opened!", vbCritical
Else
Command2.Enabled = True
Print "COM2 Success!"
End IfEnd SubPrivate Sub Command4_Click()
Unload Me
End SubPrivate Sub Form_Load()
Command1.Caption = "准备接收"
Command2.Caption = "发送数据"
Command3.Caption = "打开端口"
Command4.Caption = "退出"
Command1.Enabled = False
Command2.Enabled = False
End SubPrivate Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then ''关闭串口
MSComm1.PortOpen = False
End If
If MSComm2.PortOpen Then ''关闭串口
MSComm2.PortOpen = False
End If
End
End SubPrivate Sub MSComm1_OnComm()
Dim bBuffer(0 To 7) As Byte
Dim I As Integer
Dim sTemp As String
Select Case MSComm1.CommEvent
Case comEvReceive
bBuffer = MSComm1.Input
Me.Caption = "收到数据"
sTemp = "接收数据:"
For I = 0 To 7
sTemp = sTemp & Hex(bBuffer(I)) & Space(1)
Next I
Print sTemp
isReceive = True
Case Else
MsgBox "Other!", vbInformation
End Select
End Sub
,mscomm控件十分简单.