7,785
社区成员




Private Sub Comm_OnComm()
Select Case Comm.CommEvent
Case comEvReceive
tempstr = tempstr + Comm.Input
End Select
End Sub
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'串口属性设置,根据你的实际情况修改
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InBufferSize = 1024
.OutBufferSize = 1024
.InputLen = 1
.RThreshold = 1
.InputMode = comInputModeBinary
.PortOpen = True
End With
End Sub
'用中断方式接收数据
Private Sub MSComm1_OnComm()
Dim intP As Integer
Dim varP As Variant
Dim ReadByte(0 To 1023) As Byte '定义一个足够大的数组,用来接收串口数据
If MSComm1.CommEvent = 2 Then
MSComm1.RThreshold = 0
Sleep 20
For intP = 0 To MSComm1.InBufferCount - 1
If varP <> Null Then varP = Null
varP = MSComm1.Input
ReadByte(intP) = varP(0)
Next intP
MSComm1.RThreshold = 1
End If
End Sub