2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-09-28 16:49:11 -07:00
|
|
|
/* Copyright (c) 2008-2019 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-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
#include <hal/SerialPort.h>
|
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;
|
|
|
|
|
|
2019-09-28 16:49:11 -07:00
|
|
|
m_portHandle =
|
|
|
|
|
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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialBaudRate(m_portHandle, baudRate, &status);
|
2018-03-05 19:41:09 -08:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialDataBits(m_portHandle, dataBits, &status);
|
2018-03-05 19:41:09 -08:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialParity(m_portHandle, parity, &status);
|
2018-03-05 19:41:09 -08:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialStopBits(m_portHandle, stopBits, &status);
|
2018-03-05 19:41:09 -08:00
|
|
|
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);
|
|
|
|
|
|
2019-09-28 16:49:11 -07:00
|
|
|
DisableTermination();
|
2018-03-05 19:41:09 -08:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_SerialPort,
|
|
|
|
|
static_cast<uint8_t>(port) + 1);
|
2018-03-05 19:41:09 -08:00
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
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
|
|
|
|
2019-09-28 16:49:11 -07:00
|
|
|
m_portHandle = 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialBaudRate(m_portHandle, baudRate, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialDataBits(m_portHandle, dataBits, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialParity(m_portHandle, parity, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialStopBits(m_portHandle, 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);
|
|
|
|
|
|
2019-09-28 16:49:11 -07:00
|
|
|
DisableTermination();
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_SerialPort,
|
|
|
|
|
static_cast<uint8_t>(port) + 1);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SerialPort::~SerialPort() {
|
|
|
|
|
int32_t status = 0;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_CloseSerial(m_portHandle, &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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialFlowControl(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_EnableSerialTermination(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_DisableSerialTermination(m_portHandle, &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;
|
2019-09-28 16:49:11 -07:00
|
|
|
int retVal = HAL_GetSerialBytesReceived(m_portHandle, &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;
|
2019-09-28 16:49:11 -07:00
|
|
|
int retVal = HAL_ReadSerial(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
int retVal =
|
|
|
|
|
HAL_WriteSerial(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialTimeout(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialReadBufferSize(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialWriteBufferSize(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_SetSerialWriteMode(m_portHandle, 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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_FlushSerial(m_portHandle, &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;
|
2019-09-28 16:49:11 -07:00
|
|
|
HAL_ClearSerial(m_portHandle, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-23 04:49:51 -07:00
|
|
|
}
|