WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
wpi::SerialPort Class Reference

Driver for the RS-232 serial port on the roboRIO. More...

#include <wpi/hardware/bus/SerialPort.hpp>

Public Types

enum class  Port {
  ONBOARD = 0 , MXP = 1 , USB = 2 , USB_1 = 2 ,
  USB_2 = 3
}
 Serial port. More...
enum class  Parity {
  NONE = 0 , ODD = 1 , EVEN = 2 , MARK = 3 ,
  SPACE = 4
}
 Represents the parity to use for serial communications. More...
enum class  StopBits { ONE = 10 , ONE_POINT_FIVE = 15 , TWO = 20 }
 Represents the number of stop bits to use for Serial Communication. More...
enum class  FlowControl { NONE = 0 , XON_XOFF = 1 , RTS_CTS = 2 , DTR_DSR = 4 }
 Represents what type of flow control to use for serial communication. More...
enum class  WriteBufferMode { FLUSH_ON_ACCESS = 1 , FLUSH_WHEN_FULL = 2 }
 Represents which type of buffer mode to use when writing to a serial port. More...

Public Member Functions

 SerialPort (int baudRate, Port port=Port::ONBOARD, int dataBits=8, Parity parity=Parity::NONE, StopBits stopBits=StopBits::ONE)
 Create an instance of a Serial Port class.
 SerialPort (int baudRate, std::string_view portName, Port port=Port::ONBOARD, int dataBits=8, Parity parity=Parity::NONE, StopBits stopBits=StopBits::ONE)
 Create an instance of a Serial Port class.
 SerialPort (SerialPort &&rhs)=default
SerialPortoperator= (SerialPort &&rhs)=default
void SetFlowControl (FlowControl flowControl)
 Set the type of flow control to enable on this port.
void EnableTermination (char terminator='\n')
 Enable termination and specify the termination character.
void DisableTermination ()
 Disable termination behavior.
int GetBytesReceived ()
 Get the number of bytes currently available to read from the serial port.
int Read (char *buffer, int count)
 Read raw bytes out of the buffer.
int Write (const char *buffer, int count)
 Write raw bytes to the buffer.
int Write (std::string_view buffer)
 Write raw bytes to the buffer.
void SetTimeout (wpi::units::second_t timeout)
 Configure the timeout of the serial port.
void SetReadBufferSize (int size)
 Specify the size of the input buffer.
void SetWriteBufferSize (int size)
 Specify the size of the output buffer.
void SetWriteBufferMode (WriteBufferMode mode)
 Specify the flushing behavior of the output buffer.
void Flush ()
 Force the output buffer to be written to the port.
void Reset ()
 Reset the serial port driver to a known state.

Detailed Description

Driver for the RS-232 serial port on the roboRIO.

The current implementation uses the VISA formatted I/O mode. This means that all traffic goes through the formatted buffers. This allows the intermingled use of Printf(), Scanf(), and the raw buffer accessors Read() and Write().

More information can be found in the NI-VISA User Manual here: http://www.ni.com/pdf/manuals/370423a.pdf and the NI-VISA Programmer's Reference Manual here: http://www.ni.com/pdf/manuals/370132c.pdf

Member Enumeration Documentation

◆ FlowControl

enum class wpi::SerialPort::FlowControl
strong

Represents what type of flow control to use for serial communication.

Enumerator
NONE 

No flow control.

XON_XOFF 

XON/XOFF flow control.

RTS_CTS 

RTS/CTS flow control.

DTR_DSR 

DTS/DSR flow control.

◆ Parity

enum class wpi::SerialPort::Parity
strong

Represents the parity to use for serial communications.

Enumerator
NONE 

No parity.

ODD 

Odd parity.

EVEN 

Even parity.

MARK 

Parity bit always on.

SPACE 

Parity bit always off.

◆ Port

enum class wpi::SerialPort::Port
strong

Serial port.

Enumerator
ONBOARD 

Onboard serial port on the roboRIO.

MXP 

MXP (roboRIO MXP) serial port.

USB 

USB serial port (same as USB_1).

USB_1 

USB serial port 1.

USB_2 

USB serial port 2.

◆ StopBits

enum class wpi::SerialPort::StopBits
strong

Represents the number of stop bits to use for Serial Communication.

Enumerator
ONE 

One stop bit.

ONE_POINT_FIVE 

One and a half stop bits.

TWO 

Two stop bits.

◆ WriteBufferMode

Represents which type of buffer mode to use when writing to a serial port.

Enumerator
FLUSH_ON_ACCESS 

Flush the buffer on each access.

FLUSH_WHEN_FULL 

Flush the buffer when it is full.

Constructor & Destructor Documentation

◆ SerialPort() [1/3]

