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 "ErrorBase.h"
11 #include "HAL/HAL.hpp"
12 
26 class SerialPort : public ErrorBase {
27  public:
28  enum Parity {
29  kParity_None = 0,
30  kParity_Odd = 1,
31  kParity_Even = 2,
32  kParity_Mark = 3,
33  kParity_Space = 4
34  };
35  enum StopBits {
36  kStopBits_One = 10,
37  kStopBits_OnePointFive = 15,
38  kStopBits_Two = 20
39  };
40  enum FlowControl {
41  kFlowControl_None = 0,
42  kFlowControl_XonXoff = 1,
43  kFlowControl_RtsCts = 2,
44  kFlowControl_DtrDsr = 4
45  };
46  enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 };
47  enum Port { kOnboard = 0, kMXP = 1, kUSB = 2 };
48 
49  SerialPort(uint32_t baudRate, Port port = kOnboard, uint8_t dataBits = 8,
50  Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
51  ~SerialPort();
52 
53  SerialPort(const SerialPort&) = delete;
54  SerialPort& operator=(const SerialPort&) = delete;
55 
56  void SetFlowControl(FlowControl flowControl);
57  void EnableTermination(char terminator = '\n');
58  void DisableTermination();
59  int32_t GetBytesReceived();
60  uint32_t Read(char *buffer, int32_t count);
61  uint32_t Write(const std::string &buffer, int32_t count);
62  void SetTimeout(float timeout);
63  void SetReadBufferSize(uint32_t size);
64  void SetWriteBufferSize(uint32_t size);
65  void SetWriteBufferMode(WriteBufferMode mode);
66  void Flush();
67  void Reset();
68 
69  private:
70  uint32_t m_resourceManagerHandle = 0;
71  uint32_t m_portHandle = 0;
72  bool m_consoleModeEnabled = false;
73  uint8_t m_port;
74 };
SerialPort(uint32_t baudRate, Port port=kOnboard, uint8_t dataBits=8, Parity parity=kParity_None, StopBits stopBits=kStopBits_One)
Create an instance of a Serial Port class.
Definition: SerialPort.cpp:27
void SetReadBufferSize(uint32_t size)
Specify the size of the input buffer.
Definition: SerialPort.cpp:170
void SetFlowControl(FlowControl flowControl)
Set the type of flow control to enable on this port.
Definition: SerialPort.cpp:74
void Reset()
Reset the serial port driver to a known state.
Definition: SerialPort.cpp:224
void SetTimeout(float timeout)
Configure the timeout of the serial port.
Definition: SerialPort.cpp:152
~SerialPort()
Destructor.
Definition: SerialPort.cpp:63
void DisableTermination()
Disable termination behavior.
Definition: SerialPort.cpp:98
void Flush()
Force the output buffer to be written to the port.
Definition: SerialPort.cpp:213
Base class for most objects.
Definition: ErrorBase.h:66
uint32_t Write(const std::string &buffer, int32_t count)
Write raw bytes to the buffer.
Definition: SerialPort.cpp:137
int32_t GetBytesReceived()
Get the number of bytes currently available to read from the serial port.
Definition: SerialPort.cpp:109
uint32_t Read(char *buffer, int32_t count)
Read raw bytes out of the buffer.
Definition: SerialPort.cpp:123
void EnableTermination(char terminator= '\n')
Enable termination and specify the termination character.
Definition: SerialPort.cpp:89
void SetWriteBufferSize(uint32_t size)
Specify the size of the output buffer.
Definition: SerialPort.cpp:184
Driver for the RS-232 serial port on the RoboRIO.
Definition: SerialPort.h:26
void SetWriteBufferMode(WriteBufferMode mode)
Specify the flushing behavior of the output buffer.
Definition: SerialPort.cpp:201