WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
SerialPort.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2016. 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 
14 namespace frc {
15 
28 class SerialPort : public ErrorBase {
29  public:
30  enum Parity {
31  kParity_None = 0,
32  kParity_Odd = 1,
33  kParity_Even = 2,
34  kParity_Mark = 3,
35  kParity_Space = 4
36  };
37  enum StopBits {
38  kStopBits_One = 10,
39  kStopBits_OnePointFive = 15,
40  kStopBits_Two = 20
41  };
42  enum FlowControl {
43  kFlowControl_None = 0,
44  kFlowControl_XonXoff = 1,
45  kFlowControl_RtsCts = 2,
46  kFlowControl_DtrDsr = 4
47  };
48  enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 };
49  enum Port { kOnboard = 0, kMXP = 1, kUSB = 2, kUSB1 = 2, kUSB2 = 3 };
50 
51  SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
52  Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
53  ~SerialPort();
54 
55  SerialPort(const SerialPort&) = delete;
56  SerialPort& operator=(const SerialPort&) = delete;
57 
58  void SetFlowControl(FlowControl flowControl);
59  void EnableTermination(char terminator = '\n');
60  void DisableTermination();
61  int GetBytesReceived();
62  int Read(char* buffer, int count);
63  int Write(const std::string& buffer, int count);
64  void SetTimeout(double timeout);
65  void SetReadBufferSize(int size);
66  void SetWriteBufferSize(int size);
67  void SetWriteBufferMode(WriteBufferMode mode);
68  void Flush();
69  void Reset();
70 
71  private:
72  int m_resourceManagerHandle = 0;
73  int m_portHandle = 0;
74  bool m_consoleModeEnabled = false;
75  int m_port;
76 };
77 
78 } // namespace frc
void SetWriteBufferSize(int size)
Specify the size of the output buffer.
Definition: SerialPort.cpp:193
void Reset()
Reset the serial port driver to a known state.
Definition: SerialPort.cpp:234
void Flush()
Force the output buffer to be written to the port.
Definition: SerialPort.cpp:223
void SetTimeout(double timeout)
Configure the timeout of the serial port.
Definition: SerialPort.cpp:160
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:28
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
Definition: SerialPort.cpp:77
Driver for the RS-232 serial port on the roboRIO.
Definition: SerialPort.h:28
void SetReadBufferSize(int size)
Specify the size of the input buffer.
Definition: SerialPort.cpp:178
void DisableTermination()
Disable termination behavior.
Definition: SerialPort.cpp:103
int Write(const std::string &buffer, int count)
Write raw bytes to the buffer.
Definition: SerialPort.cpp:144
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:114
~SerialPort()
Destructor.
Definition: SerialPort.cpp:66
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.
Definition: SerialPort.cpp:211
void EnableTermination(char terminator= '\n')
Enable termination and specify the termination character.
Definition: SerialPort.cpp:93
int Read(char *buffer, int count)
Read raw bytes out of the buffer.
Definition: SerialPort.cpp:129