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 <llvm/StringRef.h>
13 #include <support/deprecated.h>
14 
15 #include "ErrorBase.h"
16 
17 namespace frc {
18 
31 class SerialPort : public ErrorBase {
32  public:
33  enum Parity {
34  kParity_None = 0,
35  kParity_Odd = 1,
36  kParity_Even = 2,
37  kParity_Mark = 3,
38  kParity_Space = 4
39  };
40 
41  enum StopBits {
42  kStopBits_One = 10,
43  kStopBits_OnePointFive = 15,
44  kStopBits_Two = 20
45  };
46 
47  enum FlowControl {
48  kFlowControl_None = 0,
49  kFlowControl_XonXoff = 1,
50  kFlowControl_RtsCts = 2,
51  kFlowControl_DtrDsr = 4
52  };
53 
54  enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 };
55 
56  enum Port { kOnboard = 0, kMXP = 1, kUSB = 2, kUSB1 = 2, kUSB2 = 3 };
57 
58  SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
59  Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
60  WPI_DEPRECATED("Will be removed for 2019")
61  SerialPort(int baudRate, llvm::StringRef portName, Port port = kOnboard,
62  int dataBits = 8, Parity parity = kParity_None,
63  StopBits stopBits = kStopBits_One);
64  ~SerialPort();
65 
66  SerialPort(const SerialPort&) = delete;
67  SerialPort& operator=(const SerialPort&) = delete;
68 
69  void SetFlowControl(FlowControl flowControl);
70  void EnableTermination(char terminator = '\n');
71  void DisableTermination();
72  int GetBytesReceived();
73  int Read(char* buffer, int count);
74  int Write(const char* buffer, int count);
75  int Write(llvm::StringRef buffer);
76  void SetTimeout(double timeout);
77  void SetReadBufferSize(int size);
78  void SetWriteBufferSize(int size);
79  void SetWriteBufferMode(WriteBufferMode mode);
80  void Flush();
81  void Reset();
82 
83  private:
84  int m_resourceManagerHandle = 0;
85  int m_portHandle = 0;
86  bool m_consoleModeEnabled = false;
87  int m_port;
88 };
89 
90 } // namespace frc
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
Definition: SerialPort.cpp:258
void Reset()
Reset the serial port driver to a known state.
Definition: SerialPort.cpp:299
void Flush()
Force the output buffer to be written to the port.
Definition: SerialPort.cpp:288
Definition: RobotController.cpp:14
void SetTimeout(double timeout)
Configure the timeout of the serial port.
Definition: SerialPort.cpp:225
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:129
Driver for the RS-232 serial port on the roboRIO.
Definition: SerialPort.h:31
void SetReadBufferSize(int size)
Specify the size of the input buffer.
Definition: SerialPort.cpp:243
int Write(const char *buffer, int count)
Write raw bytes to the buffer.
Definition: SerialPort.cpp:196
void DisableTermination()
Disable termination behavior.
Definition: SerialPort.cpp:155
Base class for most objects.
Definition: ErrorBase.h:74
int GetBytesReceived()
Get the number of bytes currently available to read from the serial port.
Definition: SerialPort.cpp:166
~SerialPort()
Destructor.
Definition: SerialPort.cpp:118
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.
Definition: SerialPort.cpp:276
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
void EnableTermination(char terminator= '\n')
Enable termination and specify the termination character.
Definition: SerialPort.cpp:145
int Read(char *buffer, int count)
Read raw bytes out of the buffer.
Definition: SerialPort.cpp:181