WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SerialPort.hpp
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_view>
8
10#include "wpi/hal/Types.hpp"
11#include "wpi/units/time.hpp"
12
13namespace wpi {
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.
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.
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
131 SerialPort(SerialPort&& rhs) = default;
132 SerialPort& operator=(SerialPort&& rhs) = default;
133
134 /**
135 * Set the type of flow control to enable on this port.
136 *
137 * By default, flow control is disabled.
138 */
139 void SetFlowControl(FlowControl flowControl);
140
141 /**
142 * Enable termination and specify the termination character.
143 *
144 * Termination is currently only implemented for receive.
145 * When the the terminator is received, the Read() or Scanf() will return
146 * fewer bytes than requested, stopping after the terminator.
147 *
148 * @param terminator The character to use for termination.
149 */
150 void EnableTermination(char terminator = '\n');
151
152 /**
153 * Disable termination behavior.
154 */
156
157 /**
158 * Get the number of bytes currently available to read from the serial port.
159 *
160 * @return The number of bytes available to read
161 */
163
164 /**
165 * Read raw bytes out of the buffer.
166 *
167 * @param buffer Pointer to the buffer to store the bytes in.
168 * @param count The maximum number of bytes to read.
169 * @return The number of bytes actually read into the buffer.
170 */
171 int Read(char* buffer, int count);
172
173 /**
174 * Write raw bytes to the buffer.
175 *
176 * @param buffer Pointer to the buffer to read the bytes from.
177 * @param count The maximum number of bytes to write.
178 * @return The number of bytes actually written into the port.
179 */
180 int Write(const char* buffer, int count);
181
182 /**
183 * Write raw bytes to the buffer.
184 *
185 * Use Write({data, len}) to get a buffer that is shorter than the length of
186 * the string.
187 *
188 * @param buffer the buffer to read the bytes from.
189 * @return The number of bytes actually written into the port.
190 */
191 int Write(std::string_view buffer);
192
193 /**
194 * Configure the timeout of the serial port.
195 *
196 * This defines the timeout for transactions with the hardware.
197 * It will affect reads and very large writes.
198 *
199 * @param timeout The time to wait for I/O.
200 */
201 void SetTimeout(wpi::units::second_t timeout);
202
203 /**
204 * Specify the size of the input buffer.
205 *
206 * Specify the amount of data that can be stored before data
207 * from the device is returned to Read or Scanf. If you want
208 * data that is received to be returned immediately, set this to 1.
209 *
210 * It the buffer is not filled before the read timeout expires, all
211 * data that has been received so far will be returned.
212 *
213 * @param size The read buffer size.
214 */
215 void SetReadBufferSize(int size);
216
217 /**
218 * Specify the size of the output buffer.
219 *
220 * Specify the amount of data that can be stored before being
221 * transmitted to the device.
222 *
223 * @param size The write buffer size.
224 */
225 void SetWriteBufferSize(int size);
226
227 /**
228 * Specify the flushing behavior of the output buffer.
229 *
230 * When set to kFlushOnAccess, data is synchronously written to the serial
231 * port after each call to either Printf() or Write().
232 *
233 * When set to kFlushWhenFull, data will only be written to the serial port
234 * when the buffer is full or when Flush() is called.
235 *
236 * @param mode The write buffer mode.
237 */
239
240 /**
241 * Force the output buffer to be written to the port.
242 *
243 * This is used when SetWriteBufferMode() is set to kFlushWhenFull to force a
244 * flush before the buffer is full.
245 */
246 void Flush();
247
248 /**
249 * Reset the serial port driver to a known state.
250 *
251 * Empty the transmit and receive buffers in the device and formatted I/O.
252 */
253 void Reset();
254
255 private:
257};
258
259} // namespace wpi
StopBits
Represents the number of stop bits to use for Serial Communication.
Definition SerialPort.hpp:64
@ kStopBits_OnePointFive
One and a half stop bits.
Definition SerialPort.hpp:68
@ kStopBits_One
One stop bit.
Definition SerialPort.hpp:66
@ kStopBits_Two
Two stop bits.
Definition SerialPort.hpp:70
int Read(char *buffer, int count)
Read raw bytes out of the buffer.
void SetTimeout(wpi::units::second_t timeout)
Configure the timeout of the serial port.
void DisableTermination()
Disable termination behavior.
void EnableTermination(char terminator='\n')
Enable termination and specify the termination character.
SerialPort(SerialPort &&rhs)=default
int Write(std::string_view buffer)
Write raw bytes to the buffer.
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
Parity
Represents the parity to use for serial communications.
Definition SerialPort.hpp:48
@ kParity_Space
Parity bit always off.
Definition SerialPort.hpp:58
@ kParity_Mark
Parity bit always on.
Definition SerialPort.hpp:56
@ kParity_Even
Even parity.
Definition SerialPort.hpp:54
@ kParity_Odd
Odd parity.
Definition SerialPort.hpp:52
@ kParity_None
No parity.
Definition SerialPort.hpp:50
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.
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.
WriteBufferMode
Represents which type of buffer mode to use when writing to a serial port.
Definition SerialPort.hpp:90
@ kFlushOnAccess
Flush the buffer on each access.
Definition SerialPort.hpp:92
@ kFlushWhenFull
Flush the buffer when it is full.
Definition SerialPort.hpp:94
void Flush()
Force the output buffer to be written to the port.
int GetBytesReceived()
Get the number of bytes currently available to read from the serial port.
void SetReadBufferSize(int size)
Specify the size of the input buffer.
SerialPort & operator=(SerialPort &&rhs)=default
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.
void Reset()
Reset the serial port driver to a known state.
int Write(const char *buffer, int count)
Write raw bytes to the buffer.
Port
Serial port.
Definition SerialPort.hpp:32
@ kUSB1
USB serial port 1.
Definition SerialPort.hpp:40
@ kUSB
USB serial port (same as kUSB1).
Definition SerialPort.hpp:38
@ kOnboard
Onboard serial port on the roboRIO.
Definition SerialPort.hpp:34
@ kMXP
MXP (roboRIO MXP) serial port.
Definition SerialPort.hpp:36
@ kUSB2
USB serial port 2.
Definition SerialPort.hpp:42
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
FlowControl
Represents what type of flow control to use for serial communication.
Definition SerialPort.hpp:76
@ kFlowControl_RtsCts
RTS/CTS flow control.
Definition SerialPort.hpp:82
@ kFlowControl_XonXoff
XON/XOFF flow control.
Definition SerialPort.hpp:80
@ kFlowControl_DtrDsr
DTS/DSR flow control.
Definition SerialPort.hpp:84
@ kFlowControl_None
No flow control.
Definition SerialPort.hpp:78
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Definition CvSource.hpp:15