[RESOLVED] Program hangs when closing serial port-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] Program hangs when closing serial port

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Posts
    6

    Resolved [RESOLVED] Program hangs when closing serial port

    I have a VB.Net 2008 app that I have converted from VB6 which reads data from a weight scale connected to a serial port. I am able to read the received data and display it on the screen, but when I go to Stop the reading and attempt to close the serial port, the app hangs.


    Private Delegate Sub ShowWeight(ByVal tWeight As String)

    Private Sub comScale_DataReceived(ByVal sender As Object, ByVal e
    As System.IO.Ports.SerialDataReceivedEventArgs) Handles
    comScale.DataReceived
    ReadWeight(comScale.ReadLine())
    End Sub

    Private Sub ReadWeight(ByVal tWeight As String)
    On Error GoTo ErrScale

    If TextBox1.InvokeRequired Then
    TextBox1.Invoke(New ShowWeight(AddressOf ReadWeight), tWeight)
    Else
    TextBox1.Text = tWeight
    End If
    strInput = TextBox1.Text
    End Sub


    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal
    eventArgs As System.EventArgs) Handles Command1.Click
    Select Case Command1(Index).Tag
    Case "Stop"
    RemoveHandler comScale.DataReceived, AddressOf
    comScale_DataReceived
    If comScale.IsOpen Then comScale.Close() 'HANGS HERE

    Case "Start"
    If comScale.IsOpen Then comScale.Close()
    Call Get_ComPort() 'Set Port Properties
    AddHandler comScale.DataReceived, AddressOf
    comScale_DataReceived 'add handler
    comScale.Open()
    End Select
    End Sub


    Many Thanks for any help

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    Try inserting a delay between removing the handler and the comScale.Close

    Code:
    RemoveHandler comScale.DataReceived, AddressOf comScale_DataReceived
    System.Threading.Thread.Sleep(1000)
    If comScale.IsOpen Then comScale.Close()


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Posts
    6

    Re: Program hangs when closing serial port

    Tried the System.Threading.Thread.Sleep(1000) as suggested but still no go.

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    There's an article on MSDN here http://social.msdn.microsoft.com/for...-8a4ee7b210e2/ which suggests this is a known problem A few hints but no definitive solution.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    The article suggests you close the port in the FormClosing event. So if you change your program temporarily to exit before the close, and in the closing event close the port, does it still hang?

    Code:
    RemoveHandler comScale.DataReceived, AddressOf comScale_DataReceived
    System.Threading.Thread.Sleep(1000)
    Application.Exit
    And in the FormClosing event
    Code:
    comScale.Close()


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Posts
    6

    Re: Program hangs when closing serial port

    Tried placing the port close in the Form.Closed and still hangs. When I pause it the Green arrow points to the close function

  7. #7
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    Another thread here http://social.msdn.microsoft.com/For...-c562bff11a0a/ where the user appears to have overcome this.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  8. #8
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    And yet another MSDN thread suggests this;

    Code:
    RemoveHandler comScale.DataReceived, AddressOf comScale_DataReceived
    Application.DoEvents() 
    System.Threading.Thread.Sleep(1000)
    comScale.Close()


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  9. #9
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    And one more http://social.msdn.microsoft.com/For...-5667730313ee/ suggesting a soft close method.

    And another http://social.msdn.microsoft.com/For...-e2ff1d3be0a5/

    All of these Ive posted seem to be resolved by the user.

    p.s. Isnt MSDN great!


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Posts
    6

    Thumbs up Re: Program hangs when closing serial port

    Changing
    TextBox1.Invoke(New ShowWeight(AddressOf ReadWeight), tWeight)

    to

    TextBox1.BeginInvoke(New ShowWeight(AddressOf ReadWeight), tWeight)

    Worked like a charm.

    Thanks to all for your help. Was really stuck on this

  11. #11
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,654

    Re: Program hangs when closing serial port

    Excellent.

    In that case please mark the thread resolved.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2005/8 Standard (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.