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