MSCOMM很奇怪的问题............200分..............可能只有高手才能解决..............200分...........
http://community.csdn.net/Expert/topic/5397/5397147.xml?temp=.4492609
上面那个问题解决后又出现了另一个奇怪的问题
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
Call SendData(bytData1) '发送第一组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,S,8,2 "
MSComm1.PortOpen = True
Call SendData(bytData2) '发送第二组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
'-------这是暂停500毫秒(0.5秒)用于解发接收事件---------
Delay 500
If Text1.Text = "0F " Then
msgbox "接收成功 "
endif
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
Call SendData(bytData1) '发送第一组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,S,8,2 "
MSComm1.PortOpen = True
Call SendData(bytData2) '发送第二组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
'-------这是暂停500毫秒(0.5秒)用于解发接收事件---------
Delay 500
If Text1.Text = "0F " Then
msgbox "接收成功 "
endif
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
Call SendData(bytData1) '发送第一组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,S,8,2 "
MSComm1.PortOpen = True
Call SendData(bytData2) '发送第二组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
'-------这是暂停500毫秒(0.5秒)用于解发接收事件---------
Delay 500
If Text1.Text = "0F " Then
msgbox "接收成功 "
endif
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
Call SendData(bytData1) '发送第一组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,S,8,2 "
MSComm1.PortOpen = True
Call SendData(bytData2) '发送第二组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
'-------这是暂停500毫秒(0.5秒)用于解发接收事件---------
Delay 500
If Text1.Text = "0F " Then
msgbox "接收成功 "
endif
由于程序需要。我连续向硬件发送5条数据。
这5条数据都会有返回值“0F”成功。“F0”失败。
代码和上面代码一样的。只是改变了一下bytData1和bytData2的值
一共五条记录。发了前四条记录没有问题。发最后一条的时候就没有返回值了。
感觉到很奇怪
我把最后一条发送语句的暂停移上了之后能收到结果。但结果是3F老问题又跟着来了。求高手解决。解决之后200分。决不食言。编人是小狗
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
Call SendData(bytData1) '发送第一组数据
MSComm1.PortOpen = False
MSComm1.Settings = "19200,S,8,2 "
MSComm1.PortOpen = True
Call SendData(bytData2) '发送第二组数据
'-------这是暂停500毫秒(0.5秒)用于解发接收事件---------
Delay 500一 '移上来
MSComm1.PortOpen = False
MSComm1.Settings = "19200,M,8,2 " '更改Settings值
MSComm1.PortOpen = True
'-------这是暂停500毫秒(0.5秒)用于解发接收事件---------
' Delay 500 (先前暂停在这里。)
If Text1.Text = "0F " Then
msgbox "接收成功 "
endif
[解决办法]
试试这样行不行:
Option Explicit
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Public Function SendData(ByRef bytData() As Byte) As Long
On Error Resume Next
MSComm1.Output = bytData
If Err.Number = 0 Then
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0
End If
SendData = Err.Number
Err.Clear
On Error GoTo 0
End Function
Private Sub MSComm1_OnComm()
Dim BytReceived As Variant
Dim i As Integer
On Error Resume Next
Select Case MSComm1.CommEvent
Case comEvReceive
BytReceived = MSComm1.Input
For i = 0 To UBound(BytReceived)
Text1.Text = Text1.Text & Right( "0 " & Hex(BytReceived(i)), 2)
Next
End Select
On Error GoTo 0
End Sub
Private Sub cmdConnect_Click()
Dim bytData1(0) As Byte
Dim bytData(21) As Byte
Dim i As Integer
bytData1(0) = 8
bytData(20) = 8
bytData(21) = 128
For i = 1 To 19
bytData(i) = 0
Next
Text1.Text = " "
MSComm1.Settings = "19200,m,8,2 "
Call SendData(bytData1)
MSComm1.Settings = "19200,s,8,2 "
Call SendData(bytData)
MSComm1.Settings = "19200,m,8,2 "
Delay 500
If Text1.Text = "0F " Then
MsgBox "接收成功 "
End If
Text1.Text = " "
MSComm1.Settings = "19200,m,8,2 "
Call SendData(bytData1)
MSComm1.Settings = "19200,s,8,2 "
Call SendData(bytData)
MSComm1.Settings = "19200,m,8,2 "
Delay 500
If Text1.Text = "0F " Then
MsgBox "接收成功 "
End If
Text1.Text = " "
MSComm1.Settings = "19200,m,8,2 "
Call SendData(bytData1)
MSComm1.Settings = "19200,s,8,2 "
Call SendData(bytData)
MSComm1.Settings = "19200,m,8,2 "
Delay 500
If Text1.Text = "0F " Then
MsgBox "接收成功 "
End If
Text1.Text = " "
MSComm1.Settings = "19200,m,8,2 "
Call SendData(bytData1)
MSComm1.Settings = "19200,s,8,2 "
Call SendData(bytData)
MSComm1.Settings = "19200,m,8,2 "
Delay 500
If Text1.Text = "0F " Then
MsgBox "接收成功 "
End If
Text1.Text = " "
MSComm1.Settings = "19200,m,8,2 "
Call SendData(bytData1)
MSComm1.Settings = "19200,s,8,2 "
Call SendData(bytData)
MSComm1.Settings = "19200,m,8,2 "
Delay 500
If Text1.Text = "0F " Then
MsgBox "接收成功 "
End If
End Sub