帮我看看这个串口为什么只能接收4056个字节?

linzian 2007-10-29 03:24:30
帮我看看这个串口程序为什么只能接收4056个字节?不知是发送出问题还是接收出问题了
Private Sub Form_Load()
MSComm1.InputMode = 1
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.SThreshold = 0
MSComm1.RThreshold = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.CommPort = 1
MSComm1.PortOpen = True
End Sub

Private Sub Command1_Click()
Dim Data(66061) As Byte
Data(0) = &HAA
Data(1) = &HBB
Data(2) = &HCC
For i = 3 To 66061
Data(i) = Val("&H" & Hex(Int((256 * Rnd))))
Next
MSComm1.Output = Data
End Sub

Private Sub Mscomm1_Oncomm() '接收数据事件
Dim RecDatas() As Byte
If (MSComm1.CommEvent = comEvReceive) Then
Ilens = MSComm1.InBufferCount
MSComm1.InputLen = Ilens
RecDatas = MSComm1.Input
For i = 0 To UBound(RecDatas)
text1.text = text1.text & Hex(RecDatas(i)) & " "
Next i
End If
End Sub

...全文
877 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdingyun 2007-11-01
  • 打赏
  • 举报
回复
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strData As String
Dim bytInput() As Byte
Dim dataSend() As Byte
Dim sj() As Byte
Dim i As Long
Dim Ulen As Long
Dim Llen As Long
Dim for_Sum As Long
Dim Yu_sum As Integer

Private Sub Command2_Click()
ReDim dataSend(Yu_sum - 1)
For i = 0 To Yu_sum - 1
dataSend(i) = sj(512 * (for_Sum - 1) + i)
Next
MSComm1.Output = dataSend
Label2 = Time$
RichTextBox1.Visible = True
End Sub

Private Sub Command3_Click() '完整数据包
ReDim sj(26061)
Ulen = UBound(sj)
Llen = LBound(sj)
Yu_sum = (Ulen + 1) Mod 512
for_Sum = (Ulen + 1) \ 512
For i = 0 To Ulen
sj(i) = Val("&H" & Hex(Int((256 * Rnd))))
Next
Print Ulen
Print Llen
Print for_Sum
Print Yu_sum
Label1 = Time$
Timer2.Enabled = True
End Sub

Private Sub Timer2_Timer() '大数据包分解为多个小数据包
Static sum As Long
ReDim dataSend(511) As Byte
For i = 0 To 511
If sum >= for_Sum Then
Exit For
End If
dataSend(i) = sj(i + sum * 512)
Next
MSComm1.Output = dataSend
sum = sum + 1
If sum >= for_Sum Then
Command2_Click
Timer2.Enabled = False
End If
End Sub

Private Sub Form_Load()
Timer2.Interval = 10
MSComm1.Settings = "56000,n,8,1"
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Command2.Visible = False
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData) \ 2
End Function
zdingyun 2007-11-01
  • 打赏
  • 举报
回复
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
'增加接收完整数据后判别和清除strData的代码
End Select
End Sub
见'增加接收完整数据后判别和清除strData的代码,可将长度,数据头(几字节)内容判断符合,则执行如下代码:
strData = ""
MSComm1.InBufferCount = 0 '清空接受缓冲区
linzian 2007-11-01
  • 打赏
  • 举报
回复
strData是全局变量,值是一直往后增加,能否再接收完一个数据包时清空它。RichTextBox1始终显示当前接收的一个完整数据包
zdingyun 2007-11-01
  • 打赏
  • 举报
回复
Cmd_Fsend代码修改如下:

Private Sub Cmd_Fsend()
If Yu_sum > 0 Then
ReDim dataSend(Yu_sum - 1)
For i = 0 To Yu_sum - 1
dataSend(i) = sj(512 * (for_Sum - 1) + i)
Next
MSComm2.Output = dataSend
End If
Label2 = Time$
RichTextBox1.Visible = True
End Sub
zdingyun 2007-11-01
  • 打赏
  • 举报
