| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 409 人关注过本帖
标题:请大家帮我看看串口接收程序吧!总是校验的时候容易掉数据!抓狂了!
只看楼主 加入收藏
SSbccn
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-7-11
收藏
 问题点数:0 回复次数:0 
请大家帮我看看串口接收程序吧!总是校验的时候容易掉数据!抓狂了!
我用的波特率为57600
采样的周期为10ms,
程序如下:

Public Event MyOnComm()

Public cSetting As String
Public nPortNum As Long
Public b收到一组 As Boolean
Public b收发完成 As Boolean

Public aInputstr As Variant
Public n等待时间 As Long
Public a参数 As Variant
Public b数据错误 As Boolean
Public b通信正常 As Boolean
Public b保存记录 As Boolean
Public nRecNum As Long  '收发的数据计数
Public nLastRecTime As Long
Public n命令字 As Byte
Public nC51等待时间 As Long

Dim aRecStr(1999) As String  '收发的数据
Dim ch() As Byte
Dim ch_z(2000) As Byte
Dim ch_m(16) As Byte
Dim cCode() As Byte
Dim chData() As Byte
Dim chi As Long
Dim oBz As PictureBox
Dim ch_n As Long
Dim ch_i As Long
Dim m, y As Integer

Dim WithEvents oC51 As MSComm
Private Sub oC51_OnComm()
    Dim Buffer As Variant
    Dim k As Long
    Dim lni As Long
    Dim nPlc_Rec_Len As Long
   
'    Select Case
'        Case comEvReceive

        Buffer = oC51.Input

        If UBound(Buffer) < 900 And chi < 8096 Then
        
            ch = Buffer
            For k = LBound(Buffer) To UBound(Buffer)
                ch_z(chi) = ch(k)
                chi = chi + 1
            Next k
            frmMain.Text7.Text = CStr(chi)
            y = y + 1
            frmMain.Text8.Text = CStr(y)
 
            If chi > 6 Then
                Call p收到数据检查
            End If
            
            If chi = 1 Then
                b收到一组 = True
                n等待时间 = 0
                b通信正常 = True
                b数据错误 = False

                ReDim chData(0) As Byte
                chData(0) = ch_z(0)
                chi = 0
                aInputstr = chData
                RaiseEvent MyOnComm
            End If
        Else
            chi = 0
        End If
'
'        Case comEvSend
'        ' 错误
'        Case comEventBreak                              ' 收到 Break。
'        Case comEventCDTO                               ' CD (RLSD) 超时。
'        Case comEventCTSTO                              ' CTS Timeout。
'        Case comEventDSRTO                              ' DSR Timeout。
'        Case comEventFrame                              ' Framing Error
'        Case comEventOverrun                            '数据丢失。
'             MsgBox "与串口通信时,数据丢失!", vbInformation, App.Title
'        Case comEventRxOver                             '接收缓冲区溢出。
'             MsgBox "与串口通信时,接收缓冲区溢出!", vbInformation, App.Title
'        Case comEventRxParity                           ' Parity 错误。
'        Case comEventTxFull                             '传输缓冲区已满。
'        Case comEventDCB                                '获取 DCB] 时意外错误
'
'        ' 事件
'        Case comEvCD                                    ' CD 线状态变化。
'        Case comEvCTS                                   ' CTS 线状态变化。
'        Case comEvDSR                                   ' DSR 线状态变化。
'        Case comEvRing                                  ' Ring Indicator 变化。
'        Case comEvEOF                                   ' 输入数据流中发现 EOF 字符
'    End Select

End Sub

Private Sub p收到数据检查()
    Dim lni As Long
    Dim k As Long
    Dim CRC As Long
    Dim i As Long
   
    If chi = 16 Then
        ch_n = 1
        Do While ch_n <= chi - 2 And Not (ch_z(ch_n) = &HCC And ch_z(ch_n + 1) = &H33 And ch_z(ch_n + 2) = &HC3)
            ch_n = ch_n + 1
            DoEvents
        Loop
   
        If ch_n < chi - 2 Then    '收到一个帧结尾
            If ch_z(0) <> &HAA Then    '不是一个完整的帧开头
