Sending and Receiving Data Through a Serial Connection

After you make a connection to the serial port by setting PortOpen to True, you can send and receive data through the connection. Whenever the Comm control sends or receives data, the control initiates the OnComm event to communicate with your application. The OnComm event also initiates the event when the control detects an error condition. The OnComm event changes the value of the CommEvent property to indicate the cause of the event.

The Comm control uses buffers to store data transferred through the control. Each buffer has an associated threshold property that controls the frequency of the OnComm event. The threshold property for the receive buffer is Rthreshold, and the threshold property for the transmit buffer is SThreshold.

The RThreshold property determines how often the OnComm event responds to incoming data. For example, if you set RThreshold to 1, the OnComm event initiates each time a character is placed into the receive buffer. The SThreshold property determines how often the OnComm event responds to outgoing data. For example, if you set SThreshold to 1, the OnComm event initiates each time all data is sent and the transmit buffer is empty. When the OnComm event starts in response to incoming data, the control sets the value of the CommEvent property to comEvReceive. When this occurs, you can use the Input property to read the new data from the receive buffer. The incoming data in the receive buffer can be either binary or character-based. You can determine how the control interprets the incoming data by setting the InputMode property. Set InputMode to comInputModeText for data that uses the ASCII character set and comInputModeBinary for all other data, such as data that has embedded control characters.

The InputLen property controls the number of characters the Input property reads from the buffer. Setting InputLen to 0 causes Input to read the entire buffer. In addition, you can use the InBufferCount property to determine the number of characters in the receive buffer.

The following code example shows how to receive data from a serial connection, one byte at a time.

Private Sub Comm1_OnComm()
  Dim TmpStr As String
  Comm1.InputLen = 1
  If Comm1.CommEvent = comEvReceive Then
    While Comm1.InBufferCount > 0
      TmpStr = TmpStr & Comm1.Input
    Wend
    Msgbox TmpStr,,” The input buffer received:”
  End If
End Sub

You cannot set the buffer sizes for the Comm control because the toolkit does not support the InBufferSize and OutBufferSize properties. To clear the receive buffer, set InBufferCount to 0.

To send data, you can use the Output property to place new data into the transmit buffer. When the OnComm event initiates in response to outgoing data, the control sets the value of the CommEvent property to comEvSend. You can use the OutBufferCount property to determine the number of characters in the transmit buffer. To clear the transmit buffer, set OutBufferCount to 0.

Note Setting the value for the RThreshold property or the SThreshold property to 0 prevents the OnComm event from initiating.

For more information about using the Comm control, see the corresponding Visual Basic 6.0 documentation for the MSComm control.