读书人

mscomm接收串口数据程序死机的有关问题

发布时间: 2013-08-04 18:26:15 作者: rapoo

【求助】mscomm接收串口数据程序死机的问题!


Private Sub Form_Load()
With ctrMSComm
.CommPort = 3
.Settings = "38400,N,8,1"
.InBufferSize = 4096
.RThreshold = 1
.InBufferCount = 0
.InputLen = 0
End With
If ctrMSComm.PortOpen = False Then
ctrMSComm.PortOpen = True
If Err Then
MsgBox (Err.Description)
Exit Sub
End If
End If
End Sub



Private Sub ctrMSComm_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer



Select Case Form1.ctrMSComm.CommEvent

Case comEvReceive
'此处添加处理接收的代码

Form1.ctrMSComm.InputMode = comInputModeBinary
intInputLen = Form1.ctrMSComm.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Form1.ctrMSComm.Input
Call InputManage(bytInput, intInputLen)
Call GetDisplayText

End Select

End Sub


 



Public bytReceiveByte() As Byte '接收到的字节
Public intReceiveLen As Integer '接收到的字节数

Public strHex As String '十六进制编码


'***********************************
'为输出准备文本
'保存在全局变量
'strText
'strHex
'strAddress
'总行数保存在
'intLine
'***********************************

Public Sub GetDisplayText()

Dim n As Integer
Dim intValue As Integer
Dim intHighHex As Integer
Dim intLowHex As Integer

strHex = "" '设置初值

'*****************************************
'获得16进制码
'*****************************************



For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)

intHighHex = intValue \ 16
intLowHex = intValue - intHighHex * 16

If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If

strHex = strHex + " " + Chr$(intHighHex) + Chr$(intLowHex) + " "

If (n Mod 8) = 0 Then '设置换行


strHex = strHex + Chr$(13) + Chr$(10)
End If
Form1.Text1.Text = strHex
Next n



End Sub

'**********************************
'输入处理
'处理接收到的字节流,并保存在全局变量
'bytReceiveRyte()
'**********************************



Public Sub InputManage(bytInput() As Byte, intInputLenth As Integer)




Dim n As Integer '定义变量及初始化

ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)

For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n

intReceiveLen = intReceiveLen + intInputLenth

End Sub




数据是硬件发过来的,发送的数据量非常大,串口调试精灵可以一直接收数据并且显示。结果自己写的只能显示2,3行,然后程序就死了。求大神帮一下!新手,实在弄不明白。。 VB 串口
[解决办法]
每次硬件发送的数据有多大?1K够不够?
[解决办法]
你得程序应该是“假死”,也就是接受事件 OnComm没有处理好所致。参阅这个:
http://download.csdn.net/detail/veron_04/4037248
[解决办法]
先别对接收到的数据做处理,只是显示下看有没有你说的死机的问题?

读书人网 >VB

热点推荐