2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SerialPort.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
#include <hal/SerialPort.h>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// static ViStatus _VI_FUNCH ioCompleteHandler (ViSession vi, ViEventType
|
|
|
|
|
// eventType, ViEvent event, ViAddr userHandle);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
SerialPort::SerialPort(int baudRate, Port port, int dataBits,
|
2016-05-20 17:30:37 -07:00
|
|
|
SerialPort::Parity parity,
|
|
|
|
|
SerialPort::StopBits stopBits) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
m_port = port;
|
|
|
|
|
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_InitializeSerialPort(static_cast<HAL_SerialPort>(port), &status);
|
2018-03-05 19:41:09 -08:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
// Don't continue if initialization failed
|
|
|
|
|
if (status < 0) return;
|
|
|
|
|
HAL_SetSerialBaudRate(static_cast<HAL_SerialPort>(port), baudRate, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
HAL_SetSerialDataBits(static_cast<HAL_SerialPort>(port), dataBits, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
HAL_SetSerialParity(static_cast<HAL_SerialPort>(port), parity, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
HAL_SetSerialStopBits(static_cast<HAL_SerialPort>(port), stopBits, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
|
|
|
|
|
// Set the default timeout to 5 seconds.
|
|
|
|
|
SetTimeout(5.0);
|
|
|
|
|
|
|
|
|
|
// Don't wait until the buffer is full to transmit.
|
|
|
|
|
SetWriteBufferMode(kFlushOnAccess);
|
|
|
|
|
|
|
|
|
|
EnableTermination();
|
|
|
|
|
|
|
|
|
|
// viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler,
|
|
|
|
|
// this);
|
|
|
|
|
// viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL);
|
|
|
|
|
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_SerialPort, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 23:31:08 -07:00
|
|
|
SerialPort::SerialPort(int baudRate, const wpi::Twine& portName, Port port,
|
2018-03-05 19:41:09 -08:00
|
|
|
int dataBits, SerialPort::Parity parity,
|
|
|
|
|
SerialPort::StopBits stopBits) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
m_port = port;
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<char, 64> buf;
|
2018-05-22 23:31:08 -07:00
|
|
|
const char* portNameC = portName.toNullTerminatedStringRef(buf).data();
|
2018-03-05 19:41:09 -08:00
|
|
|
|
|
|
|
|
HAL_InitializeSerialPortDirect(static_cast<HAL_SerialPort>(port), portNameC,
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2016-11-22 21:51:47 -08:00
|
|
|
// Don't continue if initialization failed
|
|
|
|
|
if (status < 0) return;
|
|
|
|
|
HAL_SetSerialBaudRate(static_cast<HAL_SerialPort>(port), baudRate, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialDataBits(static_cast<HAL_SerialPort>(port), dataBits, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialParity(static_cast<HAL_SerialPort>(port), parity, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialStopBits(static_cast<HAL_SerialPort>(port), stopBits, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Set the default timeout to 5 seconds.
|
2016-11-20 07:25:03 -08:00
|
|
|
SetTimeout(5.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Don't wait until the buffer is full to transmit.
|
|
|
|
|
SetWriteBufferMode(kFlushOnAccess);
|
|
|
|
|
|
|
|
|
|
EnableTermination();
|
|
|
|
|
|
|
|
|
|
// viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler,
|
|
|
|
|
// this);
|
2016-05-25 23:07:45 -07:00
|
|
|
// viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_SerialPort, 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SerialPort::~SerialPort() {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_CloseSerial(static_cast<HAL_SerialPort>(m_port), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl) {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialFlowControl(static_cast<HAL_SerialPort>(m_port), flowControl,
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SerialPort::EnableTermination(char terminator) {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_EnableSerialTermination(static_cast<HAL_SerialPort>(m_port), terminator,
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SerialPort::DisableTermination() {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_DisableSerialTermination(static_cast<HAL_SerialPort>(m_port), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int SerialPort::GetBytesReceived() {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
int retVal =
|
|
|
|
|
HAL_GetSerialBytesReceived(static_cast<HAL_SerialPort>(m_port), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return retVal;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int SerialPort::Read(char* buffer, int count) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
int retVal = HAL_ReadSerial(static_cast<HAL_SerialPort>(m_port), buffer,
|
|
|
|
|
count, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return retVal;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-12-10 22:40:44 -08:00
|
|
|
int SerialPort::Write(const char* buffer, int count) {
|
2018-04-29 23:33:19 -07:00
|
|
|
return Write(wpi::StringRef(buffer, static_cast<size_t>(count)));
|
2016-12-10 22:40:44 -08:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
int SerialPort::Write(wpi::StringRef buffer) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
int retVal = HAL_WriteSerial(static_cast<HAL_SerialPort>(m_port),
|
2016-12-10 22:40:44 -08:00
|
|
|
buffer.data(), buffer.size(), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return retVal;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void SerialPort::SetTimeout(double timeout) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialTimeout(static_cast<HAL_SerialPort>(m_port), timeout, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void SerialPort::SetReadBufferSize(int size) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialReadBufferSize(static_cast<HAL_SerialPort>(m_port), size,
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void SerialPort::SetWriteBufferSize(int size) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialWriteBufferSize(static_cast<HAL_SerialPort>(m_port), size,
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SerialPort::SetWriteBufferMode(SerialPort::WriteBufferMode mode) {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_SetSerialWriteMode(static_cast<HAL_SerialPort>(m_port), mode, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SerialPort::Flush() {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_FlushSerial(static_cast<HAL_SerialPort>(m_port), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SerialPort::Reset() {
|
|
|
|
|
int32_t status = 0;
|
2016-11-22 21:51:47 -08:00
|
|
|
HAL_ClearSerial(static_cast<HAL_SerialPort>(m_port), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-23 04:49:51 -07:00
|
|
|
}
|