SComm32 Communications OCX

   







Properties/Methods

. Break
. CDHolding
. Commevent
. CommName
. CommPort
. CTSHolding
. DSRHolding
. DTREnable
. EOFChar
. EOFEnable
. EvtChar
. EvtCharEnable
. Handshaking
. InBufferCount
. InBufferSize
. Input
. InputLen
. InputMode
. NullDiscard
. OutBufferCount
. OutBufferSize
. Output
. OverlappedIO
. ParityReplace
. PortOpen
. Read
. RThreshold
. RTSEnable
. Settings
. SThreshold

OnComm Event
Hardware/Cables etc
Loopback Test Plug

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 SComm32 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( )

      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 SComm32 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( )

      Select Case SComm1.CommEvent

            Case 2   ' comEvReceive event occured

Do While SComm1.InbufferCount > 0
      Text1.Text = Text1.Text & 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