WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
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 <string>
8#include <string_view>
9
10#include <hal/SerialPort.h>
11#include <hal/Types.h>
12#include <units/time.h>
13
14namespace frc {
15
16/**
17 * Driver for the RS-232 serial port on the roboRIO.
18 *
19 * The current implementation uses the VISA formatted I/O mode. This means that
20 * all traffic goes through the formatted buffers. This allows the intermingled
21 * use of Printf(), Scanf(), and the raw buffer accessors Read() and Write().
22 *
23 * More information can be found in the NI-VISA User Manual here:
24 * http://www.ni.com/pdf/manuals/370423a.pdf
25 * and the NI-VISA Programmer's Reference Manual here:
26 * http://www.ni.com/pdf/manuals/370132c.pdf
27 */
29 public:
30 /**
31 * Serial port.
32 */
33 enum Port {
34 /// Onboard serial port on the roboRIO.
36 /// MXP (roboRIO MXP) serial port.
37 kMXP = 1,
38 /// USB serial port (same as kUSB1).
39 kUSB = 2,
40 /// USB serial port 1.
41 kUSB1 = 2,
42 /// USB serial port 2.
43 kUSB2 = 3
44 };
45
46 /**
47 * Represents the parity to use for serial communications.
48 */
49 enum Parity {
50 /// No parity.
52 /// Odd parity.
54 /// Even parity.
56 /// Parity bit always on.
58 /// Parity bit always off.
60 };
61
62 /**
63 * Represents the number of stop bits to use for Serial Communication.
64 */
65 enum StopBits {
66 /// One stop bit.
68 /// One and a half stop bits.
70 /// Two stop bits.
71 kStopBits_Two = 20
72 };
73
74 /**
75 * Represents what type of flow control to use for serial communication.
76 */
78 /// No flow control.
80 /// XON/XOFF flow control.
82 /// RTS/CTS flow control.
84 /// DTS/DSR flow control.
86 };
87
88 /**
89 * Represents which type of buffer mode to use when writing to a serial port.
90 */
92 /// Flush the buffer on each access.
94 /// Flush the buffer when it is full.
96 };
97
98 /**
99 * Create an instance of a Serial Port class.
100 *
101 * @param baudRate The baud rate to configure the serial port.
102 * @param port The physical port to use
103 * @param dataBits The number of data bits per transfer. Valid values are
104 * between 5 and 8 bits.
105 * @param parity Select the type of parity checking to use.
106 * @param stopBits The number of stop bits to use as defined by the enum
107 * StopBits.
108 */
109 explicit SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
110 Parity parity = kParity_None,
111 StopBits stopBits = kStopBits_One);
112
113 /**
114 * Create an instance of a Serial Port class.
115 *
116 * Prefer to use the constructor that doesn't take a port name, but in some
117 * cases the automatic detection might not work correctly.
118 *
119 * @param baudRate The baud rate to configure the serial port.
120 * @param port The physical port to use
121 * @param portName The direct port name to use
122 * @param dataBits The number of data bits per transfer. Valid values are
123 * between 5 and 8 bits.
124 * @param parity Select the type of parity checking to use.
125 * @param stopBits The number of stop bits to use as defined by the enum
126 * StopBits.
127 */
128 SerialPort(int baudRate, std::string_view portName, Port port = kOnboard,
129 int dataBits = 8, Parity parity = kParity_None,
130 StopBits stopBits = kStopBits_One);
131
132 SerialPort(SerialPort&& rhs) = default;
133 SerialPort& operator=(SerialPort&& rhs) = default;
134
135 /**
136 * Set the type of flow control to enable on this port.
137 *
138 * By default, flow control is disabled.
139 */
140 void SetFlowControl(FlowControl flowControl);
141
142 /**
143 * Enable termination and specify the termination character.
144 *
145 * Termination is currently only implemented for receive.
146 * When the the terminator is received, the Read() or Scanf() will return
147 * fewer bytes than requested, stopping after the terminator.
148 *
149 * @param terminator The character to use for termination.
150 */
151 void EnableTermination(char terminator = '\n');
152
153 /**
154 * Disable termination behavior.
155 */
157
158 /**
159 * Get the number of bytes currently available to read from the serial port.
160 *
161 * @return The number of bytes available to read
162 */
164
165 /**
166 * Read raw bytes out of the buffer.
167 *
168 * @param buffer Pointer to the buffer to store the bytes in.
169 * @param count The maximum number of bytes to read.
170 * @return The number of bytes actually read into the buffer.
171 */
172 int Read(char* buffer, int count);
173
174 /**
175 * Write raw bytes to the buffer.
176 *
177 * @param buffer Pointer to the buffer to read the bytes from.
178 * @param count The maximum number of bytes to write.
179 * @return The number of bytes actually written into the port.
180 */
181 int Write(const char* buffer, int count);
182
183 /**
184 * Write raw bytes to the buffer.
185 *
186 * Use Write({data, len}) to get a buffer that is shorter than the length of
187 * the string.
188 *
189 * @param buffer the buffer to read the bytes from.
190 * @return The number of bytes actually written into the port.
191 */
192 int Write(std::string_view buffer);
193
194 /**
195 * Configure the timeout of the serial port.
196 *
197 * This defines the timeout for transactions with the hardware.
198 * It will affect reads and very large writes.
199 *
200 * @param timeout The time to wait for I/O.
201 */
202 void SetTimeout(units::second_t timeout);
203
204 /**
205 * Specify the size of the input buffer.
206 *
207 * Specify the amount of data that can be stored before data
208 * from the device is returned to Read or Scanf. If you want
209 * data that is received to be returned immediately, set this to 1.
210 *
211 * It the buffer is not filled before the read timeout expires, all
212 * data that has been received so far will be returned.
213 *
214 * @param size The read buffer size.
215 */
216 void SetReadBufferSize(int size);
217
218 /**
219 * Specify the size of the output buffer.
220 *
221 * Specify the amount of data that can be stored before being
222 * transmitted to the device.
223 *
224 * @param size The write buffer size.
225 */
226 void SetWriteBufferSize(int size);
227
228 /**
229 * Specify the flushing behavior of the output buffer.
230 *
231 * When set to kFlushOnAccess, data is synchronously written to the serial
232 * port after each call to either Printf() or Write().
233 *
234 * When set to kFlushWhenFull, data will only be written to the serial port
235 * when the buffer is full or when Flush() is called.
236 *
237 * @param mode The write buffer mode.
238 */
240
241 /**
242 * Force the output buffer to be written to the port.
243 *
244 * This is used when SetWriteBufferMode() is set to kFlushWhenFull to force a
245 * flush before the buffer is full.
246 */
247 void Flush();
248
249 /**
250 * Reset the serial port driver to a known state.
251 *
252 * Empty the transmit and receive buffers in the device and formatted I/O.
253 */
254 void Reset();
255
256 private:
258};
259
260} // namespace frc
Driver for the RS-232 serial port on the roboRIO.
Definition SerialPort.h:28
SerialPort(SerialPort &&rhs)=default
void Flush()
Force the output buffer to be written to the port.
SerialPort(int baudRate, std::string_view portName, Port port=kOnboard, int dataBits=8, Parity parity=kParity_None, StopBits stopBits=kStopBits_One)
Create an instance of a Serial Port class.
void EnableTermination(char terminator='\n')
Enable termination and specify the termination character.
void SetReadBufferSize(int size)
Specify the size of the input buffer.
StopBits
Represents the number of stop bits to use for Serial Communication.
Definition SerialPort.h:65
@ kStopBits_OnePointFive
One and a half stop bits.
Definition SerialPort.h:69
@ kStopBits_One
One stop bit.
Definition SerialPort.h:67
@ kStopBits_Two
Two stop bits.
Definition SerialPort.h:71
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
SerialPort & operator=(SerialPort &&rhs)=default
SerialPort(int baudRate, Port port=kOnboard, int dataBits=8, Parity parity=kParity_None, StopBits stopBits=kStopBits_One)
Create an instance of a Serial Port class.
void DisableTermination()
Disable termination behavior.
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output 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(units::second_t timeout)
Configure the timeout of the serial port.
FlowControl
Represents what type of flow control to use for serial communication.
Definition SerialPort.h:77
@ kFlowControl_XonXoff
XON/XOFF flow control.
Definition SerialPort.h:81
@ kFlowControl_DtrDsr
DTS/DSR flow control.
Definition SerialPort.h:85
@ kFlowControl_RtsCts
RTS/CTS flow control.
Definition SerialPort.h:83
@ kFlowControl_None
No flow control.
Definition SerialPort.h:79
Port
Serial port.
Definition SerialPort.h:33
@ kOnboard
Onboard serial port on the roboRIO.
Definition SerialPort.h:35
@ kUSB2
USB serial port 2.
Definition SerialPort.h:43
@ kMXP
MXP (roboRIO MXP) serial port.
Definition SerialPort.h:37
@ kUSB
USB serial port (same as kUSB1).
Definition SerialPort.h:39
@ kUSB1
USB serial port 1.
Definition SerialPort.h:41
void Reset()
Reset the serial port driver to a known state.
Parity
Represents the parity to use for serial communications.
Definition SerialPort.h:49
@ kParity_Space
Parity bit always off.
Definition SerialPort.h:59
@ kParity_None
No parity.
Definition SerialPort.h:51
@ kParity_Mark
Parity bit always on.
Definition SerialPort.h:57
@ kParity_Even
Even parity.
Definition SerialPort.h:55
@ kParity_Odd
Odd parity.
Definition SerialPort.h:53
WriteBufferMode
Represents which type of buffer mode to use when writing to a serial port.
Definition SerialPort.h:91
@ kFlushWhenFull
Flush the buffer when it is full.
Definition SerialPort.h:95
@ kFlushOnAccess
Flush the buffer on each access.
Definition SerialPort.h:93
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.
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
Definition CAN.h:11