回复
以下代码是将任意长度的数据数组分解发送

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strData As String
Dim bytInput() As Byte
Dim dataSend() As Byte
Dim sj() As Byte
Dim i As Long
Dim Ulen As Long
Dim Llen As Long
Dim for_Sum As Long
Dim Yu_sum As Integer

Private Sub Cmd_Dsend()
ReDim dataSend(Yu_sum - 1)
For i = 0 To Yu_sum - 1
dataSend(i) = sj(i)
Next
MSComm1.Output = dataSend
Label2 = Time$
RichTextBox1.Visible = True
End Sub

Private Sub Cmd_Fsend()
ReDim dataSend(Yu_sum - 1)
For i = 0 To Yu_sum - 1
dataSend(i) = sj(512 * (for_Sum - 1) + i)
Next
MSComm1.Output = dataSend
Label2 = Time$
RichTextBox1.Visible = True
End Sub

Private Sub Command3_Click() '完整数据包
ReDim sj(66061)
Ulen = UBound(sj)
Llen = LBound(sj)
Yu_sum = (Ulen + 1) Mod 512
for_Sum = (Ulen + 1) \ 512
For i = 0 To Ulen
sj(i) = Val("&H" & Hex(Int((256 * Rnd))))
Next
Print "字节长= "; Ulen + 1; "循环次数= "; for_Sum; "余数= "; Yu_sum
Label1 = Time$
If for_Sum > 0 Then
Timer1.Enabled = True
ElseIf for_Sum = 0 Then
Cmd_Dsend
End If
End Sub

Private Sub Timer2_Timer() '大数据包分解为多个小数据包
Static sum As Long
ReDim dataSend(511) As Byte
For i = 0 To 511
If sum >= for_Sum Then
Exit For
End If
dataSend(i) = sj(i + sum * 512)
Next
MSComm1.Output = dataSend
sum = sum + 1
If sum >= for_Sum Then
Cmd_Fsend
Timer2.Enabled = False
End If
End Sub

Private Sub Form_Load()
Timer2.Interval = 10
MSComm1.Settings = "56000,n,8,1"
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
'MSComm2.Settings = "56000,n,8,1"
'MSComm2.CommPort = 2
'MSComm2.RThreshold = 1
'MSComm2.PortOpen = True
Command2.Visible = False
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData) \ 2
End Function
linzian 2007-10-31
  • 打赏
  • 举报
回复
还有个问题啊,发送超过1024字节的数据包,用下面的代码接收数据,如果缓冲区超过1024的话就分批接收了,这样我Datas()数组里永远是接收的最后的一批数据。怎样判断我发过来的一个数据包有没有都接收完,使Datas()数组存下我发送一次的数据包呢
Private Sub Mscomm1_Oncomm()
Dim DataIn() As Byte
If (MSComm1.CommEvent = comEvReceive) Then
ILens = MSComm1.InBufferCount
MSComm1.InputLen = ILens
ReDim Datas(ILens) As String
DataIn = MSComm1.Input
For i = 0 To ILens - 1
Datas(i) = Hex(DataIn(i))
Next i
end sub
zdingyun 2007-10-31
  • 打赏
  • 举报
回复
设置MSComm1.Settings = "56000,n,8,1 "
不执行本机接收,最快也需12秒能传送66062字节。实际执行位15秒。
zdingyun 2007-10-31
  • 打赏
  • 举报
回复
我的机器设置MSComm1.Settings = "128000,n,8,1"
执行打开COM口报错:实时错误8015
comSetCommStateFailed 8015 不能设置 comm 状态
设置MSComm1.Settings = "56000,n,8,1"
执行打开COM口不报错。

我的理解高波特率可能另需驱动或专用硬件。
波特率最大是128000,每秒最高能送12800字节,66062字节也需5秒多,所以,你的要求确实是难以实现。建议使用并口,采用特殊方法,能提高传送速度。
zdingyun 2007-10-31
  • 打赏
  • 举报
