2008-10-14
评论3
楼主 2007/12/21 22:39:19
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte '定义动态数组
aa(0) = &HC
aa(1) = &H3
aa(2) = &H0
aa(3) = &H32
aa(4) = &H0
aa(5) = &H4
CRC = CRC16(aa)
str = CRC
s = ""
For i = 1 To LenB(str)
s = s + Hex(AscB(MidB(str, i, 1)))
Next i
bb = Right(s, 2)
cc = Mid(s, 1, 2)
If Len(s) < 4 Then
cc = Mid(s, 1, 1)
End If
ReDim Preserve aa(0 To 7) As Byte
aa(6) = Val("&H" & bb)
aa(7) = Val("&H" & cc)
MSComm1.OutBufferCount = 0 '清空输出寄存器
MSComm1.Output = aa
FGetData = ReceiveData
Text5.Text = FGetData
p11 = Val("&H" & Mid(FGetData, 7, 4))
p12 = Val("&H" & Mid(FGetData, 11, 4))
p13 = Val("&H" & Mid(FGetData, 15, 4))
p14 = Val("&H" & Mid(FGetData, 19, 4))
End Sub
Private Sub Command4_Click() 'I状态
Dim CRC() As Byte
Dim FGetData As String
Dim aa() As Byte
Dim s As String
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte '定义动态数组
aa(0) = &HC
aa(1) = &H2
aa(2) = &H0
aa(3) = &H0
aa(4) = &H0
aa(5) = &H1
CRC = CRC16(aa)
str = CRC
s = ""
For i = 1 To LenB(str)
s = s + Hex(AscB(MidB(str, i, 1)))
Next i
bb = Right(s, 2)
cc = Mid(s, 1, 2)
If Len(s) < 4 Then
cc = Mid(s, 1, 1)
End If
ReDim Preserve aa(0 To 7) As Byte
aa(6) = Val("&H" & bb)
aa(7) = Val("&H" & cc)
MSComm1.OutBufferCount = 0 '清空输出寄存器
MSComm1.Output = aa
FGetData = IReceiveData
p17 = Val("&H" & Mid(FGetData, 7, 2))
p18 = Val("&H" & Mid(FGetData, 9, 2))
End Sub
Private Sub Command5_Click() 'Q状态
Dim CRC() As Byte
Dim FGetData As String
Dim aa() As Byte
Dim s As String
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte '定义动态数组
aa(0) = &HC
aa(1) = &H1
aa(2) = &H0
aa(3) = &H0
aa(4) = &H0
aa(5) = &H1
CRC = CRC16(aa)
str = CRC
s = ""
For i = 1 To LenB(str)
s = s + Hex(AscB(MidB(str, i, 1)))
Next i
bb = Right(s, 2)
cc = Mid(s, 1, 2)
If Len(s) < 4 Then
cc = Mid(s, 1, 1)
End If
ReDim Preserve aa(0 To 7) As Byte
aa(6) = Val("&H" & bb)
aa(7) = Val("&H" & cc)
MSComm1.OutBufferCount = 0 '清空输出寄存器
MSComm1.Output = aa
FGetData = QReceiveData
p15 = Val("&H" & Mid(FGetData, 7, 2))
p16 = Val("&H" & Mid(FGetData, 9, 2))
End Sub
Private Function ReceiveData() As String '返回V存储器区数据
Dim FGetData As String
Dim t1 As Long
Dim av As Variant
Dim i As Integer
Dim ReDataLen As Integer
FGetData = ""
t1 = GetTickCount() '取时间,做延时用
Do '循环等待接收数据
DoEvents
If MSComm1.InBufferCount > 0 Then '串口有数据了
ReDataLen = MSComm1.InBufferCount '取数据长度
av = MSComm1.Input '将串口数据取出来
For i = 0 To ReDataLen - 1
FGetData = FGetData & Right("00" & Hex(av(i)), 2)
Next i
End If
If Len(FGetData) >= 6 Then
If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
ReceiveData = FGetData
Exit Function
End If
End If
If GetTickCount - t1 > 2000 Then '2秒没收完就不收了
ReceiveData = ""
Exit Function
End If
Loop
End Function
Private Function IReceiveData() As String '反回I状态
Dim FGetData As String
Dim t1 As Long
Dim av As Variant
Dim i As Integer
Dim ReDataLen As Integer
FGetData = ""
t1 = GetTickCount() '取时间,做延时用
Do '循环等待接收数据
DoEvents
If MSComm1.InBufferCount > 0 Then '串口有数据了
ReDataLen = MSComm1.InBufferCount '取数据长度
楼主 2007/12/21 22:39:55
av = MSComm1.Input '将串口数据取出来
For i = 0 To ReDataLen - 1
FGetData = FGetData & Right("00" & Hex(av(i)), 2)
Next i
End If
If Len(FGetData) >= 6 Then
If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
IReceiveData = FGetData
Exit Function
End If
End If
If GetTickCount - t1 > 2000 Then '2秒没收完就不收了
IReceiveData = ""
Exit Function
End If
Loop
End Function
Private Function QReceiveData() As String '反回Q状态
Dim FGetData As String
Dim t1 As Long
Dim av As Variant
Dim i As Integer
Dim ReDataLen As Integer
FGetData = ""
t1 = GetTickCount() '取时间,做延时用
Do '循环等待接收数据
DoEvents
If MSComm1.InBufferCount > 0 Then '串口有数据了
ReDataLen = MSComm1.InBufferCount '取数据长度
av = MSComm1.Input '将串口数据取出来
For i = 0 To ReDataLen - 1
FGetData = FGetData & Right("00" & Hex(av(i)), 2)
Next i
End If
If Len(FGetData) >= 6 Then
If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
QReceiveData = FGetData
Exit Function
End If
End If
If GetTickCount - t1 > 2000 Then '2秒没收完就不收了
QReceiveData = ""
Exit Function
End If
Loop
End Function
Private Sub Timer2_Timer()
x1 = x1 + 1
Text10.Text = str(x1)
Text1.Text = str(p11)
Text2.Text = str(p12)
Text3.Text = str(p13)
Text4.Text = str(p14)
Text7.Text = str(p15)
If Text7.Text = 1 Then Text7.BackColor = RGB(255, 0, 255)
If Text7.Text = 0 Then Text7.BackColor = RGB(0, 255, 255)
'Text6.Text = str(p16)
Text9.Text = str(p17)
If Text9.Text = 1 Then Text9.BackColor = RGB(255, 0, 255)
If Text9.Text = 0 Then Text9.BackColor = RGB(0, 255, 255)
'Text8.Text = str(p18)
End Sub
楼主 2008/10/14 12:35:38
thankyou