wpi::SerialPort::SerialPort ( int baudRate,
Port port = Port::ONBOARD,
int dataBits = 8,
Parity parity = Parity::NONE,
StopBits stopBits = StopBits::ONE )
explicit

Create an instance of a Serial Port class.

Parameters
baudRateThe baud rate to configure the serial port.
portThe physical port to use
dataBitsThe number of data bits per transfer. Valid values are between 5 and 8 bits.
paritySelect the type of parity checking to use.
stopBitsThe number of stop bits to use as defined by the enum StopBits.

◆ SerialPort() [2/3]

wpi::SerialPort::SerialPort ( int baudRate,
std::string_view portName,
Port port = Port::ONBOARD,
int dataBits = 8,
Parity parity = Parity::NONE,
StopBits stopBits = StopBits::ONE )

Create an instance of a Serial Port class.

Prefer to use the constructor that doesn't take a port name, but in some cases the automatic detection might not work correctly.

Parameters
baudRateThe baud rate to configure the serial port.
portThe physical port to use
portNameThe direct port name to use
dataBitsThe number of data bits per transfer. Valid values are between 5 and 8 bits.
paritySelect the type of parity checking to use.
stopBitsThe number of stop bits to use as defined by the enum StopBits.

◆ SerialPort() [3/3]

wpi::SerialPort::SerialPort ( SerialPort && rhs)
default

Member Function Documentation

◆ DisableTermination()

void wpi::SerialPort::DisableTermination ( )

Disable termination behavior.

◆ EnableTermination()

void wpi::SerialPort::EnableTermination ( char terminator = '\n')

Enable termination and specify the termination character.

Termination is currently only implemented for receive. When the the terminator is received, the Read() or Scanf() will return fewer bytes than requested, stopping after the terminator.

Parameters
terminatorThe character to use for termination.

◆ Flush()

void wpi::SerialPort::Flush ( )

Force the output buffer to be written to the port.

This is used when SetWriteBufferMode() is set to FLUSH_WHEN_FULL to force a flush before the buffer is full.

◆ GetBytesReceived()

int wpi::SerialPort::GetBytesReceived ( )

Get the number of bytes currently available to read from the serial port.

Returns
The number of bytes available to read

◆ operator=()

SerialPort & wpi::SerialPort::operator= ( SerialPort && rhs)
default

◆ Read()

int wpi::SerialPort::Read ( char * buffer,
int count )

Read raw bytes out of the buffer.

Parameters
bufferPointer to the buffer to store the bytes in.
countThe maximum number of bytes to read.
Returns
The number of bytes actually read into the buffer.

◆ Reset()

void wpi::SerialPort::Reset ( )

Reset the serial port driver to a known state.

Empty the transmit and receive buffers in the device and formatted I/O.

◆ SetFlowControl()

void wpi::SerialPort::SetFlowControl ( FlowControl flowControl)

Set the type of flow control to enable on this port.

By default, flow control is disabled.

◆ SetReadBufferSize()

void wpi::SerialPort::SetReadBufferSize ( int size)

Specify the size of the input buffer.

Specify the amount of data that can be stored before data from the device is returned to Read or Scanf. If you want data that is received to be returned immediately, set this to 1.

It the buffer is not filled before the read timeout expires, all data that has been received so far will be returned.

Parameters
sizeThe read buffer size.

◆ SetTimeout()

void wpi::SerialPort::SetTimeout ( wpi::units::second_t timeout)

Configure the timeout of the serial port.

This defines the timeout for transactions with the hardware. It will affect reads and very large writes.

Parameters
timeoutThe time to wait for I/O.

◆ SetWriteBufferMode()

void wpi::SerialPort::SetWriteBufferMode ( WriteBufferMode mode)

Specify the flushing behavior of the output buffer.

When set to FLUSH_ON_ACCESS, data is synchronously written to the serial port after each call to either Printf() or Write().

When set to FLUSH_WHEN_FULL, data will only be written to the serial port when the buffer is full or when Flush() is called.

Parameters
modeThe write buffer mode.

◆ SetWriteBufferSize()

void wpi::SerialPort::SetWriteBufferSize ( int size)

Specify the size of the output buffer.

Specify the amount of data that can be stored before being transmitted to the device.

Parameters
sizeThe write buffer size.

◆ Write() [1/2]

int wpi::SerialPort::Write ( const char * buffer,
int count )

Write raw bytes to the buffer.

Parameters
bufferPointer to the buffer to read the bytes from.
countThe maximum number of bytes to write.
Returns
The number of bytes actually written into the port.

◆ Write() [2/2]

int wpi::SerialPort::Write ( std::string_view buffer)

Write raw bytes to the buffer.

Use Write({data, len}) to get a buffer that is shorter than the length of the string.

Parameters
bufferthe buffer to read the bytes from.
Returns
The number of bytes actually written into the port.

The documentation for this class was generated from the following file: