whay my app stop after a time??

Hello VB programers. I make a project in VB.net (2003).(the app communicate with a board...send/recive strings)...and, after a time...the application closed(whithouth reason). WHAY



Answer this question

whay my app stop after a time??

  • SHG

    That code looks to be quite a mess... There are several instances of objects that I can only assume are defined elsewhere. The problem could be burried anywhere... this is what happens when you pick up someone else's old code...

    If you've got two seperate programs both trying to access the same COM port, you're going to have issues. It can only be in use by one app. From your description it sounds as if this other app does not properly release the port.

    If you were to redo this in VS05, it doesn't look like you'd need any of this code.



  • NathanCaaaaaaaan

    I have to make some assumptions here, but since this is a VS03 app that 'send/receive strings with a board', in all likelyhood you are using the AxComm component - and probably improperly. Somewhere at a lowlevel the AxComm component is failing - maybe due to improper buffer usage - and this is crashing the application.

    Nearly any managed code error will result in an exception being raised. The fact that no exception is raised prior to the app terminating unexpectadly points heavily toward some failure of a COM object.

    I'd look closely at your "communication" code and doublecheck the usage of any COM objects in the app.

    I don't know how extensive this app is but you might try recreating it in Visual Studio 2005, where you have a managed SerialPort component to work with.



  • lushdog

    There's no way to answer this question with the information you gave us. Start debugging...


  • Bhaskar Sarma

    for a while i stay in vb.net 2003. I review the code ho managed serial communication for 100 times....nothing ...but not give-up. Anything has a solution. This code help's me because contain...on anouther part...the TAPI function...on my project i need that....sow...i prefer to stay in 2003.

  • Jan-Dirk

    my app just close....but not every time...for exemple...yestarday morning was closing withouth reason...but on the evening he worck's fine. i ask because...maybe...somebody has encountered this situation....i now that withouth the board...nobody can't reproduce......
    even i (with the board)...can't say were is the problemm....he just close...withouth anouther message....strange


  • Sylvain in Ottawa

    I can't garuantee that this'll help but try anyway:

    In the solution explorer, double-click 'My Project'

    Under 'Shutdown Mode', select 'When last form closes'

    Hope that helps!



  • AndyMills

    Yes Kimble, you have right …

    But I fink I’m discover the problem.
    I use another program ho communicate with same board…and I let this program open…of course I close the handle of the com port…and I connect with my app….then my app crush after I try to “read/write”…using the same com port. In this situation…I don’t now how to solve the problem …. I don’t want to recreate in vb 2005 …

    I give the code ho managed the serial communication:

    (ofcorse...not all)

    Public Property Port() As Integer

    Get

    Return miPort

    End Get

    Set(ByVal Value As Integer)

    miPort = Value

    End Set

    End Property

    Public Sub PurgeBuffer(ByVal Mode As PurgeBuffers)

    'This method will clear any character into buffer, use TxAbort/RxAbort

    ' to terminate any pending overlapped Tx/Rx operation.


    If (mhRS.ToInt32 > 0) Then PurgeComm(mhRS, Mode)

    End Sub

    Public Overridable Property Timeout() As Integer

    Get

    Return miTimeout

    End Get

    Set(ByVal Value As Integer)

    miTimeout = CInt(IIf(Value = 0, 500, Value))

    '// If Port is open updates it on the fly

    pSetTimeout()

    End Set

    End Property

    Public Property Parity() As DataParity

    Get

    Return meParity

    End Get

    Set(ByVal Value As DataParity)

    meParity = Value

    End Set

    End Property

    Public Property StopBit() As DataStopBit

    Get

    Return meStopBit

    End Get

    Set(ByVal Value As DataStopBit)

    meStopBit = Value

    End Set

    End Property

    Public Property BaudRate() As Integer

    Get

    Return miBaudRate

    End Get

    Set(ByVal Value As Integer)

    miBaudRate = Value

    End Set

    End Property

    Public Property Trimis() As Integer

    Get

    Return mitrimis

    End Get

    Set(ByVal Value As Integer)

    mitrimis = Value

    End Set

    End Property

    Public Property DataBit() As Integer

    Get

    Return miDataBit

    End Get

    Set(ByVal Value As Integer)

    miDataBit = Value

    End Set

    End Property

    Public Property BufferSize() As Integer

    Get

    Return miBufferSize

    End Get

    Set(ByVal Value As Integer)

    miBufferSize = Value

    End Set

    End Property

    Public Overloads Sub Open()

    '// Get Dcb block,Update with current data

    Dim uDcb As DCB, iRc As Int32

    '// Set working mode

    meMode = Mode.Overlapped

    Dim iMode As Int32 = Convert.ToInt32(IIf(meMode = Mode.Overlapped, FILE_FLAG_OVERLAPPED, 0))

    '// Initializes Com Port

    If miPort > 0 Then

    Try

    '// Creates a COM Port stream handle

    mhRS = CreateFile("\\.\COM" & miPort.ToString, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, iMode, 0)

    If (mhRS.ToInt32 > 0) Then

    '// Clear all comunication errors

    Dim lpErrCode As Int32

    iRc = ClearCommError(mhRS, lpErrCode, New COMSTAT)

    '// Clears I/O buffers

    iRc = PurgeComm(mhRS, PurgeBuffers.RXClear Or PurgeBuffers.TxClear)

    '// Gets COM Settings

    iRc = GetCommState(mhRS, uDcb)

    '// Updates COM Settings

    Dim sParity As String = "NOEM"

    sParity = sParity.Substring(meParity, 1)

    '// Set DCB State

    Dim sDCBState As String = String.Format("baud={0} parity={1} data={2} stop={3}", miBaudRate, sParity, miDataBit, CInt(meStopBit))

    iRc = BuildCommDCB(sDCBState, uDcb)

    uDcb.Parity = CByte(meParity)

    '// Set Xon/Xoff State

    If mbUseXonXoff Then

    uDcb.Bits1 = 768

    Else

    uDcb.Bits1 = 0

    End If

    iRc = SetCommState(mhRS, uDcb)

    If iRc = 0 Then

    Dim sErrTxt As String = New Win32Exception().Message

    Throw New CIOChannelException("Unable to set COM state " & sErrTxt)

    End If

    '// Setup Buffers (Rx,Tx)

    iRc = SetupComm(mhRS, miBufferSize, miBufferSize)

    '// Set Timeouts

    pSetTimeout()

    '//Enables events if required

    If mbEnableEvents Then Me.EnableEvents()

    Else

    '// Raise Initialization problems

    Dim sErrTxt As String = New Win32Exception().Message

    Throw New CIOChannelException("Unable to open COM" + miPort.ToString + ControlChars.CrLf + sErrTxt)

    End If

    Catch Ex As Exception

    '// Generica error

    Throw New CIOChannelException(Ex.Message, Ex)

    End Try

    Else

    '// Port not defined, cannot open

    Throw New ApplicationException("COM Port not defined,use Port property to set it before invoking InitPort")

    End If

    End Sub

    Public Overloads Sub Open(ByVal Port As Integer, ByVal BaudRate As Integer, ByVal DataBit As Integer, ByVal Parity As DataParity, ByVal StopBit As DataStopBit, ByVal BufferSize As Integer)

    Me.Port = Port

    Me.BaudRate = BaudRate

    Me.DataBit = DataBit

    Me.Parity = Parity

    Me.StopBit = StopBit

    Me.BufferSize = BufferSize

    Open()

    End Sub

    Public Sub Close()

    If mhRS.ToInt32 > 0 Then

    If mbEnableEvents = True Then

    Me.DisableEvents()

    End If

    Dim ret As Boolean = CloseHandle(mhRS)

    If Not ret Then Throw New Win32Exception

    mhRS = New IntPtr(0)

    End If

    End Sub

    ReadOnly Property IsOpen() As Boolean

    Get

    Return CBool(mhRS.ToInt32 > 0)

    End Get

    End Property

    Public Overloads Sub Write(ByVal Buffer As Byte())

    mitrimis = 1

    Dim iRc, iBytesWritten As Integer, hOvl As GCHandle

    muOvlW = New Overlapped

    If mhRS.ToInt32 <= 0 Then

    Throw New ApplicationException("Please initialize and open port before using this method")

    Else

    '// Creates Event

    Try

    hOvl = GCHandle.Alloc(muOvlW, GCHandleType.Pinned)

    muOvlW.hEvent = CreateEvent(Nothing, 1, 0, Nothing)

    If muOvlW.hEvent.ToInt32 = 0 Then Throw New ApplicationException("Error creating event for overlapped writing")

    '// Clears IO buffers and sends data

    iRc = WriteFile(mhRS, Buffer, Buffer.Length, 0, muOvlW)

    If iRc = 0 Then

    If Marshal.GetLastWin32Error <> ERROR_IO_PENDING Then

    Throw New ApplicationException("Write command error")

    Else

    '// Check Tx results

    If GetOverlappedResult(mhRS, muOvlW, iBytesWritten, 1) = 0 Then

    Throw New ApplicationException("Write pending error")

    Else

    '// All bytes sent

    If iBytesWritten <> Buffer.Length Then Throw New ApplicationException("Write Error - Bytes Written " & iBytesWritten.ToString & " of " & Buffer.Length.ToString)

    End If

    End If

    End If

    Finally

    '//Closes handle

    CloseHandle(muOvlW.hEvent)

    If (hOvl.IsAllocated = True) Then hOvl.Free()

    End Try

    End If

    End Sub

    Public Overloads Sub Write(ByVal Buffer As String)

    mitrimis = 1

    Dim oEncoder As New System.Text.ASCIIEncoding

    Dim oEnc As Encoding = oEncoder.GetEncoding(1252)

    Dim aByte() As Byte = oEnc.GetBytes(Buffer)

    Me.Write(aByte)

    End Sub

    Public Sub Write_string(ByVal Buffer As String)

    mitrimis = 1

    Dim oEncoder As New System.Text.ASCIIEncoding

    Dim oEnc As Encoding = oEncoder.GetEncoding(1252)

    Dim aByte() As Byte = oEnc.GetBytes(Buffer)

    Me.Write(aByte)

    End Sub

    Public Function Read(ByVal Bytes2Read As Integer) As Integer

    Dim iReadChars, iRc As Integer, bReading As Boolean, hOvl As GCHandle

    If Bytes2Read = 0 Then Bytes2Read = miBufferSize

    muOvlR = New Overlapped

    If mhRS.ToInt32 <= 0 Then

    Throw New ApplicationException("Please initialize and open port before using this method")

    Else

    '// Get bytes from port

    Try

    hOvl = GCHandle.Alloc(muOvlR, GCHandleType.Pinned)

    muOvlR.hEvent = CreateEvent(Nothing, 1, 0, Nothing)

    If muOvlR.hEvent.ToInt32 = 0 Then Throw New ApplicationException("Error creating event for overlapped reading")

    '// Clears IO buffers and reads data

    ReDim mabtRxBuf(Bytes2Read - 1)

    iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read, iReadChars, muOvlR)

    If iRc = 0 Then

    If Marshal.GetLastWin32Error() <> ERROR_IO_PENDING Then

    Throw New ApplicationException("Read pending error")

    Else

    '// Wait for characters

    iRc = WaitForSingleObject(muOvlR.hEvent, miTimeout)

    Select Case iRc

    Case WAIT_OBJECT_0

    '// Some data received...

    If GetOverlappedResult(mhRS, muOvlR, iReadChars, 0) = 0 Then

    Throw New ApplicationException("Read pending error.")

    Else

    Return iReadChars

    End If

    Case WAIT_TIMEOUT

    Throw New IOTimeoutException("Read Timeout.")

    Case Else

    Throw New ApplicationException("General read error.")

    End Select

    End If

    Else

    Return (iReadChars)

    End If

    Finally

    '//Closes handle

    CloseHandle(muOvlR.hEvent)

    If (hOvl.IsAllocated) Then hOvl.Free()

    End Try

    End If

    End Function

    Overridable ReadOnly Property InputStream() As Byte()

    Get

    Return mabtRxBuf

    End Get

    End Property

    Overridable ReadOnly Property InputStreamString() As String

    Get

    Dim oEncoder As New System.Text.ASCIIEncoding

    Dim oEnc As Encoding = oEncoder.GetEncoding(1252)


    If Not Me.InputStream Is Nothing Then Return oEnc.GetString(Me.InputStream)

    End Get

    End Property

    Public Sub ClearInputBuffer()

    If mhRS.ToInt32 > 0 Then

    PurgeComm(mhRS, PURGE_RXCLEAR)

    End If

    End Sub

    Public WriteOnly Property Rts() As Boolean

    Set(ByVal Value As Boolean)

    If mhRS.ToInt32 > 0 Then

    If Value Then

    EscapeCommFunction(mhRS, Lines.SetRts)

    Else

    EscapeCommFunction(mhRS, Lines.ClearRts)

    End If

    End If

    End Set

    End Property

    Public WriteOnly Property Dtr() As Boolean

    Set(ByVal Value As Boolean)

    If mhRS.ToInt32 > 0 Then

    If Value Then

    EscapeCommFunction(mhRS, Lines.SetDtr)

    Else

    EscapeCommFunction(mhRS, Lines.ClearDtr)

    End If

    End If

    End Set

    End Property

    Public Sub EnableEvents()

    If mhRS.ToInt32 <= 0 Then

    Throw New ApplicationException("Please initialize and open port before using this method")

    Else

    If moEvents Is Nothing Then

    mbEnableEvents = True

    moEvents = New Thread(AddressOf pEventsWatcher)

    moEvents.IsBackground = True

    moEvents.Start()

    End If

    End If

    End Sub

    Public Sub DisableEvents()

    If mbEnableEvents = True Then

    SyncLock Me

    mbEnableEvents = False '// This should kill the thread

    End SyncLock

    '// Let WaitCommEvent exit...

    If muOvlE.hEvent.ToInt32 <> 0 Then SetEvent(muOvlE.hEvent)

    moEvents = Nothing

    End If

    End Sub

    Public Property RxBufferThreshold() As Int32

    Get

    Return miBufThreshold

    End Get

    Set(ByVal Value As Int32)

    miBufThreshold = Value

    End Set

    End Property

    Public Shared Function IsPortAvailable(ByVal portNumber As Int32) As Boolean

    If portNumber <= 0 Then

    Return False

    Else

    Dim cfg As COMMCONFIG

    Dim cfgsize As Int32 = Marshal.SizeOf(cfg)

    cfg.dwSize = cfgsize

    Dim ret As Boolean = GetDefaultCommConfig("COM" + portNumber.ToString, cfg, cfgsize)

    Return ret

    End If

    End Function

    Public Sub SetBreak()

    If mhRS.ToInt32 > 0 Then

    If SetCommBreak(mhRS) = False Then Throw New Win32Exception

    End If

    End Sub

    Public Sub ClearBreak()

    If mhRS.ToInt32 > 0 Then

    If ClearCommBreak(mhRS) = False Then Throw New Win32Exception

    End If

    End Sub

    Public ReadOnly Property InBufferCount() As Int32

    Get

    Dim comStat As COMSTAT

    Dim lpErrCode As Int32

    Dim iRc As Int32

    comStat.cbInQue = 0

    If mhRS.ToInt32 > 0 Then

    iRc = ClearCommError(mhRS, lpErrCode, comStat)

    Return comStat.cbInQue

    End If

    Return 0

    End Get

    End Property

    .......................
    this code was created by Corrado Cavalli in 2001...
    ....................



  • h1

    I can send'u the project. just email-me (timofte_gabriel@yahoo.com)(but i don't now if u can help'me....just email'me and i will explain to you what button i press...and whatt answer i recive...and then is close -whith no logic reason-).

    I debug ...but i don't find were is the problem(i don't use any instruction to close app). (now i run my app...and worcks fine...but 4 hours ago...he was closing withouth reason)


  • Gizmox

    So from your descption - the app communicates with a board. If I dont have the board how am I meant to repro the scenario

    Has anything been written to any of the event logs, does the machine restart/crash or does you application just close.


  • whay my app stop after a time??