vb串口接收数据的奇怪问题

Like_328 2009-06-02 03:35:01
我现在做一个简单串口通信的小测试,遇到一奇怪问题,一直想不通,望各位帮忙看一下,代码如下:
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "38400,n,8,1"
.OutBufferCount = 0
.InBufferCount = 0
.InputMode = 1
.RThreshold = 1
.SThreshold = 0
.OutBufferSize = 512
.InBufferSize = 1024
.InputLen = 0
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim I As Integer
Dim strbuff As String
Dim indata As String
Dim inbte() As Byte
' Dim reclen As Integer
Select Case MSComm1.CommEvent
Case 2
strbuff = MSComm1.Input
' reclen = MSComm1.InBufferCount
inbte() = strbuff
For I = 0 To UBound(inbte)
If Len(Hex(inbte(I))) = 1 Then
indata = indata & "0" & Hex(inbte(I))
Else
indata = indata & Hex(inbte(I))
End If
Next I

Text1.Text = indata
End Select
End Sub
我用串口调试助手发送数据,把收到的数据显示在Text中,以十六进制发送
比如发送:A5 67 35 31 31 30 90 01 03 A0
但Text中只收到了01 03 A0
如果发送时把01 03 A0去掉,就可以收到了A5 67 35 31 31 30 90
我试过好多次都是这样,好像一次最多只能接收8位数似的
望高手看一下问题出在哪了
...全文
237 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2009-06-03
  • 打赏
  • 举报
回复
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "38400,n,8,1"
.OutBufferCount = 0
.InBufferCount = 0
.InputMode = comInputModeBinary
.RThreshold = 10
.SThreshold = 0
.OutBufferSize = 512
.InBufferSize = 1024
.InputLen = 0
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim I As Integer
Dim strbuff As Variant
Dim indata As String
Dim inbte() As Byte

Select Case MSComm1.CommEvent
Case 2
strbuff = MSComm1.Input
inbte = strbuff
For I = 0 To UBound(inbte)
indata = indata & Right("0" & Hex(inbte(I)), 2)
Next I

Text1.Text = indata
End Select
End Sub
zdingyun 2009-06-03
  • 打赏
  • 举报
回复
串口通信线的接法:
2个RS232口间
2-3
3-2
5-5
连接
各自RS232口
4-6
7-8
短接
上述做法不考虑握手.
Like_328 2009-06-03
  • 打赏
  • 举报
回复
我试了一下还是不行,第一次只收到前面8位,再发下一组数据时,先收到的是第一次没接收完的数据
simon__sun 2009-06-02
  • 打赏
  • 举报
回复
.Settings = "38400,n,8,1" 改成 .Settings = "9600,n,8,1" 试试

贝隆 2009-06-02
  • 打赏
  • 举报
回复
我也试了一下,楼主的代码能用
给你一个带源码的串口调试器,你单步调试一下看看。
http://download.csdn.net/source/1262066
zdingyun 2009-06-02
  • 打赏
  • 举报
回复
LZ:你的代码没有问题,请查找其它方面的原因,串口助手与PC机的VB代码是否设置一致.此外你的通信线是如何接的?
Option Explicit
Dim indata As String
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "38400,n,8,1"
.OutBufferCount = 0
.InBufferCount = 0
.InputMode = comInputModeBinary
.RThreshold = 1
.SThreshold = 0
.OutBufferSize = 512
.InBufferSize = 1024
.InputLen = 0
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim I As Integer
Dim strbuff As String
Dim inbte() As Byte
Select Case MSComm1.CommEvent
Case 2
strbuff = MSComm1.Input
inbte() = strbuff
For I = 0 To UBound(inbte)
If Len(Hex(inbte(I))) = 1 Then
indata = indata & "0" & Hex(inbte(I))
Else
indata = indata & Hex(inbte(I))
End If
Next I
Text1.Text = indata
End Select
End Sub
贝隆 2009-06-02
  • 打赏
  • 举报
回复
把InputLen修改为:1
贝隆 2009-06-02
  • 打赏
  • 举报
回复
把InputLen修改为:1
Like_328 2009-06-02
  • 打赏
  • 举报
回复
只收到03 A0
Like_328 2009-06-02
  • 打赏
  • 举报
回复
不长啊,我两台电脑紧挨着,应该不是这个问题,我刚才又试了一下,还是只受到01 03 A0,唉
cageling 2009-06-02
  • 打赏
  • 举报
回复
RS232支持的串口线不能太长
Like_328 2009-06-02
  • 打赏
  • 举报
回复
那怎么回事啊,我这边总是不对啊
cageling 2009-06-02
  • 打赏
  • 举报
回复
运行环境:windowsXP+SP3+ VB6+SP6
用串口调试助手V2.2 + 用你的代码帮你测了下,可以接收到全部的A56735313130900103A0
不存在你说的问题...

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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