回复
提供将数据包分解的代码:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strData As String
Dim bytInput() As Byte
Dim dataSend() As Byte
Dim sj() As Byte
Dim i As Long

Private Sub Command2_Click()
ReDim dataSend(525)
For i = 0 To 525
dataSend(i) = sj(65536 + i)
Next
MSComm2.Output = dataSend
Label2 = Time$
RichTextBox1.Visible = True
End Sub

Private Sub Command3_Click()'完整数据包
ReDim sj(66061)
For i = 0 To 66061
sj(i) = Val("&H" & Hex(Int((256 * Rnd))))
Next
Label1 = Time$
Timer2.Enabled = True
End Sub

Private Sub Timer2_Timer()'大数据包分解为多个小数据包
Static sum As Long
ReDim dataSend(511) As Byte
For i = 0 To 511
If sum >= 128 Then
'Sleep (10)
Exit For
End If
dataSend(i) = sj(i + sum * 512)
Next
MSComm2.Output = dataSend
sum = sum + 1
If sum >= 128 Then
Command2_Click
Timer2.Enabled = False
End If
End Sub

Private Sub Form_Load()
Timer1.Interval = 10
MSComm1.Settings = "56000,n,8,1"
MSComm2.Settings = "56000,n,8,1"
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
MSComm2.CommPort = 3
MSComm2.RThreshold = 1
MSComm2.PortOpen = True
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData) \ 2
End Function
linzian 2007-10-31
  • 打赏
  • 举报
回复
波特率最大是128000,每秒是16k吧,那就是要4.1秒才能发完,还要接收时间,是不是实现不了啊?
linzian 2007-10-31
  • 打赏
  • 举报
回复
你是用strData一个全局变量吧,但我要在接收第二个数据包前把strData清空,最好是放在数组里。RichtextBox1里就显示最近一次发的完整的数据包。
linzian 2007-10-31
  • 打赏
  • 举报
回复
好像不管设多大一次最多只能收1012个字节。zdingyun你可以加我吗QQ:64001958
zdingyun 2007-10-31
  • 打赏
  • 举报
回复
你使用我贴的代码,可一次发2048或4096字节长度数据不出错.

Private   Sub   Command1_Click() 
Dim Data(1093) As Byte
Data(0) = &HAA
Data(1) = &HBB
Data(2) = &HCC
For i = 3 To 1093
Data(i) = Val( "&H " & Hex(Int((256 * Rnd))))
Next
MSComm1.Output = Data
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData) \ 2
End Function
linzian 2007-10-31
  • 打赏
  • 举报
回复
MSComm1.OutBufferSize = 4096
MSComm1.InBufferSize = 4096
我把这两个都设置了,发送1904个字节还是作为两次收取的,一次1012一次892。
zdingyun 2007-10-31
  • 打赏
  • 举报
回复
出现发送大于发送缓冲区的数据,应提高发送缓冲区设置。
OutBufferSize 属性
以字节的形式设置并返回传输缓冲区的大小。
语法
object.OutBufferSize [ = value ]
OutBufferSize 属性语法包括下列部分:
部分 描述
object 对象表达式,其值是“应用于”列表中的对象。
value 整型表达式,说明传输缓冲区的大小。
说明
OutBufferSize 指整个传输缓冲区的大小:缺省值是 512 字节。不要把该属性与 OutBufferCount 属性混淆,OutBufferCount 属性返回当前在传输缓冲区等待的字节数。
注意 传输缓冲区设置的越大则应用程序可用内存越小。但若缓冲区太小,若不使用握手协议,就可能有溢出的危险。一般的规律是,首先设置一个 512 字节的缓冲区。如果出现溢出错误,则通过增加缓冲区的大小来控制应用程序的传输速率。
数据类型
Integer
linzian 2007-10-30
  • 打赏
  • 举报
