本帖最后由 qiuming1 于 2010-05-13 12:09:33 编辑

解决方案 »

  1.   

    LZ:你贴出的代码有问题:
    除了jieshou函数代码是完整的.
    OnComm事件代码无End Sub句
                Dim i As Integer
                Dim decSum As Integer, hexSum As String
                For i = 1 To Len(str) - 2 Step 2
                decSum = decSum + ("&H" & Mid(str, i, 2))
                Next
                decSum = decSum And 255
                hexSum = Hex(decSum)
                If hexSum = Mid(str, 17, 2) Then
                    MsgBox "正确!", vbInformation, "提示"
                Else
                    MsgBox "错误!", vbInformation, "提示"
                End If上述代码是放在哪的?
      

  2.   

    我放在oncomm事件里了 应该放哪?
      

  3.   

    Option Explicit
        Dim strSj As StringPrivate Sub Form_Load()
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.InputMode = comInputModeBinary
        MSComm1.RThreshold = 1
        MSComm1.PortOpen = True
    End SubPrivate Sub MSComm1_OnComm()
        Dim bytInput() As Byte
        Dim intInputLen As Integer
        Dim str As String
        Select Case MSComm1.CommEvent
            Case comEvReceive
                MSComm1.InputMode = comInputModeBinary
                intInputLen = MSComm1.InBufferCount
                ReDim bytInput(intInputLen)
                bytInput = MSComm1.Input
                Dim i As Integer
                For i = 0 To UBound(bytInput)
                    strSj = strSj & Right("0" & Hex(bytInput(i)), 2)
                Next
                If Mid(strSj, 1, 2) = "AA" And Len(strSj) = 18 Then
                    Dim decSum As Integer
                    Dim hexSum As String
                    For i = 1 To Len(strSj) - 2 Step 2
                        decSum = decSum + ("&H" & Mid(strSj, i, 2))
                    Next
                    decSum = decSum And 255
                    hexSum = Hex(decSum)
                    If hexSum = Mid(strSj, 17, 2) Then
                        Label1.Caption = "正确!"
                    Else
                        Label1.Caption = "错误!"
                    End If
                    strSj = ""
                End If
        End Select
    End Sub
      

  4.   

    变量定义要养成好习惯,要习惯使用Option Explicit强制定义变量你要明白的是在你定义全局变量str前,MSComm1_OnComm()中的str并不是jieshou()里的str1、定义全局变量str
    2、删除MSComm1_OnComm()中的dim str as string
    --------------------------------------------------------Dim str As StringPrivate Sub MSComm1_OnComm()
      Dim intInputLen As Integer
      Select Case MSComm1.CommEvent
      Case comEvReceive
      MSComm1.InputMode = comInputModeBinary '这句应该放在mscomm初始化里
      intInputLen = MSComm1.InBufferCount
      ReDim bytInput(intInputLen)
      bytInput = MSComm1.Input
      jieshou
      End Select
      Dim i As Integer
      Dim decSum As Integer, hexSum As String
      For i = 1 To Len(str) - 2 Step 2
      decSum = decSum + ("&H" & Mid(str, i, 2))
      Next
      decSum = decSum And 255
      hexSum = Hex(decSum)If hexSum = Mid(str, 17, 2) Then
    MsgBox "正确!", vbInformation, "提示"
    Else
    MsgBox "错误!", vbInformation, "提示"
    End Ifstr = ""End SubPublic Function jieshou()
      Dim i As Integer
      For i = 0 To UBound(bytInput)
      str = str & Right("0" & Hex(bytInput(i)), 2)
      Next
    End Function