WPILibC++ 2023.4.3-108-ge5452e3
Serial Port Functions

Enumerations

enum  HAL_SerialPort : int32_t { HAL_SerialPort_Onboard = 0 , HAL_SerialPort_MXP = 1 , HAL_SerialPort_USB1 = 2 , HAL_SerialPort_USB2 = 3 }
 

Functions

HAL_SerialPortHandle HAL_InitializeSerialPort (HAL_SerialPort port, int32_t *status)
 Initializes a serial port. More...
 
HAL_SerialPortHandle HAL_InitializeSerialPortDirect (HAL_SerialPort port, const char *portName, int32_t *status)
 Initializes a serial port with a direct name. More...
 
int HAL_GetSerialFD (HAL_SerialPortHandle handle, int32_t *status)
 Gets the raw serial port file descriptor from a handle. More...
 
void HAL_SetSerialBaudRate (HAL_SerialPortHandle handle, int32_t baud, int32_t *status)
 Sets the baud rate of a serial port. More...
 
void HAL_SetSerialDataBits (HAL_SerialPortHandle handle, int32_t bits, int32_t *status)
 Sets the number of data bits on a serial port. More...
 
void HAL_SetSerialParity (HAL_SerialPortHandle handle, int32_t parity, int32_t *status)
 Sets the number of parity bits on a serial port. More...
 
void HAL_SetSerialStopBits (HAL_SerialPortHandle handle, int32_t stopBits, int32_t *status)
 Sets the number of stop bits on a serial port. More...
 
void HAL_SetSerialWriteMode (HAL_SerialPortHandle handle, int32_t mode, int32_t *status)
 Sets the write mode on a serial port. More...
 
void HAL_SetSerialFlowControl (HAL_SerialPortHandle handle, int32_t flow, int32_t *status)
 Sets the flow control mode of a serial port. More...
 
void HAL_SetSerialTimeout (HAL_SerialPortHandle handle, double timeout, int32_t *status)
 Sets the minimum serial read timeout of a port. More...
 
void HAL_EnableSerialTermination (HAL_SerialPortHandle handle, char terminator, int32_t *status)
 Sets the termination character that terminates a read. More...
 
void HAL_DisableSerialTermination (HAL_SerialPortHandle handle, int32_t *status)
 Disables a termination character for reads. More...
 
void HAL_SetSerialReadBufferSize (HAL_SerialPortHandle handle, int32_t size, int32_t *status)
 Sets the size of the read buffer. More...
 
void HAL_SetSerialWriteBufferSize (HAL_SerialPortHandle handle, int32_t size, int32_t *status)
 Sets the size of the write buffer. More...
 
int32_t HAL_GetSerialBytesReceived (HAL_SerialPortHandle handle, int32_t *status)
 Gets the number of bytes currently in the read buffer. More...
 
int32_t HAL_ReadSerial (HAL_SerialPortHandle handle, char *buffer, int32_t count, int32_t *status)
 Reads data from the serial port. More...
 
int32_t HAL_WriteSerial (HAL_SerialPortHandle handle, const char *buffer, int32_t count, int32_t *status)
 Writes data to the serial port. More...
 
void HAL_FlushSerial (HAL_SerialPortHandle handle, int32_t *status)
 Flushes the serial write buffer out to the port. More...
 
void HAL_ClearSerial (HAL_SerialPortHandle handle, int32_t *status)
 Clears the receive buffer of the serial port. More...
 
void HAL_CloseSerial (HAL_SerialPortHandle handle, int32_t *status)
 Closes a serial port. More...
 

Detailed Description

Enumeration Type Documentation

◆ HAL_SerialPort

enum HAL_SerialPort : int32_t
Enumerator
HAL_SerialPort_Onboard 
HAL_SerialPort_MXP 
HAL_SerialPort_USB1 
HAL_SerialPort_USB2 

Function Documentation

◆ HAL_ClearSerial()

void HAL_ClearSerial ( HAL_SerialPortHandle  handle,
int32_t *  status 
)

Clears the receive buffer of the serial port.

Parameters
[in]handlethe serial port handle
[out]statusthe error code, or 0 for success

◆ HAL_CloseSerial()

void HAL_CloseSerial ( HAL_SerialPortHandle  handle,
int32_t *  status 
)

Closes a serial port.

Parameters
[in]handlethe serial port handle to close
[out]statusthe error code, or 0 for success

◆ HAL_DisableSerialTermination()

void HAL_DisableSerialTermination ( HAL_SerialPortHandle  handle,
int32_t *  status 
)

Disables a termination character for reads.

Parameters
[in]handlethe serial port handle
[out]statusthe error code, or 0 for success

◆ HAL_EnableSerialTermination()

void HAL_EnableSerialTermination ( HAL_SerialPortHandle  handle,
char  terminator,
int32_t *  status 
)

Sets the termination character that terminates a read.

By default this is disabled.

Parameters
[in]handlethe serial port handle
[in]terminatorthe termination character to set
[out]statusthe error code, or 0 for success

◆ HAL_FlushSerial()

void HAL_FlushSerial ( HAL_SerialPortHandle  handle,
int32_t *  status 
)

Flushes the serial write buffer out to the port.

Parameters
[in]handlethe serial port handle
[out]statusthe error code, or 0 for success

◆ HAL_GetSerialBytesReceived()

int32_t HAL_GetSerialBytesReceived ( HAL_SerialPortHandle  handle,
int32_t *  status 
)

Gets the number of bytes currently in the read buffer.

