Serial Visual Basic .net

Serial-port-vb.doc 1 2/1/2010 Serial Port Using Visual Basic. NET and Windows Introduction The serial COM port is one of the simplest ways to communicate.

More events can be executed on the same thread. This is the method used by the UI. All UI related actions gets

translated to a message. Each message has a name, which starts with WM_ Windows Message like WM_CLICK, and it has an

associated method, which shall be called when the message is handled. This method has a similar name except that WM_

is replaced with On like OnClick. Very urgent messages like mouse cursor movements WM_SETCURSOR and change of

active window WM_ACTIVATE and WM_SETFOCUS are handled immediately, but most messages are linked together with other

messages to form a message queue.

It is possible to post up to 10,000 messages to one queue. With the exception of the WM_PAINT message, the WM_TIMER

message, and the WM_QUIT message, the system always posts messages at the top of a message queue by means of a

pointer, which points to the previous message. This ensures that a window receives its input messages in the proper

first in, first out FIFO sequence. The WM_PAINT message, the WM_TIMER message, and the WM_QUIT message, however, are

kept in the queue and are only handled when the queue contains no other messages. In

addition, multiple WM_PAINT messages for the same window are combined into a single WM_PAINT message, consolidating

all invalid parts of the client area into a single area. Combining WM_PAINT messages reduces the number of times a

window must redraw the contents of its client area.

The routine that reads from the message queue is called the message pump. It s basically an almost infinite loop

that waits for someone to post a message to the queue by means of GetMessage as shown below:

Once someone post a message, GetMessage returns with the value true. The loop then performs any message

translations by means of TranslateMessage. For example, the keyboard sends raw, virtual-key messages like WM_KEYDOWN

and WM_KEYUP. These virtual-key messages contain a code that identifies which key was pressed or released, but not its

character value, which might depend on the state of the Shift or Alt key or the character Language. TranslateMessage

converts the virtual-key codes into a more user friendly character message WM_CHAR. At last, the message pump calls

DispatchMessage, which calls the message handling procedure - WndProc - of the control window specified in the message. Note that in case of one

of the four thread safe methods - Invoke, BeginInvoke, EndInvoke and CreateGraphics, it is WhdProc of the control you have used in the call, which

will receive the message - for example Button2.WndProc in case of Button2.BeginInvoke - even though Button2 may have absolutely nothing to do with

WndProc is basically a Select-case construction, which calls the method associated with the message like this:

Protected Overridable Sub WndProc ByRef m As System.Windows.Forms.Message

Case WM_ERASEBKGND Paint background

Case WM_PAINT Paint foreground

Case WindowsForms20_ThreadCallbackMessage

Me.InvokeMarshaledCallbacks Empty the ThreadCallbackList of the control

RaiseEvent Closing Old obsolete closing event

RaiseEvent FormClosing New 2.0 form closing event

DestroyWindow m.hWnd Close specified window/form

PostQuitMessage 0 Stop the message pump and terminate the application

Protected Overridable Sub OnClick ByVal e As System.Windows.Forms.BulletedListEventArgs

RaiseEvent Click e Execute all event handlers for the Click event

Protected Overridable Sub OnPaint ByVal e As System.Windows.Forms.PaintEventArgs

Do whatever painting it has to do

RaiseEvent Paint e Execute all event handlers for the Paint event

The PaintEventArgs specifies the Graphics to use to paint the control and the Rectangle, which needs to be

re painted. This is used in both OnPaint and OnPaintBackground.

The 20 in WindowsForms20_ThreadCallbackMessage depends on the version - 11 for. NET 1.1 and 20 for version 2.0.

WindowsForms20_ThreadCallbackMessage is described in more details in the control.Invoke/BeginInvoke chapter, but it

should be noted that these kind of messages have higher priority than input messages like WM_KEYUP and WM_KEYDOWN.

This means that if a program keeps sending such messages to the message queue, the window will not react on e.g.

keyboard input. Therefore it is very important not to overload the message queue with these kind of messages.

In. NET 2.0, the form closing event, which has only CancelEventArgs, is obsolete and replaced with the FormClosing

event, which inherits from Closing, but also has information about the reason why the form is closed CloseReason

property. Note that both events are raised before the form is closed so that it is possible to cancel the operation

Note that very urgent messages like WM_SETCURSOR are send directly to WndProc bypassing the message queue.

When the associated method like OnClick or OnPaint has done whatever it has to do, it must

call any other methods, which shall also be executed when this event occurs. These methods are called event handlers,

and the method, which executes the event handlers, is RaiseEvent. It is a very confusing name because it makes you

believe that it raises a new event like raising a flag or an interrupt. It doesn t. It just execute some event

handlers from a list, but it is a convenient way of describing things. You write some event handlers and when you want

to execute these, you just raise the event. The event has a name similar to the message and

