How to Configure the Speed and Data Format: Tutorial

This tutorial shows the basic steps to perform any serial port configuration through the RComm API.

  1. Load the CSY and open a port.
  2. Get the capabilities of the port by calling RComm::Caps()
  3. The capabilities of the port are in the TCommCaps structure. For example, TCommCapsV01.iStopBits specifies the number of stop bits the port can manage. Note: Port capabilities are never exclusive. A port that can manage a speed of 50 bps may also be able to manage faster speeds as well. Most fields in TCommsCaps are bit fields which bitmasks must be used to determine all the capabilities. An example bitmask for the TCommCapsV01.iRate field is KCapsBps19200. These same fields in the configuration class TCommConfig are enumerations. For example, TCommCapsV01.iRate is a TUint since it is a bitfield. TCommConfigV01.iRate is an enumeration of type TBps.

  4. Create an object of type TCommConfigV01.

  5. Call RComm::Config() with the aConfig parameter set to the TCommConfigV01 object to get the port configuration.

  6. Set the fields that must be changed in the TCommConfigV01 object. For example, set the iSIREnable flag to enable Infrared.

  7. Call RComm::Config() with the TCommConfigV01 object as the aConfig parameter. The port is now configured.

    Tip: In cases where the application is written for a specific device, the application may skip the capability check since the capabilities are known at the time of manufacture.

Example

The following code uses Caps() to find out if its desired port configuration of 19200 bps and 8 data bits with no parity and one stop bit is possible:

TCommCaps ourCapabilities;
commPort.Caps (ourCapabilities);

if (((ourCapabilities ().iRate & KCapsBps19200)  == 0)||
    ((ourCapabilities ().iDataBits & KCapsData8) == 0)||
    ((ourCapabilities ().iStopBits & KCapsStop1) == 0)||
    ((ourCapabilities ().iParity & KCapsParityNone) == 0))
   User::Leave (KErrNotSupported) ;

The following code configures the speed settings for a port.

TCommConfig portSettings;
commPort.Config (portSettings);
portSettings ().iRate = EBps19200;
portSettings ().iParity = EParityNone;
portSettings ().iDataBits = EData8;
portSettings ().iStopBits = EStop1;
r = commPort.SetConfig (portSettings);
User::LeaveIfError (r);