'                lni = 1
'                Do While ch_z(lni) <> &HAA And lni <= chi - 2
'                    lni = lni + 1
'                Loop
'                If ch_z(lni) = &HAA Then
'                    For k = lni To chi
'                        ch_z(k - lni) = ch_z(k)
'                    Next k
'                    chi = chi - lni
'                End If
            End If
            If ch_z(0) = &HAA Then     '是一个完整的帧
    '            lni = 1
    '            Do While ch_z(lni) <> &HAA And lni < ch_n - 2
    '                lni = lni + 1
    '            Loop
    '            If ch_z(lni) = &HAA Then
    '                For k = lni To chi
    '                    ch_z(k - lni) = ch_z(k)
    '                Next k
    '                chi = chi - lni
    '                ch_n = ch_n - lni
    '            End If
   
                CRC = 0
                For lni = 1 To ch_n - 3         '算出校验和
                    CRC = CRC + ch_z(lni)
                Next lni
               
                ReDim chData(ch_n) As Byte
               
                If CRC Mod 256 = ch_z(ch_n - 1) Then       '数据完整     校验和正确
                    For lni = 0 To ch_n
                        chData(lni) = ch_z(lni)
                    Next lni
                    aInputstr = chData
                    m = m + 1
                    frmMain.Text4.Text = CStr(m)
                    chi = chi - ch_n - 3
                    If chi > 0 Then
                        chi = 0
    '                    For lni = 0 To chi
    '                        ch_z(lni) = ch_z(lni + ch_n + 2)
    '                    Next lni
                    End If
                    b收到一组 = True
                    n等待时间 = 0
                    b通信正常 = True
                    b数据错误 = False
                    nLastRecTime = GetTickCount
                    DoEvents
                    RaiseEvent MyOnComm
                Else
                    For lni = 0 To ch_n
                        chData(lni) = 5
                    Next lni
                    aInputstr = chData
                    chi = chi - ch_n - 3
                    If chi > 0 Then
                        chi = 0
    '                    For lni = 0 To chi
    '                        ch_z(lni) = ch_z(lni + ch_n + 2)
    '                    Next lni
                    End If
                    b收到一组 = True
                    n等待时间 = 0
                    b通信正常 = True
                    b数据错误 = False
                    nLastRecTime = GetTickCount
                    RaiseEvent MyOnComm
                End If
            Else
                chi = 0 '放弃该帧
            End If
        End If
    ElseIf chi = 32 Then
        ch_n = 16
        Do While ch_n <= chi - 2 And Not (ch_z(ch_n) = &HCC And ch_z(ch_n + 1) = &H33 And ch_z(ch_n + 2) = &HC3)
            ch_n = ch_n + 1
            DoEvents
        Loop

        If ch_n < chi - 2 Then    '收到一个帧结尾
            If ch_z(ch_n - 13) <> &HAA Then  '不是一个完整的帧开头
'                lni = 1
'                Do While ch_z(lni) <> &HAA And lni <= chi - 2
'                    lni = lni + 1
'                Loop
'                If ch_z(lni) = &HAA Then
'                    For k = lni To chi
'                        ch_z(k - lni) = ch_z(k)
'                    Next k
'                    chi = chi - lni
'                End If
            End If
            If ch_z(ch_n - 13) = &HAA Then   '是一个完整的帧
                CRC = 0
                For lni = 17 To chi - 6         '算出校验和
                    CRC = CRC + ch_z(lni)
                Next lni

                ReDim chData(ch_n - 14) As Byte

                If CRC Mod 256 = ch_z(chi - 4) Then       '数据完整     校验和正确
                    For lni = 0 To 15
                        chData(lni) = ch_z(lni + 16)
                    Next lni
                    aInputstr = chData

                    chi = chi - ch_n - 3
                    If chi > 0 Then
                        chi = 0
    '                    For lni = 0 To chi
    '                        ch_z(lni) = ch_z(lni + ch_n + 2)
    '                    Next lni
                    End If
                    b收到一组 = True
                    n等待时间 = 0
                    b通信正常 = True
                    b数据错误 = False
                    nLastRecTime = GetTickCount
                    DoEvents
                    RaiseEvent MyOnComm
                Else
                    For lni = 17 To ch_n - 6
                        chData(lni) = 5
                    Next lni
                    aInputstr = chData
                    chi = chi - ch_n - 3
                    If chi > 0 Then
                        chi = 0
    '                    For lni = 0 To chi
    '                        ch_z(lni) = ch_z(lni + ch_n + 2)
    '                    Next lni
                    End If
                    b收到一组 = True
                    n等待时间 = 0
                    b通信正常 = True
                    b数据错误 = False
                    nLastRecTime = GetTickCount
                    RaiseEvent MyOnComm

                End If
            Else
                chi = 0 '放弃该帧
            End If
        End If
    End If
End Sub
现在的现象是10000个数据,递增方式传输给我,就是说1,2,3,4,5,6……,9998,9999,10000
但是传输的数据到数组Data(10000)中时,Data的值总是有跳动的数字,比如Data(1)=2,Data(3)=5,为什么?
2013-07-11 17:34
快速回复:请大家帮我看看串口接收程序吧!总是校验的时候容易掉数据!抓狂了!
数据加载中...
 
   



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

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