回复
To zdingyun
照你的程序是可以发送的,但时间太长,66062是作为一个数据包,每5秒就要发送一个这样的包。但现在发送一个包就差不多20几秒了。
of123 2007-10-30
  • 打赏
  • 举报
回复
把接收方的接收缓冲区扩大到足以容纳你一次发送的数据的大小。
zdingyun 2007-10-30
  • 打赏
  • 举报
回复

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strData As String
Dim bytInput() As Byte
Dim dataSend() As Byte

Private Sub Command1_Click()
ReDim dataSend(2)
dataSend(0) = &HAA
dataSend(1) = &HBB
dataSend(2) = &HCC
MSComm1.Output = dataSend
Sleep (10)
Timer1.Enabled = True
Label1 = Time$
End Sub

Private Sub Command2_Click()
ReDim dataSend(7)
Dim i As Integer
For i = 0 To 7
dataSend(i) = Val("&H" & Hex(Int((256 * Rnd))))
Next
MSComm1.Output = dataSend
ReDim dataSend(2)
dataSend(0) = 255
dataSend(1) = 255
dataSend(2) = 255
MSComm1.Output = dataSend
Label2 = Time$
RichTextBox1.Visible = True
End Sub

Private Sub Form_Load()
Timer1.Interval = 6
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData) \ 2
End Function

Private Sub Timer1_Timer()
Dim i As Integer
Static sum As Integer
ReDim dataSend(255) As Byte
For i = 0 To 255
dataSend(i) = Val("&H" & Hex(Int((256 * Rnd))))
Next
MSComm1.Output = dataSend
sum = sum + 1
If sum >= 258 Then
Command2_Click
Timer1.Enabled = False
End If
End Sub
zdingyun 2007-10-30
  • 打赏
  • 举报
回复
Option Explicit
Dim strData As String
Dim bytInput() As Byte

Private Sub Command1_Click()
Timer1.Enabled = True
Label1 = Time$
End Sub

Private Sub Form_Load()
Timer1.Interval = 6
MSComm1.CommPort = 1
'MSComm2.CommPort = 2
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
'MSComm2.PortOpen = True
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData)
End Function

Private Sub Timer1_Timer()
Dim i As Integer
Static sum As Integer
Dim dataSend(255) As Byte
For i = 0 To 255
dataSend(i) = i
Next
MSComm1.Output = dataSend
sum = sum + 1
If sum > 258 Then
Label2 = Time$
Timer1.Enabled = False
End If
End Sub
zdingyun 2007-10-30
  • 打赏
  • 举报
回复
你的代码一次由FOR NEXT循环生成长66062发送数组,大大超出了发送缓冲区的能力,而且发送及接收都需一定时间,所以接收字节长度仅4096字节长,现利用TIMER计时器来控制发送节奏,已保证接收正常。
此外TEXTBOX文本框能显示的字节长度为64KB,用16进制显示需大于128KB容量,改用RichTextBox控件:
Option Explicit
Dim strData As String
Dim bytInput() As Byte

Private Sub Command1_Click()
Timer1.Enabled = True
Label1 = Time$
End Sub

Private Sub Form_Load()
Timer1.Interval = 6
MSComm1.CommPort = 1
'MSComm2.CommPort = 2
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
'MSComm2.PortOpen = True
End Sub
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
jieshou
End Select
End Sub

Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
RichTextBox1 = strData
Text1 = Len(strData)
End Function

Private Sub Timer1_Timer()
Dim i As Integer
Static sum As Integer
Dim dataSend(255) As Byte
For i = 0 To 255
dataSend(i) = i
Next
MSComm1.Output = dataSend
sum = sum + 1
If sum > 16 Then
Label2 = Time$
Timer1.Enabled = False
End If
End Sub
加载更多回复(5)

864

社区成员

发帖
与我相关
我的任务
社区描述
VB COM/DCOM/COM+
c++ 技术论坛(原bbs)
社区管理员
  • COM/DCOM/COM+社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