Parameters
[in]handlethe serial port handle
[out]statusthe error code, or 0 for success
Returns
the number of bytes in the read buffer

◆ HAL_GetSerialFD()

int HAL_GetSerialFD ( HAL_SerialPortHandle  handle,
int32_t *  status 
)

Gets the raw serial port file descriptor from a handle.

Parameters
[in]handlethe serial port handle
[out]statusthe error code, or 0 for success
Returns
the raw port descriptor

◆ HAL_InitializeSerialPort()

HAL_SerialPortHandle HAL_InitializeSerialPort ( HAL_SerialPort  port,
int32_t *  status 
)

Initializes a serial port.

The channels are either the onboard RS232, the MXP UART, or 2 USB ports. The top port is USB1, the bottom port is USB2.

Parameters
[in]portthe serial port to initialize
[out]statusthe error code, or 0 for success
Returns
Serial Port Handle

◆ HAL_InitializeSerialPortDirect()

HAL_SerialPortHandle HAL_InitializeSerialPortDirect ( HAL_SerialPort  port,
const char *  portName,
int32_t *  status 
)

Initializes a serial port with a direct name.

This name is the /dev name for a specific port. Note these are not always consistent between roboRIO reboots.

Parameters
[in]portthe serial port to initialize
[in]portNamethe dev port name
[out]statusthe error code, or 0 for success
Returns
Serial Port Handle

◆ HAL_ReadSerial()

int32_t HAL_ReadSerial ( HAL_SerialPortHandle  handle,
char *  buffer,
int32_t  count,
int32_t *  status 
)

Reads data from the serial port.

Will wait for either timeout (if set), the termination char (if set), or the count to be full. Whichever one comes first.

Parameters
[in]handlethe serial port handle
[out]bufferthe buffer in which to store bytes read
[in]countthe number of bytes maximum to read
[out]statusthe error code, or 0 for success
Returns
the number of bytes actually read

◆ HAL_SetSerialBaudRate()

void HAL_SetSerialBaudRate ( HAL_SerialPortHandle  handle,
int32_t  baud,
int32_t *  status 
)

Sets the baud rate of a serial port.

Any value between 0 and 0xFFFFFFFF may be used. Default is 9600.

Parameters
[in]handlethe serial port handle
[in]baudthe baud rate to set
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialDataBits()

void HAL_SetSerialDataBits ( HAL_SerialPortHandle  handle,
int32_t  bits,
int32_t *  status 
)

Sets the number of data bits on a serial port.

Defaults to 8.

Parameters
[in]handlethe serial port handle
[in]bitsthe number of data bits (5-8)
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialFlowControl()

void HAL_SetSerialFlowControl ( HAL_SerialPortHandle  handle,
int32_t  flow,
int32_t *  status 
)

Sets the flow control mode of a serial port.

Valid values are: 0: None (default) 1: XON-XOFF 2: RTS-CTS 3: DTR-DSR

Parameters
[in]handlethe serial port handle
[in]flowthe mode to set (see remarks for valid values)
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialParity()

void HAL_SetSerialParity ( HAL_SerialPortHandle  handle,
int32_t  parity,
int32_t *  status 
)

Sets the number of parity bits on a serial port.

Valid values are: 0: None (default) 1: Odd 2: Even 3: Mark - Means exists and always 1 4: Space - Means exists and always 0

Parameters
[in]handlethe serial port handle
[in]paritythe parity bit mode (see remarks for valid values)
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialReadBufferSize()

void HAL_SetSerialReadBufferSize ( HAL_SerialPortHandle  handle,
int32_t  size,
int32_t *  status 
)

Sets the size of the read buffer.

Parameters
[in]handlethe serial port handle
[in]sizethe read buffer size
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialStopBits()

void HAL_SetSerialStopBits ( HAL_SerialPortHandle  handle,
int32_t  stopBits,
int32_t *  status 
)

Sets the number of stop bits on a serial port.

Valid values are: 10: One stop bit (default) 15: One and a half stop bits 20: Two stop bits

Parameters
[in]handlethe serial port handle
[in]stopBitsthe stop bit value (see remarks for valid values)
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialTimeout()

void HAL_SetSerialTimeout ( HAL_SerialPortHandle  handle,
double  timeout,
int32_t *  status 
)

Sets the minimum serial read timeout of a port.

Parameters
[in]handlethe serial port handle
[in]timeoutthe timeout in milliseconds
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialWriteBufferSize()

void HAL_SetSerialWriteBufferSize ( HAL_SerialPortHandle  handle,
int32_t  size,
int32_t *  status 
)

Sets the size of the write buffer.

Parameters
[in]handlethe serial port handle
[in]sizethe write buffer size
[out]statusthe error code, or 0 for success

◆ HAL_SetSerialWriteMode()

void HAL_SetSerialWriteMode ( HAL_SerialPortHandle  handle,
int32_t  mode,
int32_t *  status 
)

Sets the write mode on a serial port.

Valid values are: 1: Flush on access 2: Flush when full (default)

Parameters
[in]handlethe serial port handle
[in]modethe mode to set (see remarks for valid values)
[out]statusthe error code, or 0 for success

◆ HAL_WriteSerial()

int32_t HAL_WriteSerial ( HAL_SerialPortHandle  handle,
const char *  buffer,
int32_t  count,
int32_t *  status 
)

Writes data to the serial port.

Parameters
[in]handlethe serial port handle
[in]bufferthe buffer to write
[in]countthe number of bytes to write from the buffer
[out]statusthe error code, or 0 for success
Returns
the number of bytes actually written