对方机器发送的数据不定长,但有开始和结束标志(均为字符“#”),
我编写了下面的程序进行接收,但显示结果和利用串口调试助手得到的16进制数据不符,
大家帮忙看看是怎么回事:其中部分代码来自以前“zdingyun”同志的帮助 Option Explicit
Dim bytInput() As Byte
Dim strData As String
Dim counterreceive%
Public Function receivedata() ‘将收到的字节数据以16进制形式显示在RichTextBox中
Dim i%
For i = 0 To UBound(bytInput)
strData = strData & Hex(bytInput(i)) & " "
Next i
RichTextBox1.Text = RichTextBox1.Text & strData
End FunctionPrivate Sub Comm1_OnComm()
Dim slen%, j%, msgvalue%
Select Case Comm1.CommEvent
Case comEvReceive
slen = Comm1.InBufferCount
ReDim bytInput(slen)
bytInput = Comm1.Input
receivedata ’调用字节数据处理过程
j = InStr(strData, "#") ’查找数据的开始和结束标志
If j > 0 Then
counterreceive = counterreceive + 1
j = j + 1
If InStr(j, strData, "#") > 0 Then
counterreceive = counterreceive + 1
End If
End If
If counterreceive = 2 Then ‘计数器值为2,即收到开始和结束标志就显示传输完毕的提示
msgvalue = MsgBox("传输完毕")
If msgvalue = vbOK Then
Comm1.PortOpen = False
RichTextBox1.SaveFile CommonDlg1.FileName, rtfText ’自动保存收到的数据
End If
Exit Sub
End If
End Select
End SubPrivate Sub Command4_Click()
Comm1.PortOpen = True
Comm1.InputMode = comInputModeBinary
Comm1.InBufferCount = 0
counterreceive = 0
CommonDlg1.FileName = "" ‘设置收到数据的保存路径
CommonDlg1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDlg1.ShowSave
End Sub
我编写了下面的程序进行接收,但显示结果和利用串口调试助手得到的16进制数据不符,
大家帮忙看看是怎么回事:其中部分代码来自以前“zdingyun”同志的帮助 Option Explicit
Dim bytInput() As Byte
Dim strData As String
Dim counterreceive%
Public Function receivedata() ‘将收到的字节数据以16进制形式显示在RichTextBox中
Dim i%
For i = 0 To UBound(bytInput)
strData = strData & Hex(bytInput(i)) & " "
Next i
RichTextBox1.Text = RichTextBox1.Text & strData
End FunctionPrivate Sub Comm1_OnComm()
Dim slen%, j%, msgvalue%
Select Case Comm1.CommEvent
Case comEvReceive
slen = Comm1.InBufferCount
ReDim bytInput(slen)
bytInput = Comm1.Input
receivedata ’调用字节数据处理过程
j = InStr(strData, "#") ’查找数据的开始和结束标志
If j > 0 Then
counterreceive = counterreceive + 1
j = j + 1
If InStr(j, strData, "#") > 0 Then
counterreceive = counterreceive + 1
End If
End If
If counterreceive = 2 Then ‘计数器值为2,即收到开始和结束标志就显示传输完毕的提示
msgvalue = MsgBox("传输完毕")
If msgvalue = vbOK Then
Comm1.PortOpen = False
RichTextBox1.SaveFile CommonDlg1.FileName, rtfText ’自动保存收到的数据
End If
Exit Sub
End If
End Select
End SubPrivate Sub Command4_Click()
Comm1.PortOpen = True
Comm1.InputMode = comInputModeBinary
Comm1.InBufferCount = 0
counterreceive = 0
CommonDlg1.FileName = "" ‘设置收到数据的保存路径
CommonDlg1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDlg1.ShowSave
End Sub
参阅上述网址,我的答贴。
Dim bytInput() As Byte
Dim strData As String
Dim counterreceive%Public Function receivedata() '将收到的字节数据以16进制形式显示在RichTextBox中
Dim i%
For i = 0 To UBound(bytInput)
strData = strData & Hex(bytInput(i)) & " "
Next i
RichTextBox1.Text = RichTextBox1.Text & strData
End FunctionPrivate Sub Comm1_OnComm()
Dim slen%, j%, msgvalue%
Dim s As Integer
Select Case Comm1.CommEvent
Case comEvReceive
s = s + 1
slen = Comm1.InBufferCount
ReDim bytInput(slen)
bytInput = Comm1.Input
receivedata '调用字节数据处理过程
j = InStr(strData, "23") '查找数据的开始和结束标志
If j > 0 Then
counterreceive = counterreceive + 1
Label1 = counterreceive
j = j + 1
'If InStr(j, strData, "#") > 0 Then
' counterreceive = counterreceive + 1
'End If
End If
If counterreceive = 2 Then '计数器值为2,即收到开始和结束标志就显示传输完毕的提示
msgvalue = MsgBox("传输完毕")
If msgvalue = vbOK Then
Comm1.PortOpen = False
RichTextBox1.SaveFile CommonDlg1.FileName, rtfText '自动保存收到的数据
End If
Exit Sub
End If
End Select
End SubPrivate Sub Form_Load()
Comm1.RThreshold = 1
Comm1.PortOpen = True
Comm1.InputMode = comInputModeBinary
Comm1.InBufferCount = 0
RichTextBox1 = ""
End Sub
Select Case Comm1.CommEvent
Case comEvReceive
tmp = Comm1.Input
bytInput = tmp
...