请问串口的数据接收,是不是一定要在MSComm控件的OnComm事件里实现啊????为什么我把参数发送后,调试时,根本就进入不了OnComm事件呢?请高手指点,在线等!谢谢!!!!!谁有源码发份我,感激不尽!!

解决方案 »

  1.   

    给你一份,自己看看吧
    '收到探头温湿度命令
    uchCRCHi = &HFF
    uchCRCLo = &HFF
    If MDIFrmMain.MSComm1.InBufferCount = 9 Then '如果接收到字符
       e = MDIFrmMain.MSComm1.Input
       
       For j = 0 To 8
           data = e(j)
           uIndex = uchCRCHi Xor data
           uchCRCHi = uchCRCLo Xor CByte(auchCRCHi(uIndex))
           uchCRCLo = CByte(auchCRCLo(uIndex))
       Next j
       
       If (uchCRCHi Or uchCRCLo = 0) Then
              If e(3) * 256 + e(4) > 32767 Then
                 temp = Format(str(Val((e(3) * 256 + e(4) - 65536) / 10)), "#####.0")
              Else
                 temp = Format(str(e(3) * 256 + e(4)) / 10, "#####.0")
              End If
                 humi = Format(str(e(5) * 256 + e(6)) / 10, "#####.0")
              Select Case e(0) '收到字节中的地址码,共15个探头
                     Case "1":
                           t1 = Int(temp)
                           frmtest.Text1.Text = t1
                           h1 = Int(humi)
                           frmtest.Text2.Text = h1
                     Case "2":
                           t2 = Int(temp)
                           frmtest.Text3.Text = t2
                           h2 = Int(humi)
                           frmtest.Text4.Text = h2
                     Case "3":
                           t3 = temp
                           frmtest.Text5.Text = t3
                           h3 = humi
                           frmtest.Text6.Text = h3
                     Case "4":
                           t4 = temp
                           frmtest.Text7.Text = t4
                           h4 = humi
                           frmtest.Text8.Text = h4
                     Case "5":
                           t5 = temp
                           frmtest.Text9.Text = t5
                           h5 = humi
                           frmtest.Text10.Text = h5
                     Case "6":
                           t6 = temp
                           frmtest.Text11.Text = t6
                           h6 = humi
                           frmtest.Text12.Text = h6
                     Case "7":
                           t7 = temp
                           frmtest.Text13.Text = t7
                           h7 = humi
                           frmtest.Text14.Text = h7
                     Case "8":
                           t8 = temp
                           frmtest.Text15.Text = t8
                           h8 = humi
                           frmtest.Text16.Text = h8
                     Case "9":
                           t9 = temp
                           frmtest.Text17.Text = t9
                           h9 = humi
                           frmtest.Text18.Text = h9
                     Case "10":
                           t10 = temp
                           frmtest.Text19.Text = t10
                           h10 = humi
                           frmtest.Text20.Text = h10
                     Case "11":
                           t11 = temp
                           frmtest.Text21.Text = t11
                           h11 = humi
                           frmtest.Text22.Text = h11
                     Case "12":
                           t12 = temp
                           frmtest.Text23.Text = t12
                           h12 = humi
                           frmtest.Text24.Text = h12
                     Case "13":
                           t13 = temp
                           frmtest.Text25.Text = t13
                           h13 = humi
                           frmtest.Text26.Text = h13
                     Case "14":
                           t14 = temp
                           frmtest.Text27.Text = t14
                           h14 = humi
                           frmtest.Text28.Text = h14
                     Case "15":
                           t15 = temp
                           frmtest.Text29.Text = t15
                           h15 = humi
                           frmtest.Text30.Text = h15
                           ' 如扩展仓库4则加
                     Case "16":
                           t16 = temp
                           h16 = humi
                     Case "17":
                           t17 = temp
                           h17 = humi
                     Case "18":
                           t18 = temp
                           h18 = humi
             End Select
           lblMonitorStart.Caption = "系统已正常工作,自动检测系统每半小时会记录一次数据"
            Else
                lblMonitorStart.Caption = "CRC check error!" '校验出错
                txtTempNow1.Text = "invalid"
                txtTempNow2.Text = "invalid"
                txtTempNow3.Text = "invalid"
            '       如扩展仓库4则加
                txtTempNow4.Text = "invalid"
            
                txtHumiNow1.Text = "invalid"
                txtHumiNow2.Text = "invalid"
                txtHumiNow3.Text = "invalid"
            '       如扩展仓库4则加
                txtHumiNow4.Text = "invalid"
            End If
    Else
        lblMonitorStart.Caption = "无应答,请确认 " & tantou & "号是否存在,并检查供电或网络连接故障"  '返回信息出错
        Select Case tantou
               Case "1":
                        txtTempNow1.Text = "----"
                        txtHumiNow1.Text = "----"
                         Label1.Visible = True
               Case "2":
                        txtTempNow1.Text = "----"
                        txtHumiNow1.Text = "----"
                        Label1.Visible = True
               Case "3":
                        txtTempNow1.Text = "----"
                        txtHumiNow1.Text = "----"
                        Label1.Visible = True
               Case "4":
                        txtTempNow1.Text = "----"
                        txtHumiNow1.Text = "----"
                        Label1.Visible = True
               Case "5":
                        txtTempNow1.Text = "----"
                        txtHumiNow1.Text = "----"
                        Label1.Visible = True
               Case "6":
                        txtTempNow2.Text = "----"
                        txtHumiNow2.Text = "----"
                        Label2.Visible = True
               Case "7":
                        txtTempNow2.Text = "----"
                        txtHumiNow2.Text = "----"
                        Label2.Visible = True
               Case "8":
                        txtTempNow2.Text = "----"
                        txtHumiNow2.Text = "----"
                        Label2.Visible = True
               Case "9":
                        txtTempNow2.Text = "----"
                        txtHumiNow2.Text = "----"
                        Label2.Visible = True
               Case "10":
                        txtTempNow2.Text = "----"
                        txtHumiNow2.Text = "----"
                        Label2.Visible = True
               Case "11":
                        txtTempNow3.Text = "----"
                        txtHumiNow3.Text = "----"
                        Label3.Visible = True
               Case "12":
                        txtTempNow3.Text = "----"
                        txtHumiNow3.Text = "----"
                        Label3.Visible = True
               Case "13":
                        txtTempNow3.Text = "----"
                        txtHumiNow3.Text = "----"
                        Label3.Visible = True
               Case "14":
                        txtTempNow3.Text = "----"
                        txtHumiNow3.Text = "----"
                        Label3.Visible = True
               Case "15":
                        txtTempNow3.Text = "----"
                        txtHumiNow3.Text = "----"
                        Label3.Visible = True
               Case "16":
                        txtTempNow4.Text = "----"
                        txtHumiNow4.Text = "----"
                        Label4.Visible = True
               Case "17":
                        txtTempNow4.Text = "----"
                        txtHumiNow4.Text = "----"
                        Label4.Visible = True
               Case "18":
                        txtTempNow4.Text = "----"
                        txtHumiNow4.Text = "----"
                        Label4.Visible = True
       End Select
    End If
      

  2.   

    有那种使用MSCOMM控件完成的例子吗??
    这个太复杂,看着好乱!!!
      

  3.   

    从简单的流程讲,COMM一共分3步来做:
    1:串口初始化,包括握手方式,波特率,串口号,输出输入缓冲大小等,可以在FORM_LOAD事件中完成,不要忘记设置完成后用COMM1.PORTOPEN=TRUE来打开串口,一般可以在设置之前先判断该串口号是否已经打开,如已经打开则先关闭,然后再设置,设置完成后再打开。2:使用ON_COMM事件来获得数据,当串口发送数据或获得数据都会引发该事件,但是还有一些别的信号也会出发该事件,所以用SELECT CASE来分开这些事件,但是我们要获得串口读入的数据只需要判断COMM1.COMMEVENT=RECEIVE ,如果是串口得到数据,则COMMEVENT=RECEIVE ,此时可以将Comm1.Input赋给一个变量,就得到了来自串口的数据,当INPUT中的数据被读取后,控件会自动清空缓冲(特别是当要接收的数据超过缓冲大小的时候这点特别要注意)。整个数据通讯的过程可以反复进行,直到程序结束。3:在通讯结束后,不要忘记关闭该串口,COMM.PORTOPEN=FALSE就可以了
      

  4.   

    帮我看看代码:
    Option Explicit
    Private Declare Function GetCurrentTime Lib "kernel32" Alias "GetTickCount" () As LongPrivate Sub cmdSend_Click()
      Dim bytData(7) As Byte
      bytData(0) = &HAA
      bytData(1) = &H2
      bytData(2) = &H3
      bytData(3) = &H89
      bytData(4) = &H18
      bytData(5) = &HA
      bytData(6) = &H9A
      bytData(7) = &HBB
      
      '数据
      txtMsg.Text = SendData(1, bytData, 8)      '发送命令
    End SubPrivate Sub Form_Load()
       OpenPort 1            '打开串口
    End SubPublic Function OpenPort(PortNo As Integer, Optional InBufferSize As Integer = 1024, Optional OutBufferSize As Integer = 512) As Long
       On Error GoTo ErrExit
        MSComm1.CommPort = PortNo                   '采用COM端口
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.InputMode = comInputModeBinary      '采用二进制传输
        MSComm1.NullDiscard = False                 'NULL字符从端口传送到接受缓冲区
        MSComm1.DTREnable = False                   'DTR线无效
        MSComm1.EOFEnable = False                   '不寻找EOF符
        MSComm1.RTSEnable = False                   'RTS线无效
        MSComm1.InBufferCount = 0                   '清空接受缓冲区
        MSComm1.OutBufferCount = 0                  '清空传输缓冲区
        MSComm1.SThreshold = 1                      '如果传输缓冲区完全空时产生MSComm事件
        MSComm1.RThreshold = 0                      '不产生MSComm事件
        MSComm1.InBufferSize = InBufferSize         '接收缓冲区  默认为1024个字节
        MSComm1.OutBufferSize = OutBufferSize       '发送缓冲区  默认为512个字节
        MSComm1.PortOpen = True                     '打开端口
        OpenPort = 0
       Exit Function
    ErrExit:
       OpenPort = 1
    End FunctionPublic Sub ClosePort()
       On Error GoTo ErrExit
         MSComm1.PortOpen = False                      '关闭端口
       Exit Sub
    ErrExit:
       
    End SubPublic Function SendData(bytAddr As Byte, bytData() As Byte, Optional bytNum As Byte = 1) As Long
        On Error GoTo ErrExit    Dim bytSendArray() As Byte                     '发送数据缓冲区
        Dim intGetDataLen As Integer                   '要接收的数据长度
        Dim sngTimeSpace As Single                     '延时时间
        Dim sngTime As Single
        Dim bytReceiveArray() As Byte                  '接收的数据
        Dim VarReceiveData As Variant                  '接收的变体数据    Dim i As Long    ReDim bytSendArray(0 To bytNum) As Byte  '发送数据缓冲区    'bytSendArray(0) = &HAC                         '同步字头
        'bytSendArray(1) = bytAddr                      '下位机地址
        'bytSendArray(2) = bytAddr                      '副本
        For i = 0 To (bytNum - 1) Step 1
        bytSendArray(i) = bytData(i)
        Next
        '数据
        'For i = 0 To bytNum * 2 - 1 Step 2
        '    bytSendArray(i + 3) = bytData(i / 2)
        '    bytSendArray(i + 4) = bytData(i / 2)
        'Next    '=====================================================================================
        '信息发送
        '=====================================================================================
        MSComm1.InBufferCount = 0                      '清空接收缓冲区
        MSComm1.Output = bytSendArray                  '发送数据    Do
            DoEvents
        Loop Until MSComm1.OutBufferCount = 0          '等待,直到数据发送完毕    '=====================================================================================
        '信息接收
        '=====================================================================================    '设定要接收的数据长度
        intGetDataLen = 7    '超时时间计算:字节数×每个字节的传输时间×10             9600为波特率 请根据实际设定
        sngTimeSpace = intGetDataLen * (11000# / 9600#) * 10#    sngTime = GetCurrentTime()                          '    Do While True                                  '数据接收        DoEvents
            If MSComm1.InBufferCount >= intGetDataLen Then Exit Do        '超时处理
            If Abs(GetCurrentTime() - sngTime) > sngTimeSpace Then    '超时
                SendData = 1
                Exit Function
            End If    Loop    VarReceiveData = MSComm1.Input
        bytReceiveArray = VarReceiveData    '返回帧校验
        SendData = 2                        '先赋值接收的数据错误
        If bytReceiveArray(0) = &HAA Then   '字头
            '帧数据是否正确(正副本校验)
            If bytReceiveArray(1) = bytReceiveArray(2) And bytReceiveArray(3) = bytReceiveArray(4) Then
                '站号,命令判断
                If bytReceiveArray(1) = bytSendArray(1) And bytReceiveArray(3) = &HAA Then
                    SendData = 0                '命令正确
                End If
            End If
        End IfExit FunctionErrExit:
        SendData = 3
    End FunctionPrivate Sub Form_Unload(Cancel As Integer)
       ClosePort  '关闭串口
    End Sub
      

  5.   

    建议先用串口通讯的测试软件测试一下串口的通讯,我最近的项目也包含和工业仪器的串口通讯程序设计,只要把仪器调好了,程序的实现部分不是很难。或者你可以不用Oncomm事件,直接用Timer去读串口数据也行。
      

  6.   

    我做梦判断我的指令是否通过COMM.OUTPUT放送成功啊
      

  7.   

    最后问两个问题,怎么才能够把16进制的数据转换回ASCII啊VB里有没有EOF(异或)运算符啊这个星期内结分!!!
      

  8.   

    WINDOWS2000PRO自己帶了個超級終端機﹐可以用來測試COM的﹗