-
Feb 15th, 2009, 07:20 PM
#1
Thread Starter
New Member
[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
-
Feb 15th, 2009, 07:26 PM
#2
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 VS2015 (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 1979, 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.
-
Feb 15th, 2009, 07:35 PM
#3
Thread Starter
New Member
Re: Program hangs when closing serial port
Tried the System.Threading.Thread.Sleep(1000) as suggested but still no go.
-
Feb 15th, 2009, 07:41 PM
#4
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 VS2015 (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 1979, 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.
-
Feb 15th, 2009, 07:53 PM
#5
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
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (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 1979, 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.
-
Feb 15th, 2009, 08:03 PM
#6
Thread Starter
New Member
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
-
Feb 15th, 2009, 08:08 PM
#7
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 VS2015 (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 1979, 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.
-
Feb 15th, 2009, 08:15 PM
#8
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 VS2015 (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 1979, 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.
-
Feb 15th, 2009, 08:23 PM
#9
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 VS2015 (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 1979, 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.
-
Feb 15th, 2009, 08:46 PM
#10
Thread Starter
New Member
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
-
Feb 15th, 2009, 08:59 PM
#11
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 VS2015 (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 1979, 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|