Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. |
| 3 | */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | #pragma once |
| 8 | |
| 9 | #include "ErrorBase.h" |
| 10 | #include "HAL/HAL.hpp" |
| 11 | |
| 12 | /** |
| 13 | * Driver for the RS-232 serial port on the RoboRIO. |
| 14 | * |
| 15 | * The current implementation uses the VISA formatted I/O mode. This means that |
| 16 | * all traffic goes through the fomatted buffers. This allows the |
| 17 | * intermingled |
| 18 | * use of Printf(), Scanf(), and the raw buffer accessors Read() and Write(). |
| 19 | * |
| 20 | * More information can be found in the NI-VISA User Manual here: |
| 21 | * http://www.ni.com/pdf/manuals/370423a.pdf |
| 22 | * and the NI-VISA Programmer's Reference Manual here: |
| 23 | * http://www.ni.com/pdf/manuals/370132c.pdf |
| 24 | */ |
| 25 | class SerialPort : public ErrorBase { |
| 26 | public: |
| 27 | enum Parity { |
| 28 | kParity_None = 0, |
| 29 | kParity_Odd = 1, |
| 30 | kParity_Even = 2, |
| 31 | kParity_Mark = 3, |
| 32 | kParity_Space = 4 |
| 33 | }; |
| 34 | enum StopBits { |
| 35 | kStopBits_One = 10, |
| 36 | kStopBits_OnePointFive = 15, |
| 37 | kStopBits_Two = 20 |
| 38 | }; |
| 39 | enum FlowControl { |
| 40 | kFlowControl_None = 0, |
| 41 | kFlowControl_XonXoff = 1, |
| 42 | kFlowControl_RtsCts = 2, |
| 43 | kFlowControl_DtrDsr = 4 |
| 44 | }; |
| 45 | enum WriteBufferMode { kFlushOnAccess = 1, kFlushWhenFull = 2 }; |
| 46 | enum Port { kOnboard = 0, kMXP = 1, kUSB = 2 }; |
| 47 | |
| 48 | SerialPort(uint32_t baudRate, Port port = kOnboard, uint8_t dataBits = 8, |
| 49 | Parity parity = kParity_None, StopBits stopBits = kStopBits_One); |
| 50 | ~SerialPort(); |
| 51 | |
| 52 | SerialPort(const SerialPort&) = delete; |
| 53 | SerialPort& operator=(const SerialPort&) = delete; |
| 54 | |
| 55 | void SetFlowControl(FlowControl flowControl); |
| 56 | void EnableTermination(char terminator = '\n'); |
| 57 | void DisableTermination(); |
| 58 | int32_t GetBytesReceived(); |
| 59 | uint32_t Read(char *buffer, int32_t count); |
| 60 | uint32_t Write(const std::string &buffer, int32_t count); |
| 61 | void SetTimeout(float timeout); |
| 62 | void SetReadBufferSize(uint32_t size); |
| 63 | void SetWriteBufferSize(uint32_t size); |
| 64 | void SetWriteBufferMode(WriteBufferMode mode); |
| 65 | void Flush(); |
| 66 | void Reset(); |
| 67 | |
| 68 | private: |
| 69 | uint32_t m_resourceManagerHandle = 0; |
| 70 | uint32_t m_portHandle = 0; |
| 71 | bool m_consoleModeEnabled = false; |
| 72 | uint8_t m_port; |
| 73 | }; |