回复 27楼 lianyicq
已按给出的方案修正了代码并测试了,有以下问题
1:当程序初始化过程中发送查询命令时,只能接收到19K的数据,前面的8K丢失了。
2:程序运行过程中再次发送查询命令时,数据可以全部接收,但是会累计在之前的数据后面。
即重新接收数据时,在新建临时文件前的Kill "c:\temp.dat"命令没有运行。
3:Select Case id 没有运行,而且id的值也随着新数据包在变化,不能固定取值为头包中的值。
现贴出全部接收代码,请再帮忙看看
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim S, S3, s1, flag, flag1 As String
Dim id, strlen As Integer
Dim tmp As String
Dim str() As Byte
Dim temp() As Byte
Dim FileByte() As Byte
ReDim str(bytesTotal - 1)
'初始化接收缓冲区
Winsock1.getData str, vbByte + vbArray
Select Case ReceiveState
Case Is = True
If str(UBound(str)) = &HA And str(UBound(str) - 1) = &HD And str(UBound(str) - 3) = Asc("E") And str(UBound(str) - 2) = Asc("N") Then
ReDim FileByte(UBound(str) - 4)
CopyMemory FileByte(0), str(0), UBound(str) - 3
Put #1, , FileByte
ReceiveState = False
Select Case id
Case Is = 19
'当id为19时,表示接收的为设定信息
'显示信息
tmp = Mid(StrConv(1, vbUnicode), 6000, 10000)
'将全部查询数据转换为字符串,并截取中间部分
S3 = Split(tmp, "ST", , vbBinaryCompare)
'字符串分解,区分大小写
'Text4.Text = Trim(s3)
For i = 1 To UBound(S3)
s1 = Split(Split(S3(i), vbCrLf)(0), ",")(1)
'取通道标记
s1 = Trim(s1)
Text2 = Text2 + s1 + vbCrLf
Form4.CHNAME(j - 1) = Trim(s1)
Next i
End Select
Close #1
Else
Put #1, , str
End If
Case Is = False
flag = Chr(str(0)) & Chr(str(1))
Select Case flag
Case "E1"
'登录
Sleep 200
Login
'登录
log.Text = log.Text & Format(Hour(Time), "00") & ":" & Format(Minute(Time), "00") & ":" & Format(Second(Time), "00") & " " & "admin" & vbCrLf
'显示日志
Case "E0"
u = u + 1
log.Text = log.Text & Format(Hour(Time), "00") & ":" & Format(Minute(Time), "00") & ":" & Format(Second(Time), "00") & " " & strdata
'显示日志
log.SelStart = Len(log.Text)
If u = 1 Then
'
Sleep 500
log.Text = log.Text & Format(Hour(Time), "00") & ":" & Format(Minute(Time), "00") & ":" & Format(Second(Time), "00") & " " & "登录成功" & vbCrLf
'显示日志
state_lab = "TCP/IP Connect OK"
'登录成功
log.SelStart = Len(log.Text)
End If
Case "EA"
'温度值返回
S = strdata
log.Text = log.Text
'不显示日志
log.SelStart = Len(log.Text)
S3 = Split(S, "^C", , vbTextCompare)
For j = 1 To UBound(S3)
s1 = Split(S3(j), vbCrLf)(0)
s1 = Trim(s1)
If Format(s1) > Val(Form4.CH(j - 1)) Then
Form4.CH(j - 1) = Format(s1, "00.0")
End If
Next j
Case "EB"
'二进制数据返回,
If ReceiveState = False Then
Kill "c:\temp.dat"
Sleep 100
Open "c:\temp.dat" For Binary As #1
ReceiveState = True
id = str(9)
Exit Sub
End If
End Select
End Select
End Sub
[
本帖最后由 hcyang1422 于 2015-5-5 18:59 编辑 ]