SComm64 Communications DLL.

   




OnComm Event

If an application is waiting for data to arrive via the serial port many developers will utilize a timer event or run a continuous loop inspecting the serial port waiting for something to happen. Of course that isn't very efficient. After all,in the real world, you don't keep running to the door to see if you have a visitor - you get on with something productive (or sleep) until the doorbell rings.

The OnComm event, just like a doorbell, is triggered when the com port needs your attention.

Place the SComm64 control on your form - double click it and that will open the code window and create the template for your OnComm event. Any code you write inside the OnComm event will run every time 'something' happens at the ComPort. Here's the simplest OnComm event we could think of:-

Private Sub SComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SComm1.OnComm

      Text1.Text = SComm1.Input

End Sub

That event assumes there's data to be read from the serial port. But in fact there may have been some other reason for the event. Was it the arrival of data or perhaps an error. But at least your application didn't spend all day in a CPU intensive loop waiting for something to happen.

Because of the number of different things that can happen both MSComm32 and our SComm64 component have a property called CommEvent. Whenever an OnComm event occurs you can inspect the CommEvent property and that tells you what happened. Here's a more useful OnComm event.

Private Sub SComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SComm1.OnComm

      Select Case SComm1.CommEvent

            Case 2   ' comEvReceive event occured

Do While SComm1.InbufferCount > 0
      Text1.AppendText( SComm1.Input )
Loop


            Case Is > 1000

' The CommEvent property always returns a numerical value.
' Whenever the CommEvent property returns a number
' above 1000 then you know that an error occurred.

                   Text2.Text = "Some ComPort Error occurred"

             Case Else

' What happened? It wasn't the arrival of data - and it wasn't
' an error. See the ' CommEvent property for a full listing
' of all the events and errors.

      End Select

End Sub

In the above example we just handled two eventualities. The arrival of data or an error. Everything else would be ignored. There are quite a few different event values that can be returned by the CommEvent property as well as a number of different errors. See the CommEvent page for a full list.

 

Remarks

The values returned by the CommEvent property are numeric and there's quite a few different values so it makes your code easier to read if you use the constant names instead of the numbers.

      Select Case SComm1.CommEvent

            Case SCommLib.OnCommConstants.comEvReceive
                ' The full constant name including namespace

Do While SComm1.InbufferCount > 0
      Text1.AppendText( SComm1.Input )
Loop

etc. etc.

Note that the namespaces and syntax of our SComm64 component is exactly the same as MSComm32 except for the 'M' so when migrating any existing code from MSComm32 to SComm64 it's usually just a case of removing the 'M'.