the associated method except that no prefix is used like for example just Click. When the method and any event handler s

have finished their jobs, DispatchMessage returns and the loop calls GetMessage again to wait for the next message.

When GetMessage receives a WM_QUIT message posted with PostQuitMessage by means of WM_DESTROY it returns with the

value false and the While loop exits.

Let us take an example. If the user moves or resizes a window, some part of the window - a rectangle, a region or

maybe the whole window - needs to be redrawn. The method, which has moved or resized the windows, tells which part

it is by calling Invalidate. However, there may be other UI events, which has occured before the window has been

changed and also needs to be executed, so the redraw is not done immediately. Instead, Invalidate sends the messages

WM_ERASEBKGND and WM_PAINT with the necessary PaintEventArgs to the message queue. When WM_ERASEBKGND

reaches the bottom of the queue, the background is repainted by means of the OnPaintBackground method and when

WM_PAINT reaches the buttom and there are no other messages in the queue except for WM_QUIT as described previously,

WM_PAINT, WM_TIMER and WM_QUIT have lower priority than all other messages, the OnPaint method is executed and

will do whatever foreground painting it needs to do. Note that the time difference between background paint and

foreground paint will course flicker if you don t use double buffering - especially because of the low priority of

WM_PAINT. When WM_PAINT is finished, it will raise the Paint event, that is, execute all handlers

for this event. If you for example has created graphics with CreateGraphics, this graphics may need to be redrawn so you must

make a handler for the Paint event, which can do this.

The OnPaint method and any other methods associated with a message are overridable so that they may be changed if you derive a class from

another. If you for example want to add something to the OnPaint method of the class ChildClass, which is derived from ParentClass, you can

Protected Overrides Sub OnPaint ByVal e As System.Windows.Forms.PaintEventArgs

MyBase.OnPaint e Call the original OnPaint method to do the rest and at last raise the Paint event

This is the preferred method rather than making some routines in ChildClass, which subscribe to the Paint event of ParentClass.

All applications or services, which use ChildClass, can then subscribe to the Paint event of this class if they have any graphics to redraw

in case the window is invalidated.

Because the message pump is used to handle events for the GUI, it runs on the Main thread, on which all GUI objects

are created. A typical GUI application, after doing some initialization in the Main method, will then start running

the message pump. Because the message pump is an infinite loop it will run until the application closes, so the Main

thread can t do anything else once the message pump starts.

The System.Windows.Forms.Application.Run method takes care of message pumping. If no form is specified, it begins

running a standard application message loop on the current thread usually the UI-thread for the application or

service. If a form is specified, it also makes the specified form visible like:

Shared Sub Main Called from the primary thread

Application.EnableVisualStyles

Application.SetCompatibleTextRenderingDefault false

Application.Run New Form1 Start the application and make Form1 visible

In case of an application, a form is usually specified or else there is no UI the user can click, but it is also

possible to show the form later by means of NameOfForm.Show if an event to do this is available. If you want to hide a

form, you can call NameOfForm.Hide. This will set the Visible property to false. If you want to close a

form, you can call NameOfForm.Close. This will post a WM_CLOSE message, which will close the form.

When you want to terminate your application for example by selecting Exit from the File menu, clicking on the

close button the small X button in the upper right corner of your window, or presses Alt F4, you must call

Application.Exit. This call sends a WM_DESTROY message to the message queue, which then closes all windows/forms and

at last post a WM_QUIT message, which stops the message pump and exits the application as explained before.

Each application can have many UI-threads, which again can have many windows, but the thread that creates a window

must also be the thread that handles all messages send to that window. That means that any thread that creates GUI

objects Windows must have a message pump so that it can receive the messages.

All the controls in a single window must be created on that windows UI thread, but if you have multiple top-level

windows, you can have multiple UI threads - as many as you have windows if you really want. More UI threads in a

application is for example used by Internet Explorer and when a MessageBox pops up. It has the advantage that if one window

is blocked the others may still be functional.

Nov 26, 2007  Describes how to access serial and parallel ports by using Visual Basic. NET. This article also provides code samples to show how to perform this task.

serial visual basic .net serial visual basic .net

Home Articles Access Serial Ports with Visual Basic. NET. Access Serial Ports with Visual Basic. NET. This article originally appeared in the April 2008 Nuts Volts.

serial visual basic .net

Video embedded  Intro: Serial Port Programming With. NET. Serial ports provide an easy way to communicate between many types of hardware and your computer.

Enumeration: In. NET it is possible to assign values to names with the structure GroupName.Member like this: Public Enum MyColors MistyRose HE1E4FF.

Serial Communication with Visual Basic .NET

serial visual basic .net

Dec 06, 2006  This article documents how to access serial ports by using the Visual Basic 2005 My.Computer.Ports object.

serial visual basic .net