How To Receive from the Serial Port by Using MScomm32.ocxLast reviewed: October 17, 1996Article ID: Q140525 |
The information in this article applies to:
SUMMARYThis article gives you two techniques you can use to receive data from the serial port using the Mscomm32.ocx control. The first uses an event-driven method and does not require you to poll the serial port to check for the presence of received characters. This technique allows the most flexibility and does not require extensive coding to prevent buffer over-runs. The second technique requires you to poll the input buffer periodically to check for the presence of received characters. This article describes these two techniques and provides examples for each.
MORE INFORMATIONYou need to set the following properties regardless of which technique you use:
Technique One: Event-Driven ReceiveThe event Driven technique generates an OnComm event when there are characters waiting in the input buffer. Also, the CommEvent property will contain a numeric 2. For the OnComm event to be triggered, you must set the Rthreshold property to a value other than zero (its default). The most common setting for the rthreshold property is 1, meaning that the OnComm event is triggered if a minimum of one character is waiting in the input buffer. For example, the following code may be placed in the OnComm event to append received data to a property of a form called mybuffer. Procedure MyCom.OnComm IF This.CommEvent = 2 ThisForm.mybuffer = ThisForm.mybuffer + This.Input ENDIFENDPROC
Technique Two: Polling the Input BufferPolling the input buffer requires that the program periodically stop what it is doing and check to see if there are characters waiting in the input buffer. When using this technique, leave the Rthreshold property at 0 (its default value), and check the InBufferCount property to see if it is greater than zero, which indicates that there are characters waiting in the buffer. NOTE: Using a technique such as checking the length of the Input property results in lost characters because as soon as the Input property is accessed, the Input buffer is emptied. Use the InBufferCount property instead. Assuming the Mscomm control is on the form and has the name MyCom and that there is a form property named mybuffer, the following code illustrates how to poll for waiting characters: Procedure myform.myproc IF Thisform.MyCom.InBufferCount > 0 Thisform.mybuffer = Thisform.mybuffer + Thisform.MyCom.Input ENDIFENDPROC The procedure code could be called in a timer method to facilitate checking for characters at semi-regular intervals. However, if large amounts of data are expected to be received from the serial port, Technique One will reduce the chance of over-running the input buffer.
|
Additional reference words: 5.00 3.00 VFoxWin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |