用逐语句或逐过程调试,结果正确,一旦正常运行,总是出错
我是vb中菜鸟 ,求指点
Option Explicit
Private Sub cmdsend_Click()
Dim i, FsArr() As Byte
ReDim FsArr(7)
For i = 0 To 7
FsArr(i) = Val("&H" + Text发送(i))
Next i
MSComm1.Output = FsArr '发送字节数组
End Sub
Private Sub Cmdclear_Click()
Dim i
For i = 0 To 7
Text接收(i) = ""
Next i
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 6 'COM端口
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.PortOpen = True '打开端口
End Sub
Private Sub MSComm1_OnComm()
Dim i, JsArr
If MSComm1.InBufferCount > 0 Then
JsArr = MSComm1.Input '从接收缓冲区取二进制数据
For i = 0 To UBound(JsArr) '显示8个接收数据
Text接收(i) = Hex(JsArr(i))
Next i
End If
End Sub
逐语句调试时正确的,但是运行时就只能接受到fsarr数组的后四位,求大侠几点
[解决办法]
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdsend_Click()
Dim i, FsArr() As Byte
ReDim FsArr(7)
For i = 0 To 7
FsArr(i) = Val("&H" + Text发送(i))
Next i
MSComm1.Output = FsArr '发送字节数组
End Sub
Private Sub Cmdclear_Click()
Dim i
For i = 0 To 7
Text接收(i) = ""
Next i
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 6 'COM端口
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.PortOpen = True '打开端口
End Sub
Private Sub MSComm1_OnComm()
Dim i, JsArr
If MSComm1.InBufferCount > 0 Then
Sleep 30
JsArr = MSComm1.Input '从接收缓冲区取二进制数据
For i = 0 To UBound(JsArr) '显示8个接收数据
Text接收(i) = Hex(JsArr(i))
Next i
End If
End Sub