本人vb初学者,有个问题希望高手看一下:
现在做个通过串口采集下位仪表的小程序,单步运行的时候能采集到值,可是全部运行就出现错误:实时错误 '5'
                无效的过程调用或参数然后我点“调试”按钮,就进入到:Function RecvCmdCom(buf As String) As String
    
    Dim strRead As String
    Dim intBeginBit As Integer
    Dim intEndBit As Integer
    
    intBeginBit = InStr(1, buf, ",")
    intEndBit = InStr(intBeginBit, buf, ":")
    strRead = Mid(buf, intBeginBit + 1, intEndBit - intBeginBit - 1)
    
    RecvCmdCom = Str(Val("&H" & strRead))
    
End Function光标停在strRead = Mid(buf, intBeginBit + 1, intEndBit - intBeginBit - 1)
这个语句上。请问各位高手,这个这么解决呢?是什么原因?

解决方案 »

  1.   

    Function RecvCmdCom(buf As String) As StringDim strRead As String
    Dim intBeginBit As Integer
    Dim intEndBit As IntegerintBeginBit = InStr(1, buf, ",")'这句加个判断
    if intBeginBit = 0 then '如果buf 中没有"," ,那么
    RecvCmdCom=""
    exit function
    end if
    intEndBit = InStr(intBeginBit, buf, ":")'这句加个判断
    if intEndBit = 0 then '如果buf 中没有":" ,那么
    RecvCmdCom=""
    exit function
    end if
    strRead = Mid(buf, intBeginBit + 1, intEndBit - intBeginBit - 1)RecvCmdCom = Str(Val("&H" & strRead))End Function