WPILibC++  2020.3.2
SerialPort.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <string>
11 
12 #include <hal/Types.h>
13 #include <wpi/StringRef.h>
14 #include <wpi/Twine.h>
15 #include <wpi/deprecated.h>
16 
17 #include "frc/ErrorBase.h"
18 
19 namespace frc {
20 
33 class SerialPort : public ErrorBase {
34  public:
35  enum Parity {
36  kParity_None = 0,
37  kParity_Odd = 1,
38  kParity_Even = 2,
39  kParity_Mark = 3,
40  kParity_Space = 4
41  };
42 
43  enum StopBits {
44  kStopBits_One = 10,
45  kStopBits_OnePointFive = 15,
46  kStopBits_Two = 20
47  };
48 
49  enum FlowControl {
50  kFlowControl_None = 0,
51  kFlowControl_XonXoff = 1,
52  kFlowControl_RtsCts = 2,
53  kFlowControl_DtrDsr = 4
54  };
55 
56  enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 };
57 
58  enum Port { kOnboard = 0, kMXP = 1, kUSB = 2, kUSB1 = 2, kUSB2 = 3 };
59 
71  SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
72  Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
73 
89  SerialPort(int baudRate, const wpi::Twine& portName, Port port = kOnboard,
90  int dataBits = 8, Parity parity = kParity_None,
91  StopBits stopBits = kStopBits_One);
92 
93  ~SerialPort();
94 
95  SerialPort(SerialPort&& rhs) = default;
96  SerialPort& operator=(SerialPort&& rhs) = default;
97 
103  void SetFlowControl(FlowControl flowControl);
104 
114  void EnableTermination(char terminator = '\n');
115 
119  void DisableTermination();
120 
126  int GetBytesReceived();
127 
135  int Read(char* buffer, int count);
136 
144  int Write(const char* buffer, int count);
145 
155  int Write(wpi::StringRef buffer);
156 
165  void SetTimeout(double timeout);
166 
179  void SetReadBufferSize(int size);
180 
189  void SetWriteBufferSize(int size);
190 
202  void SetWriteBufferMode(WriteBufferMode mode);
203 
210  void Flush();
211 
217  void Reset();
218 
219  private:
220  hal::Handle<HAL_SerialPortHandle> m_portHandle;
221 };
222 
223 } // namespace frc
frc::SerialPort::Read
int Read(char *buffer, int count)
Read raw bytes out of the buffer.
frc::SerialPort
Driver for the RS-232 serial port on the roboRIO.
Definition: SerialPort.h:33
frc::SerialPort::SetTimeout
void SetTimeout(double timeout)
Configure the timeout of the serial port.
frc::SerialPort::Flush
void Flush()
Force the output buffer to be written to the port.
frc::SerialPort::SerialPort
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.
frc::SerialPort::Write
int Write(const char *buffer, int count)
Write raw bytes to the buffer.
frc::SerialPort::EnableTermination
void EnableTermination(char terminator='\n')
Enable termination and specify the termination character.
frc::SerialPort::SetWriteBufferSize
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc::SerialPort::GetBytesReceived
int GetBytesReceived()
Get the number of bytes currently available to read from the serial port.
frc::SerialPort::DisableTermination
void DisableTermination()
Disable termination behavior.
frc::SerialPort::Reset
void Reset()
Reset the serial port driver to a known state.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::SerialPort::SetFlowControl
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::SerialPort::SetReadBufferSize
void SetReadBufferSize(int size)
Specify the size of the input buffer.
frc::SerialPort::SetWriteBufferMode
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.