WPILibC++ 2024.1.1-beta-4
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 enum Parity {
35 };
36
37 enum StopBits {
40 kStopBits_Two = 20
41 };
42
48 };
49
51
52 enum Port { kOnboard = 0, kMXP = 1, kUSB = 2, kUSB1 = 2, kUSB2 = 3 };
53
54 /**
55 * Create an instance of a Serial Port class.
56 *
57 * @param baudRate The baud rate to configure the serial port.
58 * @param port The physical port to use
59 * @param dataBits The number of data bits per transfer. Valid values are
60 * between 5 and 8 bits.
61 * @param parity Select the type of parity checking to use.
62 * @param stopBits The number of stop bits to use as defined by the enum
63 * StopBits.
64 */
65 explicit SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
66 Parity parity = kParity_None,
67 StopBits stopBits = kStopBits_One);
68
69 /**
70 * Create an instance of a Serial Port class.
71 *
72 * Prefer to use the constructor that doesn't take a port name, but in some
73 * cases the automatic detection might not work correctly.
74 *
75 * @param baudRate The baud rate to configure the serial port.
76 * @param port The physical port to use
77 * @param portName The direct port name to use
78 * @param dataBits The number of data bits per transfer. Valid values are
79 * between 5 and 8 bits.
80 * @param parity Select the type of parity checking to use.
81 * @param stopBits The number of stop bits to use as defined by the enum
82 * StopBits.
83 */
84 SerialPort(int baudRate, std::string_view portName, Port port = kOnboard,
85 int dataBits = 8, Parity parity = kParity_None,
86 StopBits stopBits = kStopBits_One);
87
89
90 SerialPort(SerialPort&& rhs) = default;
91 SerialPort& operator=(SerialPort&& rhs) = default;
92
93 /**
94 * Set the type of flow control to enable on this port.
95 *
96 * By default, flow control is disabled.
97 */
98 void SetFlowControl(FlowControl flowControl);
99
100 /**
101 * Enable termination and specify the termination character.
102 *
103 * Termination is currently only implemented for receive.
104 * When the the terminator is received, the Read() or Scanf() will return
105 * fewer bytes than requested, stopping after the terminator.
106 *
107 * @param terminator The character to use for termination.
108 */
109 void EnableTermination(char terminator = '\n');
110
111 /**
112 * Disable termination behavior.
113 */
115
116 /**
117 * Get the number of bytes currently available to read from the serial port.
118 *
119 * @return The number of bytes available to read
120 */
122
123 /**
124 * Read raw bytes out of the buffer.
125 *
126 * @param buffer Pointer to the buffer to store the bytes in.
127 * @param count The maximum number of bytes to read.
128 * @return The number of bytes actually read into the buffer.
129 */
130 int Read(char* buffer, int count);
131
132 /**
133 * Write raw bytes to the buffer.
134 *
135 * @param buffer Pointer to the buffer to read the bytes from.
136 * @param count The maximum number of bytes to write.
137 * @return The number of bytes actually written into the port.
138 */
139 int Write(const char* buffer, int count);
140
141 /**
142 * Write raw bytes to the buffer.
143 *
144 * Use Write({data, len}) to get a buffer that is shorter than the length of
145 * the string.
146 *
147 * @param buffer the buffer to read the bytes from.
148 * @return The number of bytes actually written into the port.
149 */
151
152 /**
153 * Configure the timeout of the serial port.
154 *
155 * This defines the timeout for transactions with the hardware.
156 * It will affect reads and very large writes.
157 *
158 * @param timeout The time to wait for I/O.
159 */
160 void SetTimeout(units::second_t timeout);
161
162 /**
163 * Specify the size of the input buffer.
164 *
165 * Specify the amount of data that can be stored before data
166 * from the device is returned to Read or Scanf. If you want
167 * data that is received to be returned immediately, set this to 1.
168 *
169 * It the buffer is not filled before the read timeout expires, all
170 * data that has been received so far will be returned.
171 *
172 * @param size The read buffer size.
173 */
174 void SetReadBufferSize(int size);
175
176 /**
177 * Specify the size of the output buffer.
178 *
179 * Specify the amount of data that can be stored before being
180 * transmitted to the device.
181 *
182 * @param size The write buffer size.
183 */
184 void SetWriteBufferSize(int size);
185
186 /**
187 * Specify the flushing behavior of the output buffer.
188 *
189 * When set to kFlushOnAccess, data is synchronously written to the serial
190 * port after each call to either Printf() or Write().
191 *
192 * When set to kFlushWhenFull, data will only be written to the serial port
193 * when the buffer is full or when Flush() is called.
194 *
195 * @param mode The write buffer mode.
196 */
198
199 /**
200 * Force the output buffer to be written to the port.
201 *
202 * This is used when SetWriteBufferMode() is set to kFlushWhenFull to force a
203 * flush before the buffer is full.
204 */
205 void Flush();
206
207 /**
208 * Reset the serial port driver to a known state.
209 *
210 * Empty the transmit and receive buffers in the device and formatted I/O.
211 */
212 void Reset();
213
214 private:
216};
217
218} // 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
Definition: SerialPort.h:37
@ kStopBits_OnePointFive
Definition: SerialPort.h:39
@ kStopBits_One
Definition: SerialPort.h:38
@ kStopBits_Two
Definition: SerialPort.h:40
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
Definition: SerialPort.h:43
@ kFlowControl_XonXoff
Definition: SerialPort.h:45
@ kFlowControl_DtrDsr
Definition: SerialPort.h:47
@ kFlowControl_RtsCts
Definition: SerialPort.h:46
@ kFlowControl_None
Definition: SerialPort.h:44
Port
Definition: SerialPort.h:52
@ kOnboard
Definition: SerialPort.h:52
@ kUSB2
Definition: SerialPort.h:52
@ kMXP
Definition: SerialPort.h:52
@ kUSB
Definition: SerialPort.h:52
@ kUSB1
Definition: SerialPort.h:52
void Reset()
Reset the serial port driver to a known state.
Parity
Definition: SerialPort.h:29
@ kParity_Space
Definition: SerialPort.h:34
@ kParity_None
Definition: SerialPort.h:30
@ kParity_Mark
Definition: SerialPort.h:33
@ kParity_Even
Definition: SerialPort.h:32
@ kParity_Odd
Definition: SerialPort.h:31
WriteBufferMode
Definition: SerialPort.h:50
@ kFlushWhenFull
Definition: SerialPort.h:50
@ kFlushOnAccess
Definition: SerialPort.h:50
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