blob: c4e259a8a5854d8bf9df8f9b9ce8cf98a2b62a2a [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
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
8#include "SerialPort.h"
9
10#include "HAL/HAL.hpp"
11#include <stdarg.h>
12
13// static ViStatus _VI_FUNCH ioCompleteHandler (ViSession vi, ViEventType
14// eventType, ViEvent event, ViAddr userHandle);
15
16/**
17 * Create an instance of a Serial Port class.
18 *
19 * @param baudRate The baud rate to configure the serial port.
20 * @param port The physical port to use
21 * @param dataBits The number of data bits per transfer. Valid values are
22 * between 5 and 8 bits.
23 * @param parity Select the type of parity checking to use.
24 * @param stopBits The number of stop bits to use as defined by the enum
25 * StopBits.
26 */
27SerialPort::SerialPort(uint32_t baudRate, Port port, uint8_t dataBits,
28 SerialPort::Parity parity, SerialPort::StopBits stopBits)
29{
30 int32_t status = 0;
31
32 m_port = port;
33
34 serialInitializePort(port, &status);
35 wpi_setErrorWithContext(status, getHALErrorMessage(status));
36 serialSetBaudRate(port, baudRate, &status);
37 wpi_setErrorWithContext(status, getHALErrorMessage(status));
38 serialSetDataBits(port, dataBits, &status);
39 wpi_setErrorWithContext(status, getHALErrorMessage(status));
40 serialSetParity(port, parity, &status);
41 wpi_setErrorWithContext(status, getHALErrorMessage(status));
42 serialSetStopBits(port, stopBits, &status);
43 wpi_setErrorWithContext(status, getHALErrorMessage(status));
44
45 // Set the default timeout to 5 seconds.
46 SetTimeout(5.0f);
47
48 // Don't wait until the buffer is full to transmit.
49 SetWriteBufferMode(kFlushOnAccess);
50
51 EnableTermination();
52
53 // viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler,
54 // this);
55 // viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_nullptr);
56
57 HALReport(HALUsageReporting::kResourceType_SerialPort, 0);
58}
59
60/**
61 * Destructor.
62 */
63SerialPort::~SerialPort() {
64 int32_t status = 0;
65 serialClose(m_port, &status);
66 wpi_setErrorWithContext(status, getHALErrorMessage(status));
67}
68
69/**
70 * Set the type of flow control to enable on this port.
71 *
72 * By default, flow control is disabled.
73 */
74void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl) {
75 int32_t status = 0;
76 serialSetFlowControl(m_port, flowControl, &status);
77 wpi_setErrorWithContext(status, getHALErrorMessage(status));
78}
79
80/**
81 * Enable termination and specify the termination character.
82 *
83 * Termination is currently only implemented for receive.
84 * When the the terminator is recieved, the Read() or Scanf() will return
85 * fewer bytes than requested, stopping after the terminator.
86 *
87 * @param terminator The character to use for termination.
88 */
89void SerialPort::EnableTermination(char terminator) {
90 int32_t status = 0;
91 serialEnableTermination(m_port, terminator, &status);
92 wpi_setErrorWithContext(status, getHALErrorMessage(status));
93}
94
95/**
96 * Disable termination behavior.
97 */
98void SerialPort::DisableTermination() {
99 int32_t status = 0;
100 serialDisableTermination(m_port, &status);
101 wpi_setErrorWithContext(status, getHALErrorMessage(status));
102}
103
104/**
105 * Get the number of bytes currently available to read from the serial port.
106 *
107 * @return The number of bytes available to read
108 */
109int32_t SerialPort::GetBytesReceived() {
110 int32_t status = 0;
111 int32_t retVal = serialGetBytesReceived(m_port, &status);
112 wpi_setErrorWithContext(status, getHALErrorMessage(status));
113 return retVal;
114}
115
116/**
117 * Read raw bytes out of the buffer.
118 *
119 * @param buffer Pointer to the buffer to store the bytes in.
120 * @param count The maximum number of bytes to read.
121 * @return The number of bytes actually read into the buffer.
122 */
123uint32_t SerialPort::Read(char *buffer, int32_t count) {
124 int32_t status = 0;
125 int32_t retVal = serialRead(m_port, buffer, count, &status);
126 wpi_setErrorWithContext(status, getHALErrorMessage(status));
127 return retVal;
128}
129
130/**
131 * Write raw bytes to the buffer.
132 *
133 * @param buffer Pointer to the buffer to read the bytes from.
134 * @param count The maximum number of bytes to write.
135 * @return The number of bytes actually written into the port.
136 */
137uint32_t SerialPort::Write(const std::string &buffer, int32_t count) {
138 int32_t status = 0;
139 int32_t retVal = serialWrite(m_port, buffer.c_str(), count, &status);
140 wpi_setErrorWithContext(status, getHALErrorMessage(status));
141 return retVal;
142}
143
144/**
145 * Configure the timeout of the serial port.
146 *
147 * This defines the timeout for transactions with the hardware.
148 * It will affect reads and very large writes.
149 *
150 * @param timeout The number of seconds to to wait for I/O.
151 */
152void SerialPort::SetTimeout(float timeout) {
153 int32_t status = 0;
154 serialSetTimeout(m_port, timeout, &status);
155 wpi_setErrorWithContext(status, getHALErrorMessage(status));
156}
157
158/**
159 * Specify the size of the input buffer.
160 *
161 * Specify the amount of data that can be stored before data
162 * from the device is returned to Read or Scanf. If you want
163 * data that is recieved to be returned immediately, set this to 1.
164 *
165 * It the buffer is not filled before the read timeout expires, all
166 * data that has been received so far will be returned.
167 *
168 * @param size The read buffer size.
169 */
170void SerialPort::SetReadBufferSize(uint32_t size) {
171 int32_t status = 0;
172 serialSetReadBufferSize(m_port, size, &status);
173 wpi_setErrorWithContext(status, getHALErrorMessage(status));
174}
175
176/**
177 * Specify the size of the output buffer.
178 *
179 * Specify the amount of data that can be stored before being
180 * transmitted to the device.
181 *
182 * @param size The write buffer size.
183 */
184void SerialPort::SetWriteBufferSize(uint32_t size) {
185 int32_t status = 0;
186 serialSetWriteBufferSize(m_port, size, &status);
187 wpi_setErrorWithContext(status, getHALErrorMessage(status));
188}
189
190/**
191 * Specify the flushing behavior of the output buffer.
192 *
193 * When set to kFlushOnAccess, data is synchronously written to the serial port
194 * after each call to either Printf() or Write().
195 *
196 * When set to kFlushWhenFull, data will only be written to the serial port when
197 * the buffer is full or when Flush() is called.
198 *
199 * @param mode The write buffer mode.
200 */
201void SerialPort::SetWriteBufferMode(SerialPort::WriteBufferMode mode) {
202 int32_t status = 0;
203 serialSetWriteMode(m_port, mode, &status);
204 wpi_setErrorWithContext(status, getHALErrorMessage(status));
205}
206
207/**
208 * Force the output buffer to be written to the port.
209 *
210 * This is used when SetWriteBufferMode() is set to kFlushWhenFull to force a
211 * flush before the buffer is full.
212 */
213void SerialPort::Flush() {
214 int32_t status = 0;
215 serialFlush(m_port, &status);
216 wpi_setErrorWithContext(status, getHALErrorMessage(status));
217}
218
219/**
220 * Reset the serial port driver to a known state.
221 *
222 * Empty the transmit and receive buffers in the device and formatted I/O.
223 */
224void SerialPort::Reset() {
225 int32_t status = 0;
226 serialClear(m_port, &status);
227 wpi_setErrorWithContext(status, getHALErrorMessage(status));
228}