Microsoft KB Archive/194922

= INFO: Receiving Data Using the MSComm Control's OnComm Event =

Article ID: 194922

Article Last Modified on 4/22/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q194922



SUMMARY
This article demonstrates a basic template for using an event driven data receiving routine with the OnComm event. This is the recommended method of receiving all types of data using the MSComm control. For information on converting binary data, please see the REFERENCES section at the end of this article.



MORE INFORMATION
When using the MSCOMM control to send and receive serial data, it is best to use event driven communications. This method is more efficient than polling, as it uses less CPU time and will only initiate an action when input is received. Below is a simple template to use which will allow for very efficient receiving of characters with minimal data loss.

The idea is to use the OnComm event to receive the data, then write the raw data to a string and pass it to another routine to do the processing of the raw data. Passing the data to another routine allows the OnComm event to receive every event without being "suppressed," while the actual parsing of the string takes place elsewhere.

The following example will assume that you have a null modem cable attached between COM1 and COM2. This is only necessary for testing. App1 is the COM1 application, App2 is the COM2 application.

Steps to Create App1
 Create a new Standard EXE project. Form1 is created by default. Choose Components from the Project menu, check the "Microsoft Comm Control," and click OK. Add an MSCOMM control to the form. Add a TextBox and a CommandButton to the form. Change the MultiLine property of the TextBox to True.  Add the following code to Form1's code window:

Private Sub Form_Load Form1.Caption = "App1" 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 Command1.Caption = "&Send" Text1.Text = "Test string from App1 " End Sub

Private Sub Command1_Click MSComm1.Output = Text1.Text End Sub

Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub



Steps to Create App2
 Start a new instance of Visual Basic.</li> Create a new Standard EXE project. Form1 is created by default.</li> Choose Components from the Project menu, check the "Microsoft Comm Control," and click OK.</li> Add an MSCOMM control to the form.</li> Add a TextBox to the form. Change the MultiLine property of the TextBox to True. Enlarge the TextBox so it will cover most of the form, as you will be displaying all the received data in it.</li>  Add the following code to Form1's code window:

Private Sub Form_Load Form1.Caption = "App2" With MSComm1 .CommPort = 2 .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 Text1.Text = "" End Sub

Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub

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

Sub HandleInput(InBuff As String) ' This is where you will process your input. This ' includes trapping characters, parsing strings, ' separating data fields, etc. For this case, you ' are simply going to display the data in the TextBox. Text1.SelStart = Len(Text1.Text) Text1.SelText = InBuff End Sub

</li></ol>

Steps to Run Applications

 * 1) Press the F5 key or click the run button on each project. You will need to move the apps around so you can see them both running at the same time.
 * 2) Make sure you have a standard null modem cable between COM1 and COM2, or change the MSComm1.Port properties to match the comm ports you are using.
 * 3) Click the CommandButton on App1; you should see the string "Test string from App1" appear in App2's TextBox for each button click.

<div class="references_section">