标题:vb串口通讯接收数据问题
只看楼主
剡溪渡口
Rank: 1
等 级:新手上路
帖 子:3
专家分:2
注 册:2013-11-27
 问题点数:0 回复次数:0 
vb串口通讯接收数据问题
代码如下:

Option Explicit

'********打开关闭按钮
Private Sub CmdOpen_Click()
  If CmdOpen.Caption = "Open" Then
    CmdOpen.Caption = "Close"
    MSComm.PortOpen = True
  Exit Sub
  End If
  If CmdOpen.Caption = "Close" Then
    CmdOpen.Caption = "Open"
    MSComm.PortOpen = False
  End If
End Sub

'********初始化
Private Sub Form_Load()
  Dim CardNameLen As Long
   = 3                 '使用com3口
  MSComm.InputMode = comInputModeText '文本方式读入
  MSComm.RThreshold = 1               '每到一个字节就触发一个comEvReceive事件
  MSComm.Settings = "9600,n,8,1"      '设置并返回波特率、奇偶校验、数据位、停止位参数
  CardNameLen = Len("S012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789DA")   '设置一次读入整个卡号长度
  MSComm.InputLen = CardNameLen
End Sub


Private Sub MSComm_OnComm()
  Select Case
    Case comEvReceive
      Call ReceiveDataFromCom
  End Select
End Sub

Sub ReceiveDataFromCom()
  Dim ComTemp As Variant
  Dim ReadData() As Byte
  Dim InbufferCount As Long
  Dim i As Integer
  Dim Str As String
  Str = ""
  InbufferCount = MSComm.InbufferCount     '察看接收缓冲区的字节数量
  If InbufferCount < MSComm.InputLen Then  '若接收缓冲区的字节数量小于卡号的长度,退出,什么都不做
     Exit Sub
  End If
  If InbufferCount >= MSComm.InputLen Then '若接收缓冲区的字节数量大于等于卡号的长度,读取
    ComTemp = MSComm.Input               '先读入一个变体中
    ReadData() = ComTemp                  '然后将变体赋给一个字节型数组
    For i = LBound(ReadData) To UBound(ReadData)
      Str = Str & ChrB(ReadData(i))
    Next i
    MsgBox "数据:" & vbCrLf & Str
  End If
End Sub

问题貌似出在程序无法跳至mscomm1_oncomm内
跪求大神
搜索更多相关主题的帖子: False 
2013-12-03 15:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-424628-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046383 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved