大家进来看一看!
请问各位,我的程序错在哪里?
运行时,点击command1时,总出现“端口已打开”的问题;
点击command2,却说“设备未打开”,为什么?
这是小弟的入门之作,多有抄袭拼凑之嫌,请各位多多指教!不要见笑!
Dim ytemp() As Byte
Dim stemp As String
Dim i As Integer
Private Sub Command2_Click()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 1024
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
If Err Then
MsgBox "串口通讯失败!"
Exit Sub
End If
End If
End Sub
Private Sub Command1_Click()
MSComm1.PortOpen = False
Exit Sub
End Sub
Private Sub MSComm1_OnComm()
With MSComm1
Select Case .CommEvent
Case comEvReceive
'接受
If MSComm1.InBufferCount > 0 Then
i = MSComm1.InBufferCount
ytemp = MSComm1.Input
stemp = StrConv(ytemp, vbFromUnicode)
Text1.Text = stemp
End If
End Sub
Private Sub Timer1_Timer()
'发送
stemp = Text2.Text
ytemp = StrConv(stemp, vbUnicode)
Debug.Print UBound(ytemp)
MSComm1.Output = ytemp
End Sub