WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
SerialPort.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. 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 "ErrorBase.h"
13 #include "llvm/StringRef.h"
14 #include "support/deprecated.h"
15 
16 namespace frc {
17 
30 class SerialPort : public ErrorBase {
31  public:
32  enum Parity {
33  kParity_None = 0,
34  kParity_Odd = 1,
35  kParity_Even = 2,
36  kParity_Mark = 3,
37  kParity_Space = 4
38  };
39  enum StopBits {
40  kStopBits_One = 10,
41  kStopBits_OnePointFive = 15,
42  kStopBits_Two = 20
43  };
44  enum FlowControl {
45  kFlowControl_None = 0,
46  kFlowControl_XonXoff = 1,
47  kFlowControl_RtsCts = 2,
48  kFlowControl_DtrDsr = 4
49  };
50  enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 };
51  enum Port { kOnboard = 0, kMXP = 1, kUSB = 2, kUSB1 = 2, kUSB2 = 3 };
52 
53  SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
54  Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
55  ~SerialPort();
56 
57  SerialPort(const SerialPort&) = delete;
58  SerialPort& operator=(const SerialPort&) = delete;
59 
60  void SetFlowControl(FlowControl flowControl);
61  void EnableTermination(char terminator = '\n');
62  void DisableTermination();
63  int GetBytesReceived();
64  int Read(char* buffer, int count);
65  WPI_DEPRECATED(
66  "Potential for unexpected behavior. Please use StringRef overload for "
67  "custom length buffers using std::string")
68  int Write(const std::string& buffer, int count);
69  int Write(const char* buffer, int count);
70  int Write(llvm::StringRef buffer);
71  void SetTimeout(double timeout);
72  void SetReadBufferSize(int size);
73  void SetWriteBufferSize(int size);
74  void SetWriteBufferMode(WriteBufferMode mode);
75  void Flush();
76  void Reset();
77 
78  private:
79  int m_resourceManagerHandle = 0;
80  int m_portHandle = 0;
81  bool m_consoleModeEnabled = false;
82  int m_port;
83 };
84 
85 } // namespace frc
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
Definition: SerialPort.cpp:219
void Reset()
Reset the serial port driver to a known state.
Definition: SerialPort.cpp:260
void Flush()
Force the output buffer to be written to the port.
Definition: SerialPort.cpp:249
void SetTimeout(double timeout)
Configure the timeout of the serial port.
Definition: SerialPort.cpp:186
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:29
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
Definition: SerialPort.cpp:78
int Write(llvm::StringRef buffer)
Write raw bytes to the buffer.
Definition: SerialPort.cpp:148
Driver for the RS-232 serial port on the roboRIO.
Definition: SerialPort.h:30
void SetReadBufferSize(int size)
Specify the size of the input buffer.
Definition: SerialPort.cpp:204
void DisableTermination()
Disable termination behavior.
Definition: SerialPort.cpp:104
Base class for most objects.
Definition: ErrorBase.h:72
int GetBytesReceived()
Get the number of bytes currently available to read from the serial port.
Definition: SerialPort.cpp:115
~SerialPort()
Destructor.
Definition: SerialPort.cpp:67
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.
Definition: SerialPort.cpp:237
void EnableTermination(char terminator= '\n')
Enable termination and specify the termination character.
Definition: SerialPort.cpp:94
int Read(char *buffer, int count)
Read raw bytes out of the buffer.
Definition: SerialPort.cpp:130