用MSCOMM32.OCX,串口初始化和输出都正常,但不能取输入缓冲区的值,为什么?
步骤如下
1)用SUDT SerialNull 软件虚拟了两个串口com15,com16
2)程序对com15,com16初始化,com15用做模拟接收,com16用做模拟发送
Ole_MSC.object.CommPort=15 //设置通讯端口号为COM15
Ole_MSC.object.Settings = "9600,n,8,1" //波特率9600 bps,无奇偶校验,8位数据位,1位停止位。
Ole_MSC.object.InBufferSize =600 //设置接收缓冲区的字节数
Ole_MSC.object.OutBufferSize =600 //设置发送缓冲区的字节数
If Ole_MSC.object.PortOpen <> True Then
Ole_MSC.object.PortOpen = True //打开串口
End If
Ole_MSC.object.InputMode = 1 //0文本/1二进制方式读取数据
Ole_MSC.object.SThreshold = 0 //数据传输事件不产生 OnComm 事件
Ole_MSC.object.RThreshold = 7 //每7个字节的数据到缓冲区都触发OnComm 事件。
Ole_out.object.CommPort=16 //设置通讯端口号为COM16
Ole_out.object.Settings = "9600,n,8,1" //波特率9600 bps,无奇偶校验,8位数据位,1位停止位。
Ole_out.object.InBufferSize =600 //设置接收缓冲区的字节数
Ole_out.object.OutBufferSize =600 //设置发送缓冲区的字节数
If Ole_out.object.PortOpen <> True Then
Ole_out.object.PortOpen = True //打开串口
End If
Ole_out.object.InputMode = 1 //0文本/1二进制方式读取数据
Ole_out.object.SThreshold = 0 //数据传输事件不产生 OnComm 事件
Ole_out.object.RThreshold = 7 //每7个字节的数据到缓冲区都触发OnComm 事件。
3)程序中com15中oncomm事件代码
blob buffer
long ll_count
choose Case ole_msc.object.CommEvent
case 1
Case 3
case 2
ole_msc.object.inputlen=0
//ll_count=ole_msc.object.inbuffercount
//messagebox("ll_count",ll_count)
buffer=Ole_msc.object.Intput
end choose
4)com16发送数据代码
blob bufferout
Ole_out.object.OutBufferCount=0 //清空发送缓冲区
Ole_out.object.InBufferCount=0 //清空接收缓冲区
bufferout=blob("ReadData")
Ole_out.object.Output =bufferout //向传输缓冲区发送读数据命令
问题,我在com16中发送数据后,com15可正确触发oncomm,但执行到"buffer=Ole_msc.object.Intput"这句时,系统出错
错误信息是:
Application terminated
Error:Error accessing external object property input at line 41 in oncmm event of objet ole_msc of w_ocx
请问是怎么回事呢?
应该不是吧,我用windows自带的超级终端连接15端口,可以收到我16端口发的数据
recLenFlag = 0 '命令对应回的数据长度标志
Port = 3
On Error GoTo ret
ret:
' ' For Port = pi To 4
' If COMCheckPort(Port) Then
' Else
' End If
MSComm1.CommPort = Port 'Text1.Text
MSComm1.Settings = "2400,n,8,1" '串口设置
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.InBufferSize = 512 '接收缓冲区大小
MSComm1.OutBufferSize = 512 '发送缓冲区大小
MSComm1.RThreshold = 10 '产生MSComm事件
MSComm1.PortOpen = True '开串口
MDIForm1.MSComm1.InBufferCount = 0 '清空输入寄存器
MDIForm1.MSComm1.OutBufferCount = 0 '清空传输缓冲区
recLenFlag = 3 '此命令对应回6个字符
'总长度
cmdSelfCheck(0) = &H2
'命令
cmdSelfCheck(1) = &HBA
mm = cmdSelfCheck(1) And &HFF
'mm = (Not (mm)) + 1
cmdSelfCheck(2) = mm And &HFF ' end
'打开端口
If MDIForm1.MSComm1.PortOpen = True Then
MDIForm1.MSComm1.Output = cmdSelfCheck '发送自检命令
MDIForm1.MSComm1.RThreshold = 1
End If
'处理返回的结果函数
test = doData
For i = 0 To Len(test) / 2 - 1
checkdata(i) = CByte("&H" & Mid(test, i * 2 + 1, 2))
Next i
If checkdata(0) = &H55 Then '0x55通讯标识, 若返回0x55,则换下个串口
pi = pi + 1
Else
GoTo clearon '则通讯成功,并以此端口工作
End If
MSComm1.PortOpen = False
' Next
clearon:
MDIForm1.MSComm1.InBufferCount = 0 '清空输入寄存器
MDIForm1.MSComm1.OutBufferCount = 0 '清空传输缓冲区
'MsgBox MSComm1.PortOpen
If MDIForm1.MSComm1.PortOpen = True Then
MsgBox "ok", , "普天"
Else
'MSComm1.PortOpen = False
MsgBox "请检查串口连接!", , " 普 天 "
End If
接受处理的函数:
Function doData()
Dim recMsgByte() As Byte
Dim RecLen As Integer
Dim sTemp As String
Dim s1 As String
'Dim I As Integer
Dim countTim As Long
Dim flag As Boolean
flag = True
MDIForm1.MSComm1.RThreshold = 0 '关闭OnComm事件接
Do
DoEvents
i = i + 1
If i = 10000 Then
doData = "55 "
GoTo lab
End If
Loop Until MDIForm1.MSComm1.InBufferCount = recLenFlag
RecLen = MDIForm1.MSComm1.InBufferCount
ReDim recMsgByte(RecLen)
recMsgByte = MDIForm1.MSComm1.Input
sTemp = ""
For i = 0 To RecLen - 1
s1 = Hex(recMsgByte(i))
If Len(s1) < 2 Then s1 = "0" & s1
sTemp = sTemp & s1
Next i
doData = sTemp
lab:
MDIForm1.MSComm1.InBufferCount = 0 '清空输入寄存器
MDIForm1.MSComm1.OutBufferCount = 0 '清空传输缓冲区
End Function