该数据长度不定,但都是以“0x1B”开头标识,第四五个字节是整个数据的总长度,最后一位是校验,我在接收的时候也能收到,但我用AccessPort查看的时候发现每次收到的不是一个完整的数据帧,而是被拆分成了几个帧,因为是需要高速连续接收的发送的,所以帧被拆分了很不好处理,效率很低,故求达人解答,提供解决方案。附上我的代码
'////////////////////////////////////////////////////////////////////
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
TxtSend = ""
txtReceive = ""
MSComm1.Settings = C2
MSComm1.CommPort = C1
MSComm1.PortOpen = True
'//////////////////////////////////////////////////////////////////////////
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
'‘ lenInput = Len(StrData)
' Text2 = lenInput \ 2
'数据处理代码
End Select
End SubPublic Function jieshou() '接收数据处理为16进制
Dim i As Integer
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
txtReceive = StrData
End Function
'////////////////////////////////////////////////////////////////////
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
TxtSend = ""
txtReceive = ""
MSComm1.Settings = C2
MSComm1.CommPort = C1
MSComm1.PortOpen = True
'//////////////////////////////////////////////////////////////////////////
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
'‘ lenInput = Len(StrData)
' Text2 = lenInput \ 2
'数据处理代码
End Select
End SubPublic Function jieshou() '接收数据处理为16进制
Dim i As Integer
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
txtReceive = StrData
End Function
解决方案 »
- 帮忙看看怎么可以读取的到这个网址的网页数据?
- 如何使用INET控件上传用公共对话框选择的文件?
- 在vb6中用Ms Winsock Control 6.0做的程序,放在windows2000 server 上运行,提示错误
- VB6.0高手请进:VB6.0中如何使点击右上角的关闭按钮时不执行unload事件?在线等待!!!
- 如何用CommonDialog显示一个只要选择路径的提示,而不是显示打开文件或者保存文件的提示?
- 兄弟们帮忙,在线等!!!关于两小程序。
- 请教:关于把xls文件存成txt文件
- vb如何访问oracle数据库???
- 为什么我编的VB程序的启动画面不能正常显示,只显示了边框,其它都是透明的?
- 如何获取当前显示分辨率?
- VB插件
- VB这个记录集如何判断··
For I = 0 To UBound(Rx_buff) '循环读取数组变量的内容
If Rx_buff(I) = 3 Then 'Chr(3)为终止符,自己可定义
RcvData = RcvData & Format(Hex(Rx_buff(I)), "00") '二进制一字节两个字符,定义格式为两位
'再判别 STX在第几位
List1.AddItem RcvData
............
.........
Else
RcvData = RcvData & IIf(Len(Hex(Rx_buff(I))) < 2, "0" & Hex(Rx_buff(I)) & " ", Hex(Rx_buff(I)) & " ")
End If
Next I
会导致接收的数据分为多帧,违背初衷了
所以我要你用上面这个For...Next代码试看看啊
2 [00000005] IOCTL_SERIAL_SET_BAUD_RATE Baud Rate: 115200
3 [00000005] IOCTL_SERIAL_SET_LINE_CONTROL StopBits: 1, Parity: No, DataBits: 8
4 [00000216] IRP_MJ_WRITE Length: 0011, Data: 0x1B 0x01 0x00 0x00 0x04 0x0E 0x02 0x86 0x00 0x01 0x95
5 [00000224] IRP_MJ_READ Length: 0009, Data: 0x1B 0x01 0x00 0x00 0x02 0x0E 0x02 0xC0 0xD4
6 [00000866] IRP_MJ_WRITE Length: 0008, Data: 0x1B 0x01 0x01 0x00 0x01 0x0E 0x06 0x12
7 [00000874] IRP_MJ_READ Length: 0015, Data: 0x1B 0x01 0x01 0x00 0x08 0x0E 0x06 0x00 0x02 0x00 0x00 0x00 0x00 0x01 0x18
8 [00000874] IRP_MJ_WRITE Length: 0011, Data: 0x1B 0x01 0x02 0x00 0x04 0x0E 0x02 0x86 0x00 0x02 0x94
9 [00000882] IRP_MJ_READ Length: 0009, Data: 0x1B 0x01 0x02 0x00 0x02 0x0E 0x02 0xC0 0xD6
10 [00001058] IRP_MJ_WRITE Length: 0011, Data: 0x1B 0x01 0x03 0x00 0x04 0x0E 0x02 0x86 0x00 0x00 0x97
11 [00001066] IRP_MJ_READ Length: 0009, Data: 0x1B 0x01 0x03 0x00 0x02 0x0E 0x02 0x00 0x17
12 [00001227] IRP_MJ_CLOSE Port Closed
////////////////////////////////////////////////////////////////////////
上面是用端口监测工具AccessPort监控到的正常收发的数据(原程序貌似是用C写的,没有源码)
下面是我用VB MSCOMM的接收数据,可以很明显的看到,总长度9个字节的帧,在接收的时候被拆分成了3个了(1+4+4),并且这个拆分好像是随机的,不固定,比较郁闷////////////////////////////////////////////////////////////////////////
1 [00000000] IRP_MJ_CREATE Port Opened - vb6.exe
2 [00000005] IOCTL_SERIAL_SET_BAUD_RATE Baud Rate: 115200
3 [00000005] IOCTL_SERIAL_SET_LINE_CONTROL StopBits: 1, Parity: No, DataBits: 8
4 [00000463] IRP_MJ_WRITE Length: 0011, Data: 0x1B 0x01 0x00 0x00 0x04 0x0E 0x02 0x86 0x00 0x01 0x95
5 [00000463] IRP_MJ_READ Length: 0001, Data: 0x1B
6 [00000463] IRP_MJ_READ Length: 0004, Data: 0x01 0x00 0x00 0x02
7 [00000463] IRP_MJ_READ Length: 0004, Data: 0x0E 0x02 0x00 0x14
8 [00001915] IRP_MJ_CLOSE Port Closed
太不合常理了, 没有终止符?是否为 Chr(13)或Chr(0)你看不到??既然没有终止符 有了起始符 长度固定 还有话说 没固定长度的话 太不合常理了串口通讯协议是有一定的 游戏规则的 否则只有累死你也搞不定
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Sub Form_Load()
C1 = "1"
C2 = "9600,N,8,1"
strBuff = ""
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.InputLen = 1
txtSend = ""
txtReceive = ""
MSComm1.Settings = C2
MSComm1.CommPort = CInt(C1)
MSComm1.PortOpen = True
End Sub'//////////////////////////////////////////////////////////////////////////
Private Sub MSComm1_OnComm() '接收数据
Dim varP As Variant
Dim intP As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.RThreshold = 0
Sleep 50 '延迟50ms,等待下位机数据完全上传
Do
varP = Null
varP = MSComm1.Input
strBuff = strBuff & Right("00" & Hex$(varP(0)), 2)
Loop Until MSComm1.InBufferCount = 0
Call subGetData
MSComm1.RThreshold = 1
End Select
End SubPrivate Sub subGetData()
Dim intStart As Integer
Dim intEnd As Integer
If strBuff = "" Then Exit Sub
If InStr(1, strBuff, "1B", vbTextCompare) = 0 Then Exit Sub
Rem 读取两个1B之间的数据, 比如:1BXXYYZZ1BKKDD,那么就取:1BXXYYZZ,剩余的下次判断再取
intStart = 0
intEnd = 0
Do
intStart = InStr(1, strBuff, "1B", vbTextCompare)
If intStart <> 0 Then intEnd = InStr(intStart, strBuff, "1B", vbTextCompare)
If intStart < intEnd Then
Debug.Print Mid(strBuff, intStart, intEnd - intStart)
strBuff = Mid(strBuff, intEnd) '去掉已处理的数据,留下未处理的数据
End If
Loop Until strBuff = "" Or intEnd = 0
End Sub
Option ExplicitDim C1 As String
Dim C2 As String
Dim strBuff As String
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Sub Form_Load()
C1 = "1"
C2 = "9600,N,8,1"
strBuff = ""
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.InputLen = 1
txtSend = ""
txtReceive = ""
MSComm1.Settings = C2
MSComm1.CommPort = CInt(C1)
MSComm1.PortOpen = True
End Sub'//////////////////////////////////////////////////////////////////////////
Private Sub MSComm1_OnComm() '接收数据
Dim varP As Variant
Dim intP As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.RThreshold = 0
Sleep 50 '延迟50ms,等待下位机数据完全上传
Do
varP = Null
varP = MSComm1.Input
strBuff = strBuff & Right("00" & Hex$(varP(0)), 2)
Loop Until MSComm1.InBufferCount = 0
Call subGetData
MSComm1.RThreshold = 1
End Select
End SubPrivate Sub subGetData()
Dim intStart As Integer
Dim intEnd As Integer
If strBuff = "" Then Exit Sub
If InStr(1, strBuff, "1B", vbTextCompare) = 0 Then Exit Sub
Rem 读取两个1B之间的数据, 比如:1BXXYYZZ1BKKDD,那么就取:1BXXYYZZ,剩余的下次判断再取
intStart = 0
intEnd = 0
Do
intStart = InStr(1, strBuff, "1B", vbTextCompare)
If intStart <> 0 Then intEnd = InStr(intStart, strBuff, "1B", vbTextCompare)
If intStart < intEnd Then
Debug.Print Mid(strBuff, intStart, intEnd - intStart)
strBuff = Mid(strBuff, intEnd) '去掉已处理的数据,留下未处理的数据
End If
Loop Until strBuff = "" Or intEnd = 0
End Sub
结贴散分,再次感谢!