发表于:2007/5/16 19:03:00
#0楼
VB发 单片机回复
F0(固定的) F0(固定的)
0F(固定的) 09(随机数)
09(要跟前一个随机数相同) 0F(固定的)
我的问题是:我发了F0、0F、随机数(09)后,只收到F0跟随机数(如09),没有收到0F。我把发送随机数的那个函数中的mscomm1.rthreshold以及inputlen改了无数次,都收不到0F。我确定把随机数发送过去了。我设置断点运行的时候可以运行到发送随机数的那个函数。
我程序如下:
dim a1 as variant
dim bytdata4(0) as variant
private sub 发送_click()
bytdata(0) = &HF0
bytdata1(0) = &H0F
call senddata(bytdata) '发送F0
call senddata1(bytdata1) '发送0F
call senddata4(bytdata4) '发送随机数
end sub
private function hex2(c as variant) as long '将随机数转为16进制数再发送
hex2 = Val("&H" + c)
if len(hex2<2) then
hex2="0" & hex2
end if
end function
public function senddata4(byref bytdata4() as variant) as long
bytdata4(0) = a1
on error resume next
MSComm1.InBufferCount = 0 '清空接收缓冲区
mscomm1.rthreshold = 5
MSComm1.Output = bytdata4(0) '发送数据
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
MSComm1.OutBufferCount = 0 '清空发送缓冲区
End Function
private sub mscomm1_oncomm()
On Error Resume Next
Dim BytReceived() As Byte
Dim strBuff As String
Dim strData As String
Dim i As Integer
Dim x As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next i
a1 = mid(strdata,1,2)
call hex2(a1)
call dataclear
end select
end sub
F0(固定的) F0(固定的)
0F(固定的) 09(随机数)
09(要跟前一个随机数相同) 0F(固定的)
我的问题是:我发了F0、0F、随机数(09)后,只收到F0跟随机数(如09),没有收到0F。我把发送随机数的那个函数中的mscomm1.rthreshold以及inputlen改了无数次,都收不到0F。我确定把随机数发送过去了。我设置断点运行的时候可以运行到发送随机数的那个函数。
我程序如下:
dim a1 as variant
dim bytdata4(0) as variant
private sub 发送_click()
bytdata(0) = &HF0
bytdata1(0) = &H0F
call senddata(bytdata) '发送F0
call senddata1(bytdata1) '发送0F
call senddata4(bytdata4) '发送随机数
end sub
private function hex2(c as variant) as long '将随机数转为16进制数再发送
hex2 = Val("&H" + c)
if len(hex2<2) then
hex2="0" & hex2
end if
end function
public function senddata4(byref bytdata4() as variant) as long
bytdata4(0) = a1
on error resume next
MSComm1.InBufferCount = 0 '清空接收缓冲区
mscomm1.rthreshold = 5
MSComm1.Output = bytdata4(0) '发送数据
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
MSComm1.OutBufferCount = 0 '清空发送缓冲区
End Function
private sub mscomm1_oncomm()
On Error Resume Next
Dim BytReceived() As Byte
Dim strBuff As String
Dim strData As String
Dim i As Integer
Dim x As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next i
a1 = mid(strdata,1,2)
call hex2(a1)
call dataclear
end select
end sub