Tuesday, April 6, 2010

MSComm - Serial Communication in VB

Using MSComm - programming com port in vb 6.0


In computers, a serial port is a serial communication physical interface through which information transfers in or out one bit at a time. Since the inception of personal computers serial ports are important medium of communication. For example older personal computers used serial interface for mouse, a pointing device.

Typical serial ports complies to RS-232 communications standard. RS-232 stands for Recommended Standard 232. It is a standard for serial binary single-ended data and control signals connecting between a DTE (Data Terminal Equipment) and DCE (Data Circuit-Terminating Equipment).

Serial ports use 9 pins for connectivity.
Microsoft deprecated support for the RS-232 compatible serial port of the original IBM PC design. Today, RS-232 has mostly been replaced in personal computers by USB for local communications. Compared with RS-232, USB is faster, uses lower voltages, and has connectors that are simpler to connect and use. Both standards have software support in popular operating systems. USB is designed to make it easy for device drivers to communicate with hardware. However, there is no direct analog to the terminal programs used to let users communicate directly with serial ports. USB is more complex than the RS-232 standard because it includes a protocol for transferring data to devices. This requires more software to support the protocol used. RS-232 only standardizes the voltage of signals and the functions of the physical interface pins. Serial ports of personal computers are also sometimes used to directly control various hardware devices, such as relays or lamps, since the control lines of the interface can be easily manipulated by software.

Visual Basic supports Serial port programming using the MSComm Activex Control. To use this control go to Project -> Components... select Microsft comm control 6.0 and click ok. You will a yellow color telephone icon in controls windows. double click the icon to on to your form.

The important event used by this control is 'OnComm'. This event is fired whenever a communication even occurs such and send / receive data events.

Before using com port you have to initialize the control. The following is the typical intialization properties for com port


         With MSCOMM1
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
            ' Leave all other settings as default values.
         End With

You can put the above code in the Forms load event (  Private Sub Form_Load()). Then put the following block of VB 6.0 code in the oncomm event of the control.

Private Sub MSComm1_OnComm()
         Dim InBuff As String

         Select Case MSComm1.CommEvent
         ' Handle each event or error by placing
         ' code below each case statement.

         ' This template is found in the Example
         ' section of the OnComm event Help topic
         ' in VB Help.

         ' Errors
            Case comEventBreak   ' A Break was received.
            Case comEventCDTO    ' CD (RLSD) Timeout.
            Case comEventCTSTO   ' CTS Timeout.
            Case comEventDSRTO   ' DSR Timeout.
            Case comEventFrame   ' Framing Error.
            Case comEventOverrun ' Data Lost.
            Case comEventRxOver  ' Receive buffer overflow.
            Case comEventRxParity   ' Parity Error.
            Case comEventTxFull  ' Transmit buffer full.
            Case comEventDCB     ' Unexpected error retrieving DCB]

         ' Events
            Case comEvCD   ' Change in the CD line.
            Case comEvCTS  ' Change in the CTS line.
            Case comEvDSR  ' Change in the DSR line.
            Case comEvRing ' Change in the Ring Indicator.
            Case comEvReceive ' Received RThreshold # of chars.
               InBuff = MSComm1.Input
               Call HandleInput(InBuff)
            Case comEvSend ' There are SThreshold number of
                           ' characters in the transmit buffer.
            Case comEvEOF  ' An EOF character was found in the
                           ' input stream.
         End Select

      End Sub

The 'comEvReceive' is the important event that is raised when data is received by the com port. You can use appropriate handler functions to deal with the received data. Use MsComm1.Input property to retrieve the data into a variable and manipulate the variable as per your needs. Type MSComm is used in Visual Basic applications to handle weigh machines (in weigh bridge applications), bar code reading devices, IVRS (Interactive Voice Response) Systems and other devices that use serial interface.