当前位置:编程学习 > VB >>

运行VB工程没问题,但是生成EXE后运行,报内存错误~~

程序是通过摄像机返回的图像识别车牌,然后把车牌通过串口发送到IDE上显示,下面是代码,cpsb_HavePlates是识别到车牌的事件,sendPlateToLED是
发送车牌到IED,在运行生成的EXE的时候如果我把sendPlateToLED (plateName)屏蔽没问题,如果我加上的话就报内存错误,直接运行工程都没问题。。。
Private Sub cpsb_HavePlates(ByVal strPicName As String, ByVal strPlateName As String, ByVal strPlateColor As String, ByVal trPlateType As String)
    Dim picName, plateName, plateColor, plateType, driection As String
    driection = cpsb.GetDriection
    If driection = "向上行驶" Or driection = "向左行驶" Then
     Exit Sub
     Else
    isOK = False
        plateName = strPlateName
        Label3.Caption = plateName
        'Label3.Caption = driection
        plate = plateName
        sendPlateToLED (plateName)
   End If
End Sub
Private Function sendPlateToLED(ByVal plateName As String)
   On Error GoTo myerr
   Dim outByte(15) As Byte
   Dim dataByte() As Byte
   Dim dataLen As Integer
   Dim checknum, index As Integer
    checknum = 0
    
   If MSComm1.PortOpen = False Then
         MSComm1.CommPort = comPort
        ' 9600 波特,无奇偶校验,8 位数据,一个停止位。
        MSComm1.Settings = "9600,N,8,1"
        
        ' 打开端口。
       MSComm1.PortOpen = True
   MSComm1.InputMode = comInputModeBinary
   End If
   ' 告诉控件读入整个缓冲区。
   MSComm1.InputLen = 0
   MSComm1.InBufferCount = 0
   MSComm1.OutBufferCount = 0
   '生成数据包
   dataByte = StrConv(plateName, vbFromUnicode)
   dataLen = UBound(dataByte) + 4
   outByte(0) = &HAA
   outByte(1) = &H55
   outByte(2) = Val("&h" & Hex(dataLen))
   outByte(3) = &H0
   outByte(4) = &H70
   index = 5
   For i = 0 To UBound(dataByte)
    outByte(i + 5) = dataByte(i)
    index = index + 1
   Next
   For i = 2 To UBound(outByte)
    checknum = checknum + outByte(i)
   Next
   outByte(index) = Val("&h" & Right(Hex(checknum), 2))
   outByte(index + 1) = Val("&h" & Left(Hex(checknum), 1))
   
   '发送数据包。
   MSComm1.Output = outByte
   Sleep (200)
   Call openLED
   Command1.Enabled = True
   Timer1.Enabled = True
   Exit Function
myerr:
   MsgBox "向LED发送文本出错:" + Err.Description
End Function  --------------------编程问答-------------------- 自己顶。。。 --------------------编程问答-------------------- LZ:你的问题在BAIDU中午就看到了,单独调试你的sendPlateToLED函数代码未见出错,编译成EXE也未报错:
Private Function sendPlateToLED(ByVal plateName As String)
    On Error GoTo myerr
    Dim outByte(15) As Byte
    Dim dataByte() As Byte
    Dim dataLen As Integer
    Dim checknum, index As Integer
    checknum = 0
    If MSComm1.PortOpen = False Then
         MSComm1.CommPort = 1 'comPort
        ' 9600 波特,无奇偶校验,8 位数据,一个停止位。
        MSComm1.Settings = "9600,N,8,1"
        
        ' 打开端口。
        MSComm1.PortOpen = True
    MSComm1.InputMode = comInputModeBinary
    End If
    ' 告诉控件读入整个缓冲区。
    MSComm1.InputLen = 0
    MSComm1.InBufferCount = 0
    MSComm1.OutBufferCount = 0
    '生成数据包
    dataByte = StrConv(plateName, vbFromUnicode)
    dataLen = UBound(dataByte) + 4
    outByte(0) = &HAA
    outByte(1) = &H55
    outByte(2) = Val("&h" & Hex(dataLen))
    outByte(3) = &H0
    outByte(4) = &H70
    index = 5
    Dim i As Long
    For i = 0 To UBound(dataByte)
     outByte(i + 5) = dataByte(i)
     index = index + 1
    Next
    For i = 2 To UBound(outByte)
     checknum = checknum + outByte(i)
    Next
    outByte(index) = Val("&h" & Right(Hex(checknum), 2))
    outByte(index + 1) = Val("&h" & Left(Hex(checknum), 1))
    
    '发送数据包。
    MSComm1.Output = outByte
    'Sleep (200)
    'Call openLED
    Command1.Enabled = True
    Timer1.Enabled = True
    Exit Function
myerr:
    MsgBox "向LED发送文本出错:" + Err.Description
End Function

Private Sub Command1_Click()
    Call sendPlateToLED(strplateName)
End Sub

Private Sub Form_Load()
    strplateName = "123456"
End Sub
--------------------编程问答-------------------- 我知道单独不会出错,但是cpsb_HavePlates是个OCX控件的事件,在里面调用就会出错。。。 --------------------编程问答--------------------
Private Sub cpsb_HavePlates(ByVal strPicName As String, ByVal strplateName As String, ByVal strPlateColor As String, ByVal trPlateType As String)
    Dim picName, plateName, plateColor, plateType, driection As String
    driection = cpsb.GetDriection
    If driection = "向上行驶" Or driection = "向左行驶" Then
        Exit Sub
    Else
        isOK = False
        plateName = strplateName
        Label3.Caption = plateName
        'Label3.Caption = driection
        plate = plateName
        Call sendPlateToLED(plateName)
   End If
End Sub

在sendPlateToLED (plateName)前加Call

--------------------编程问答--------------------
引用 4 楼 zdingyun 的回复:
在sendPlateToLED (plateName)前加Call



没解决问题. --------------------编程问答-------------------- 主要是你们没法调试得,我在想,是不是在用户自定义事件里面调用了其它函数,会出现问题。。。 --------------------编程问答-------------------- 用户自定义事件里面调用了其它函数不会出现问题,lz的代码需要调试才能确定问题所在,感觉是cpsb_HavePlates函数调用前有影响后面运行的代码。vb ide环境中运行工程时属于debug环境,编译后代码优化了可能影响某些函数特别是和硬件接口函数性能。 --------------------编程问答-------------------- 这个难于判断,我觉得可能是硬件驱动的问题。 --------------------编程问答-------------------- 主要是别人的OCX,我也不知道cpsb_HavePlates是咋写的,而且运行这个OCX还要加密狗。。。。纠结。。。
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,