PC Review


Reply
Thread Tools Rate Thread

Serial Port thread

 
 
Denis R Charron
Guest
Posts: n/a
 
      15th Sep 2009
When the serial port is frozen the "Try... Catch" doesn't get the error
because the serial port is using a separate thread.

Is there a way to have the serial thread to communicate with my application
thread?


This is my code:

Try
SerialPort1.Close()
SerialPort1.Dispose()
SerialPort1 = Nothing
System.GC.Collect()
SerialPort1 = New System.IO.Ports.SerialPort
Catch ex As Exception
MessageBox.Show("Error trying to reset the port", "Freelink",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End Try



 
Reply With Quote
 
 
 
 
Chris Tacke, MVP
Guest
Posts: n/a
 
      16th Sep 2009
Which code line is "forzen"? I don't see exactly what the problem is,
likely becasue this isn't all of the code. And where is this "other thread"
you speak of?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

"Denis R Charron" <(E-Mail Removed)> wrote in message
news:A9BA8D38-7C41-4540-8CFC-(E-Mail Removed)...
> When the serial port is frozen the "Try... Catch" doesn't get the error
> because the serial port is using a separate thread.
>
> Is there a way to have the serial thread to communicate with my
> application
> thread?
>
>
> This is my code:
>
> Try
> SerialPort1.Close()
> SerialPort1.Dispose()
> SerialPort1 = Nothing
> System.GC.Collect()
> SerialPort1 = New System.IO.Ports.SerialPort
> Catch ex As Exception
> MessageBox.Show("Error trying to reset the port", "Freelink",
> MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
> MessageBoxDefaultButton.Button1)
> End Try
>
>
>


 
Reply With Quote
 
 
 
 
DickGrier
Guest
Posts: n/a
 
      16th Sep 2009
I don't understand what you are trying to do. It looks like you already
have opened a serial port, you are trying to close it, then reopen it? Why?
I don't seen anything that has much to do with threading in your post. What
you might do (IMO):

Private WithEvents SerialPort1 As New System.IO.Ports.SerialPort
'class-level code

'form code

With SerialPort1
.BaudRate = 9600 'or some other speed
.RTSEnable = True
.ComPort = "Com4" 'or, whatever
'.Handshaking = somevalueifneeded
If .IsOpen() = False Then
Try
.Open()
Catch Ex As Exception
MsgBox (ex.ToString)
End Try
If .IsOpen = True Then
EveryThingIsOk() 'perhaps
Else
PerhapsTheMsgBoxSaysWhyNot() 'etc.
End If
End If
End With

Don't close the port until you exit the application (IMO, again). Then

If SerialPort1.IsOpen() Then SerialPort.Close()

I have working example code in my book (see below) that cover a number of
scenarios -- something may be appropriate there.

If you tell use more about what you really need to do, we might be able to
offer more.

Dick

--
Richard Grier (Microsoft MVP - Visual Basic) Hard & Software 12962 West
Louisiana Avenue Lakewood, CO 80228 303-986-2179 (voice) Homepage:
www.hardandsoftware.net Author of Visual Basic Programmer's Guide to Serial
Communications, 4th Edition ISBN 1-890422-28-2 (391 pages) published July
2004, Revised July 2006.

 
Reply With Quote
 
Markus Humm
Guest
Posts: n/a
 
      16th Sep 2009
Hello,

I can tell you one case where closing and reopening of a already closed
port is necessary: HP rx1950 device after a reset. The RS232 port only
sends garbage on the first time you open it. ActiveSync must handle this
in this way as it has no trouble with it. So HP came away by saying: "we
don't support custommer written apps". Great! ;-)

Greetings

Markus
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial Port with C# serial port class problem BrianK Microsoft Dot NET Compact Framework 13 22nd Sep 2008 08:20 PM
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on Joe Microsoft C# .NET 4 12th Mar 2007 10:59 AM
Thread A calls a delegate on Thread B but Thread A executes it!?!? Paul Tomlinson Microsoft C# .NET 4 3rd Feb 2005 11:09 PM
I2C through PC parallel port, serial port and/or USB port - please help Adam DIY PC 22 29th Jun 2004 05:48 PM
Shut down thread - thread closes form, form doesn't close because thread calls it etc... Robin Tucker Microsoft VB .NET 4 17th Oct 2003 12:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:36 PM.