WPILibC++ 2024.3.2
SerialPort.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include "hal/Types.h"
10
11/**
12 * @defgroup hal_serialport Serial Port Functions
13 * @ingroup hal_capi
14 * @{
15 */
16
22};
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/**
29 * Initializes a serial port.
30 *
31 * The channels are either the onboard RS232, the MXP UART, or 2 USB ports. The
32 * top port is USB1, the bottom port is USB2.
33 *
34 * @param[in] port the serial port to initialize
35 * @param[out] status the error code, or 0 for success
36 * @return Serial Port Handle
37 */
39 int32_t* status);
40
41/**
42 * Initializes a serial port with a direct name.
43 *
44 * This name is the /dev name for a specific port.
45 * Note these are not always consistent between roboRIO reboots.
46 *
47 * @param[in] port the serial port to initialize
48 * @param[in] portName the dev port name
49 * @param[out] status the error code, or 0 for success
50 * @return Serial Port Handle
51 */
53 const char* portName,
54 int32_t* status);
55
56/**
57 * Gets the raw serial port file descriptor from a handle.
58 *
59 * @param[in] handle the serial port handle
60 * @param[out] status the error code, or 0 for success
61 * @return the raw port descriptor
62 */
63int HAL_GetSerialFD(HAL_SerialPortHandle handle, int32_t* status);
64
65/**
66 * Sets the baud rate of a serial port.
67 *
68 * Any value between 0 and 0xFFFFFFFF may be used. Default is 9600.
69 *
70 * @param[in] handle the serial port handle
71 * @param[in] baud the baud rate to set
72 * @param[out] status the error code, or 0 for success
73 */
75 int32_t* status);
76
77/**
78 * Sets the number of data bits on a serial port.
79 *
80 * Defaults to 8.
81 *
82 * @param[in] handle the serial port handle
83 * @param[in] bits the number of data bits (5-8)
84 * @param[out] status the error code, or 0 for success
85 */
87 int32_t* status);
88
89/**
90 * Sets the number of parity bits on a serial port.
91 *
92 * Valid values are:
93 * 0: None (default)
94 * 1: Odd
95 * 2: Even
96 * 3: Mark - Means exists and always 1
97 * 4: Space - Means exists and always 0
98 *
99 * @param[in] handle the serial port handle
100 * @param[in] parity the parity bit mode (see remarks for valid values)
101 * @param[out] status the error code, or 0 for success
102 */
103void HAL_SetSerialParity(HAL_SerialPortHandle handle, int32_t parity,
104 int32_t* status);
105
106/**
107 * Sets the number of stop bits on a serial port.
108 *
109 * Valid values are:
110 * 10: One stop bit (default)
111 * 15: One and a half stop bits
112 * 20: Two stop bits
113 *
114 * @param[in] handle the serial port handle
115 * @param[in] stopBits the stop bit value (see remarks for valid values)
116 * @param[out] status the error code, or 0 for success
117 */
118void HAL_SetSerialStopBits(HAL_SerialPortHandle handle, int32_t stopBits,
119 int32_t* status);
120
121/**
122 * Sets the write mode on a serial port.
123 *
124 * Valid values are:
125 * 1: Flush on access
126 * 2: Flush when full (default)
127 *
128 * @param[in] handle the serial port handle
129 * @param[in] mode the mode to set (see remarks for valid values)
130 * @param[out] status the error code, or 0 for success
131 */
133 int32_t* status);
134
135/**
136 * Sets the flow control mode of a serial port.
137 *
138 * Valid values are:
139 * 0: None (default)
140 * 1: XON-XOFF
141 * 2: RTS-CTS
142 * 3: DTR-DSR
143 *
144 * @param[in] handle the serial port handle
145 * @param[in] flow the mode to set (see remarks for valid values)
146 * @param[out] status the error code, or 0 for success
147 */
149 int32_t* status);
150
151/**
152 * Sets the minimum serial read timeout of a port.
153 *
154 * @param[in] handle the serial port handle
155 * @param[in] timeout the timeout in milliseconds
156 * @param[out] status the error code, or 0 for success
157 */
158void HAL_SetSerialTimeout(HAL_SerialPortHandle handle, double timeout,
159 int32_t* status);
160
161/**
162 * Sets the termination character that terminates a read.
163 *
164 * By default this is disabled.
165 *
166 * @param[in] handle the serial port handle
167 * @param[in] terminator the termination character to set
168 * @param[out] status the error code, or 0 for success
169 */
171 int32_t* status);
172
173/**
174 * Disables a termination character for reads.
175 *
176 * @param[in] handle the serial port handle
177 * @param[out] status the error code, or 0 for success
178 */
180
181/**
182 * Sets the size of the read buffer.
183 *
184 * @param[in] handle the serial port handle
185 * @param[in] size the read buffer size
186 * @param[out] status the error code, or 0 for success
187 */
189 int32_t* status);
190
191/**
192 * Sets the size of the write buffer.
193 *
194 * @param[in] handle the serial port handle
195 * @param[in] size the write buffer size
196 * @param[out] status the error code, or 0 for success
197 */
199 int32_t* status);
200
201/**
202 * Gets the number of bytes currently in the read buffer.
203 *
204 * @param[in] handle the serial port handle
205 * @param[out] status the error code, or 0 for success
206 * @return the number of bytes in the read buffer
207 */
209 int32_t* status);
210
211/**
212 * Reads data from the serial port.
213 *
214 * Will wait for either timeout (if set), the termination char (if set), or the
215 * count to be full. Whichever one comes first.
216 *
217 * @param[in] handle the serial port handle
218 * @param[out] buffer the buffer in which to store bytes read
219 * @param[in] count the number of bytes maximum to read
220 * @param[out] status the error code, or 0 for success
221 * @return the number of bytes actually read
222 */
223int32_t HAL_ReadSerial(HAL_SerialPortHandle handle, char* buffer, int32_t count,
224 int32_t* status);
225
226/**
227 * Writes data to the serial port.
228 *
229 * @param[in] handle the serial port handle
230 * @param[in] buffer the buffer to write
231 * @param[in] count the number of bytes to write from the buffer
232 * @param[out] status the error code, or 0 for success
233 * @return the number of bytes actually written
234 */
235int32_t HAL_WriteSerial(HAL_SerialPortHandle handle, const char* buffer,
236 int32_t count, int32_t* status);
237
238/**
239 * Flushes the serial write buffer out to the port.
240 *
241 * @param[in] handle the serial port handle
242 * @param[out] status the error code, or 0 for success
243 */
244void HAL_FlushSerial(HAL_SerialPortHandle handle, int32_t* status);
245
246/**
247 * Clears the receive buffer of the serial port.
248 *
249 * @param[in] handle the serial port handle
250 * @param[out] status the error code, or 0 for success
251 */
252void HAL_ClearSerial(HAL_SerialPortHandle handle, int32_t* status);
253
254/**
255 * Closes a serial port.
256 *
257 * @param[in] handle the serial port handle to close
258 * @param[out] status the error code, or 0 for success
259 */
260void HAL_CloseSerial(HAL_SerialPortHandle handle, int32_t* status);
261#ifdef __cplusplus
262} // extern "C"
263#endif
264/** @} */
@ HAL_ENUM
Definition: Value.h:14
HAL_SerialPort
Definition: SerialPort.h:17
int32_t HAL_ReadSerial(HAL_SerialPortHandle handle, char *buffer, int32_t count, int32_t *status)
Reads data from the serial port.
HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port, const char *portName, int32_t *status)
Initializes a serial port with a direct name.
void HAL_SetSerialParity(HAL_SerialPortHandle handle, int32_t parity, int32_t *status)
Sets the number of parity bits on a serial port.
void HAL_SetSerialDataBits(HAL_SerialPortHandle handle, int32_t bits, int32_t *status)
Sets the number of data bits on a serial port.
void HAL_SetSerialWriteBufferSize(HAL_SerialPortHandle handle, int32_t size, int32_t *status)
Sets the size of the write buffer.
void HAL_SetSerialFlowControl(HAL_SerialPortHandle handle, int32_t flow, int32_t *status)
Sets the flow control mode of a serial port.
HAL_SerialPortHandle HAL_InitializeSerialPort(HAL_SerialPort port, int32_t *status)
Initializes a serial port.
int32_t HAL_GetSerialBytesReceived(HAL_SerialPortHandle handle, int32_t *status)
Gets the number of bytes currently in the read buffer.
void HAL_FlushSerial(HAL_SerialPortHandle handle, int32_t *status)
Flushes the serial write buffer out to the port.
void HAL_SetSerialReadBufferSize(HAL_SerialPortHandle handle, int32_t size, int32_t *status)
Sets the size of the read buffer.
int HAL_GetSerialFD(HAL_SerialPortHandle handle, int32_t *status)
Gets the raw serial port file descriptor from a handle.
void HAL_CloseSerial(HAL_SerialPortHandle handle, int32_t *status)
Closes a serial port.
int32_t HAL_WriteSerial(HAL_SerialPortHandle handle, const char *buffer, int32_t count, int32_t *status)
Writes data to the serial port.
void HAL_DisableSerialTermination(HAL_SerialPortHandle handle, int32_t *status)
Disables a termination character for reads.
void HAL_SetSerialBaudRate(HAL_SerialPortHandle handle, int32_t baud, int32_t *status)
Sets the baud rate of a serial port.
void HAL_SetSerialWriteMode(HAL_SerialPortHandle handle, int32_t mode, int32_t *status)
Sets the write mode on a serial port.
void HAL_SetSerialTimeout(HAL_SerialPortHandle handle, double timeout, int32_t *status)
Sets the minimum serial read timeout of a port.
void HAL_ClearSerial(HAL_SerialPortHandle handle, int32_t *status)
Clears the receive buffer of the serial port.
void HAL_SetSerialStopBits(HAL_SerialPortHandle handle, int32_t stopBits, int32_t *status)
Sets the number of stop bits on a serial port.
void HAL_EnableSerialTermination(HAL_SerialPortHandle handle, char terminator, int32_t *status)
Sets the termination character that terminates a read.
@ HAL_SerialPort_USB2
Definition: SerialPort.h:21
@ HAL_SerialPort_USB1
Definition: SerialPort.h:20
@ HAL_SerialPort_Onboard
Definition: SerialPort.h:18
@ HAL_SerialPort_MXP
Definition: SerialPort.h:19
HAL_Handle HAL_SerialPortHandle
Definition: Types.h:49
constexpr auto count() -> size_t
Definition: core.h:1203
bits
Definition: data.h:44