At the very simplest level, values are set using Config()
with its TCommConfig
package to define the speed a port will run at (with iRate
) and the data format (with iDataBits
, iStopBits
and iParity
). Enumerated types are provided for setting these up (TBps
, TDataBits
, TStopBits
and TParity
).
A similiar method is used by Caps()
and its TCommCaps
package to hold possible speeds in its own iRate
, with possible data formats in its own iDataBits,
iStopBits
and iParity
. The main differences between these and the identically named elements in TCommConfig
is that unlike settings, port capabilities are never exclusive. For instance, the fact that a port can handle a speed of 50 bps doesn’t stop it from being able to support faster speeds as well. For this reason, the integers used to hold all the relevant capabilities of the port must be used in conjunction with individually enumerated bitmasks which are provided to enable the safe extraction of specific items of information.
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) ;
In software embedded on a machine where the capabilities of the hardware are well known in advance it is not necessary to include these checks. It is not bad practise to miss out the capabilities check and go straight to configuring the port. The most efficient way of doing this is to initialise your TCommConfig
by reading the current settings into it, and then simply changing those settings which are considered to be important.
TCommConfig portSettings;
commPort.Config (portSettings);
portSettings ().iRate = EBps19200;
portSettings ().iParity = EParityNone;
portSettings ().iDataBits = EData8;
portSettings ().iStopBits = EStop1;
r = commPort.SetConfig (portSettings);
User::LeaveIfError (r);