WPILibC++  unspecified
SerialPort.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 <wpi/StringRef.h>
13 #include <wpi/Twine.h>
14 #include <wpi/deprecated.h>
15 
16 #include "ErrorBase.h"
17 
18 namespace frc {
19 
32 class SerialPort : public ErrorBase {
33  public:
34  enum Parity {
35  kParity_None = 0,
36  kParity_Odd = 1,
37  kParity_Even = 2,
38  kParity_Mark = 3,
39  kParity_Space = 4
40  };
41 
42  enum StopBits {
43  kStopBits_One = 10,
44  kStopBits_OnePointFive = 15,
45  kStopBits_Two = 20
46  };
47 
48  enum FlowControl {
49  kFlowControl_None = 0,
50  kFlowControl_XonXoff = 1,
51  kFlowControl_RtsCts = 2,
52  kFlowControl_DtrDsr = 4
53  };
54 
55  enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 };
56 
57  enum Port { kOnboard = 0, kMXP = 1, kUSB = 2, kUSB1 = 2, kUSB2 = 3 };
58 
70  SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
71  Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
72 
85  WPI_DEPRECATED("Will be removed for 2019")
86  SerialPort(int baudRate, const wpi::Twine& portName, Port port = kOnboard,
87  int dataBits = 8, Parity parity = kParity_None,
88  StopBits stopBits = kStopBits_One);
89 
90  ~SerialPort();
91 
92  SerialPort(const SerialPort&) = delete;
93  SerialPort& operator=(const SerialPort&) = delete;
94 
100  void SetFlowControl(FlowControl flowControl);
101 
111  void EnableTermination(char terminator = '\n');
112 
116  void DisableTermination();
117 
123  int GetBytesReceived();
124 
132  int Read(char* buffer, int count);
133 
141  int Write(const char* buffer, int count);
142 
152  int Write(wpi::StringRef buffer);
153 
162  void SetTimeout(double timeout);
163 
176  void SetReadBufferSize(int size);
177 
186  void SetWriteBufferSize(int size);
187 
199  void SetWriteBufferMode(WriteBufferMode mode);
200 
207  void Flush();
208 
214  void Reset();
215 
216  private:
217  int m_resourceManagerHandle = 0;
218  int m_portHandle = 0;
219  bool m_consoleModeEnabled = false;
220  int m_port;
221 };
222 
223 } // namespace frc
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
Definition: SerialPort.cpp:159
void Reset()
Reset the serial port driver to a known state.
Definition: SerialPort.cpp:178
void Flush()
Force the output buffer to be written to the port.
Definition: SerialPort.cpp:172
Definition: Utility.cpp:119
void SetTimeout(double timeout)
Configure the timeout of the serial port.
Definition: SerialPort.cpp:146
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.
Definition: SerialPort.cpp:18
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
Definition: SerialPort.cpp:98
Driver for the RS-232 serial port on the roboRIO.
Definition: SerialPort.h:32
void SetReadBufferSize(int size)
Specify the size of the input buffer.
Definition: SerialPort.cpp:152
int Write(const char *buffer, int count)
Write raw bytes to the buffer.
Definition: SerialPort.cpp:134
void DisableTermination()
Disable termination behavior.
Definition: SerialPort.cpp:112
Base class for most objects.
Definition: ErrorBase.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
int GetBytesReceived()
Get the number of bytes currently available to read from the serial port.
Definition: SerialPort.cpp:118
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.
Definition: SerialPort.cpp:166
void EnableTermination(char terminator= '\n')
Enable termination and specify the termination character.
Definition: SerialPort.cpp:105
int Read(char *buffer, int count)
Read raw bytes out of the buffer.
Definition: SerialPort.cpp:126