Damons Laboratory
    The most exciting place on the internet!


A Science & Engineering OnLine Laboratory Notebook   
This is the laboratory notebook of Damon Bruccoleri.  Here you will find engrossing, thoughtful and fun commentary/opinion.  Leave a comment and let others know what you think about any post here, view my photo gallery, or sign my guestbook.

"...one of the strongest motives that lead men to art and science is escape from everyday life with its painful crudity and hopeless dreariness, from the fetters of one's own ever-shifting desires. A finely tempered nature longs to escape from the personal life into the world of objective perception and thought." - Albert Einstein



Latest Notebook Entries

 Thursday, February 01, 2007


Using the Serial Port in Visual Basic 2005 event driven   
In Visual Basic 6.0 handling a serial port (or comm port) event was simpler than in Visual Basic 2005. That is because in .net 2.0 the event exists on a secondary thread. You can't access any objects ( timers or GUI elements) from the event because they exist on the primary UI thread. To do this you need to make an asynchronous call to a delegate procedure from the comm port event.  The following procedure works for all of Visual Studio 2005.

If your like me you have been searching for a solution to this issue. I hope you stumbled here and this helps. Use this:

Delegate Sub UpdateUIHandler()

Sub UpdateUI_Impl()
' delegate to update user interface controls from primary UI thread
' put any code to update the UI from serial data here

End Sub

Private Sub SerialPort_DataReceived(ByVal sender As Object,  _
      ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)_
      Handles SerialPort.DataReceived
      ' this is the serial port data received event on a secondary thread

      Dim buffer As String
      Dim handler As New UpdateUIHandler(AddressOf UpdateUI_Impl)

      buffer = SerialPort.ReadExisting
      ' invoke delegate on primary UI thread 
      Me.BeginInvoke(handler)
End Sub


admin4 at 3:28 PM | (8) Comments | Add a comment | Permalink





Commments
James said...

I suggest that you write a book on engineering. Your blog is sounding more and more technical and difficult to follow.

Thursday, February 01, 2007 9:44 PM

Damon said...

Sorry for that James. I try to keep the lab notes easy to read and follow generally. I just want to see if I get any hits from this off the search engines.

Friday, February 02, 2007 7:09 AM

Mark Paulson said...

How can I access the My object from C#?

Wednesday, February 07, 2007 2:22 PM

Ravel Huing said...

can't understand it

Saturday, March 10, 2007 2:07 PM

Damon said...

I believe this only works from VB Mark. I believe that Microsoft is helping Intel to promote multi-processor architecture. They are doing this by FORCING programmers to use and understand the language facilitites for multi-processor processing. They could have made serial-port processing easier but did not so you would learn multi-processor architecture. .... IMHO.

Monday, March 12, 2007 7:02 AM

Anthony said...

Thanks for the easy to read example. It took me a while to find something I could understand! My program finally works, YAY!

Thursday, November 08, 2007 7:34 PM

jmosr said...

please explain how to update a variable i can use everywhere in my program

Wednesday, June 25, 2008 3:41 AM

Damon said...

Just use the code as shown above. In the method UpdateUI_Impl use the data stored in the 'buffer' variable.

Wednesday, June 25, 2008 7:22 AM

  Leave a comment

Enter Your Name:
 
Enter your website
Security code from image below:  
Need Custom Controls Work or Training?