ClearCore Library
Loading...
Searching...
No Matches
SerialDriver COM Ports

Overview

For applications that require sending or receiving data over a serial port ClearCore provides two COM ports (COM-0 and COM-1). The COM ports may be interfaced-with using the SerialDriver class.
In code, these connectors are represented by ConnectorCOM0 and ConnectorCOM1.

For UART communications the SerialDrivers may have their operational mode configured to facilitate either TTL or RS-232 wiring schemes. For SPI communications there is an SPI mode. The SerialDriver
connectors also have an auxiliary mode called CCIO. This mode is used to connect to the ClearCore I/O Expansion Board (CCIO-8) which provides eight additional digital I/O points.

Wiring and Initialization

  • See the ClearCore System Diagram for details on wiring COM-0 or COM-1 to a UART device, SPI device, or RS-232 DCE/Modem or DTE/Host.
  • Initialize the serial port into the correct mode by calling the SerialDriver::Mode() function. See below for examples on setting specific modes.

RS-232/TTL Modes

RS-232 and TTL (UART) modes use one wire (Rx) for receiving and another wire (Tx) for transmitting serial data asynchronously at a pre-determined baud rate. These modes are identical except that
the signals used are inverted. Data frames are signaled by the use of start and stop bits of opposite polarity. The number of stop bits in a data frame is 1 by default, but may be set to 2.
Transmission parity may also be configured, but is turned off by default. For each mode, flow control may optionally be enabled to make use of the RTS and CTS lines.

// Configure COM-1 for TTL mode.
// Set the data baud rate.
// Open the serial port to enable data transmission.
@ TTL
Definition Connector.h:145
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition SerialDriver.h:80
void PortOpen() override
bool Speed(uint32_t bitsPerSecond) override
Change the baud rate for the port.
Definition SerialDriver.h:159
SerialDriver ConnectorCOM1
COM-1 connector instance.
// Configure COM-0 for RS-232 mode.
// Set the data baud rate.
// (Optional) Set the data frame parity.
// (Optional) Set each data frame to use 2 stop bits.
// (Optional) Enable flow control.
// Open the serial port to enable data transmission.
@ RS232
Definition Connector.h:151
@ PARITY_O
Odd Parity.
Definition ISerial.h:60
void FlowControl(bool useFlowControl)
Set UART CTS/RTS flow control.
bool Parity(Parities newParity) override
Set UART transmission parity format.
bool StopBits(uint8_t bits) override
SerialDriver ConnectorCOM0
COM-0 connector instance.

Once the port is open use Send() to output characters or numeric data to the serial port. Use SendLine() to send data appended with carriage return and newline characters.

// Output some data to COM-0.
ConnectorCOM0.Send("Hello, ");
bool SendLine()
Send carriage return and newline characters.
Definition ISerial.h:162
bool Send(const char *buffer, size_t bufferSize)
Send the array of characters out the port.
Definition ISerial.h:173

To halt serial communications close the port using the SerialDriver::PortClose() function. Once the port is closed data will no longer be received by or transmitted from the serial port.
The port will only start receiving input again when it is re-opened.

// Close COM-0 to end serial communications.
void PortClose() override

SPI Mode

SPI (Serial Peripheral Interface) is a synchronous data transfer protocol; besides lines for transmitting and receiving data there is also a dedicated clock (SCK) to synchronize transfers. With
the ClearCore acting as an SPI master device the MOSI (Master Out, Slave In) line is analogous to the Tx signal, while the MISO (Master In, Slave Out) line is analogous to the Rx signal in
RS-232/TTL modes. As in RS-232/TTL modes, the speed of data transfers is set by the Speed() function. The difference is that in SPI there are no start or stop bits since data transfer is
synchronized using the clock signal. The polarity of the SPI clock can be configured to be idle low (default) or idle high. The SPI clock's phase may be configured so that data is sampled on a
falling edge and shifted out on a rising edge (default) or vice versa.

// Configure COM-0 for SPI mode.
// Set the clock frequency / data transfer rate.
// Set the SPI clock polarity and phase to match the target device.
// Open the serial port to enable data transmission.
@ SPI
Definition Connector.h:156
void SpiClock(SpiClockPolarities polarity, SpiClockPhases phase)
Change the polarity and phase for the SPI clock.
@ SCK_HIGH
SCK is high when idle.
Definition SerialBase.h:160
@ LEAD_SAMPLE
Leading edge samples, trailing edge changes.
Definition SerialBase.h:168

Toggle the Slave Select line to send to or receive data from the connected SPI device.

// Assert the Slave Select line to begin data transfer.
// Receive up to 100 bytes of data from the connected SPI device.
uint8_t received[100] = {0};
ConnectorCOM0.SpiTransferData(nullptr, received, 100);
// De-assert the Slave Select line to end data transfer.
uint8_t SpiTransferData(uint8_t data)
bool SpiSsMode(CtrlLineModes mode)
Change the SPI Slave Select mode.
@ LINE_OFF
Control line is in the OFF state.
Definition SerialBase.h:178
@ LINE_ON
Control line is in the ON state.
Definition SerialBase.h:180

To halt serial communications close the port using the SerialDriver::PortClose() function. Once the port is closed, data will no longer be received by or transmitted from the serial port.
The port will only start receiving input again when it is re-opened.

// Close COM-0 to end serial communications.

CCIO-8 Mode

Both SerialDriver ports may be configured for communications with up to eight ClearCore I/O Expansion Boards (CCIO-8). The operations of the attached CCIO-8 boards will be controlled by
ClearCore's CcioBoardManager, but the initial port setup is controlled by the SerialDriver functions.

// Set the mode of COM-0 to control the attached CCIO-8 boards.
// Open the serial port in CCIO-8 mode.
@ CCIO
Definition Connector.h:160

The CcioBoardManager controls closing down of the CCIO-8 link and its associated serial port.

// Close down the CCIO-8 link when we're done.
CcioMgr.LinkClose();
CcioBoardManager & CcioMgr
CCIO-8 manager.