mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
Move SerialPort to HAL and add SerialPort support for Java
Squashed commit of the following: commit 4359262e2fef69b1646e3d76641fe622a78dfd89 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Wed Oct 29 13:43:12 2014 -0400 Add SerialPort to JNI and modify Java SerialPort to use it. Change-Id: Id3e6fa538a50e6a96274d8fb1be546dc396fc9c6 commit 114b192388c2fb01745cb3d5ad83612e9d66ea83 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Tue Oct 28 15:46:06 2014 -0400 Move Serial port handling to HAL and define serial errors Change-Id: I4cc73b64d71aafacb410bce080cb29e5fdf565a3 Change-Id: I35b729ad502137ee4c877a415d78007861991e31
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#include "SerialPort.h"
|
||||
|
||||
//#include "NetworkCommunication/UsageReporting.h"
|
||||
#include "visa/visa.h"
|
||||
#include "HAL/HAL.hpp"
|
||||
#include <stdarg.h>
|
||||
|
||||
//static ViStatus _VI_FUNCH ioCompleteHandler (ViSession vi, ViEventType eventType, ViEvent event, ViAddr userHandle);
|
||||
@@ -20,31 +20,25 @@
|
||||
* @param parity Select the type of parity checking to use.
|
||||
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
|
||||
*/
|
||||
SerialPort::SerialPort(uint32_t baudRate, uint8_t dataBits, SerialPort::Parity parity, SerialPort::StopBits stopBits)
|
||||
SerialPort::SerialPort(uint32_t baudRate, Port port, uint8_t dataBits, SerialPort::Parity parity, SerialPort::StopBits stopBits)
|
||||
: m_resourceManagerHandle (0)
|
||||
, m_portHandle (0)
|
||||
, m_consoleModeEnabled (false)
|
||||
{
|
||||
ViStatus localStatus = VI_SUCCESS;
|
||||
localStatus = viOpenDefaultRM((ViSession*)&m_resourceManagerHandle);
|
||||
wpi_setError(localStatus);
|
||||
|
||||
localStatus = viOpen(m_resourceManagerHandle, const_cast<char*>("ASRL1::INSTR"), VI_NULL, VI_NULL, (ViSession*)&m_portHandle);
|
||||
wpi_setError(localStatus);
|
||||
if (localStatus != 0)
|
||||
{
|
||||
m_consoleModeEnabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_BAUD, baudRate);
|
||||
wpi_setError(localStatus);
|
||||
localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_DATA_BITS, dataBits);
|
||||
wpi_setError(localStatus);
|
||||
localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_PARITY, parity);
|
||||
wpi_setError(localStatus);
|
||||
localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_STOP_BITS, stopBits);
|
||||
wpi_setError(localStatus);
|
||||
int32_t status = 0;
|
||||
|
||||
m_port = port;
|
||||
|
||||
serialInitializePort(port, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
serialSetBaudRate(port, baudRate, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
serialSetDataBits(port, dataBits, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
serialSetParity(port, parity, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
serialSetStopBits(port, stopBits, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
|
||||
// Set the default timeout to 5 seconds.
|
||||
SetTimeout(5.0f);
|
||||
@@ -56,7 +50,7 @@ SerialPort::SerialPort(uint32_t baudRate, uint8_t dataBits, SerialPort::Parity p
|
||||
|
||||
//viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this);
|
||||
//viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL);
|
||||
|
||||
|
||||
HALReport(HALUsageReporting::kResourceType_SerialPort, 0);
|
||||
}
|
||||
|
||||
@@ -65,12 +59,9 @@ SerialPort::SerialPort(uint32_t baudRate, uint8_t dataBits, SerialPort::Parity p
|
||||
*/
|
||||
SerialPort::~SerialPort()
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
//viUninstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this);
|
||||
viClose(m_portHandle);
|
||||
}
|
||||
viClose(m_resourceManagerHandle);
|
||||
int32_t status = 0;
|
||||
serialClose(m_port, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,11 +71,9 @@ SerialPort::~SerialPort()
|
||||
*/
|
||||
void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viSetAttribute (m_portHandle, VI_ATTR_ASRL_FLOW_CNTRL, flowControl);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialSetFlowControl(m_port, flowControl, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,12 +87,9 @@ void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl)
|
||||
*/
|
||||
void SerialPort::EnableTermination(char terminator)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
viSetAttribute(m_portHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE);
|
||||
viSetAttribute(m_portHandle, VI_ATTR_TERMCHAR, terminator);
|
||||
viSetAttribute(m_portHandle, VI_ATTR_ASRL_END_IN, VI_ASRL_END_TERMCHAR);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialEnableTermination(m_port, terminator, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,65 +97,22 @@ void SerialPort::EnableTermination(char terminator)
|
||||
*/
|
||||
void SerialPort::DisableTermination()
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
viSetAttribute(m_portHandle, VI_ATTR_TERMCHAR_EN, VI_FALSE);
|
||||
viSetAttribute(m_portHandle, VI_ATTR_ASRL_END_IN, VI_ASRL_END_NONE);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialDisableTermination(m_port, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of bytes currently available to read from the serial port.
|
||||
*
|
||||
* @return The number of bytes available to read.
|
||||
* @return The number of bytes available to read
|
||||
*/
|
||||
int32_t SerialPort::GetBytesReceived()
|
||||
{
|
||||
int32_t bytes = 0;
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viGetAttribute(m_portHandle, VI_ATTR_ASRL_AVAIL_NUM, &bytes);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output formatted text to the serial port.
|
||||
*
|
||||
* @bug All pointer-based parameters seem to return an error.
|
||||
*
|
||||
* @param writeFmt A string that defines the format of the output.
|
||||
*/
|
||||
void SerialPort::Printf(const char *writeFmt, ...)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
va_list args;
|
||||
va_start (args, writeFmt);
|
||||
ViStatus localStatus = viVPrintf(m_portHandle, (ViString)writeFmt, args);
|
||||
va_end (args);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Input formatted text from the serial port.
|
||||
*
|
||||
* @bug All pointer-based parameters seem to return an error.
|
||||
*
|
||||
* @param readFmt A string that defines the format of the input.
|
||||
*/
|
||||
void SerialPort::Scanf(const char *readFmt, ...)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
va_list args;
|
||||
va_start (args, readFmt);
|
||||
ViStatus localStatus = viVScanf(m_portHandle, (ViString)readFmt, args);
|
||||
va_end (args);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
int32_t retVal = serialGetBytesReceived(m_port, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,21 +124,10 @@ void SerialPort::Scanf(const char *readFmt, ...)
|
||||
*/
|
||||
uint32_t SerialPort::Read(char *buffer, int32_t count)
|
||||
{
|
||||
uint32_t retCount = 0;
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viBufRead(m_portHandle, (ViPBuf)buffer, count, (ViPUInt32)&retCount);
|
||||
switch (localStatus)
|
||||
{
|
||||
case VI_SUCCESS_TERM_CHAR:
|
||||
case VI_SUCCESS_MAX_CNT:
|
||||
case VI_ERROR_TMO: // Timeout
|
||||
break;
|
||||
default:
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
}
|
||||
return retCount;
|
||||
int32_t status = 0;
|
||||
int32_t retVal = serialRead(m_port, buffer, count, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,13 +139,10 @@ uint32_t SerialPort::Read(char *buffer, int32_t count)
|
||||
*/
|
||||
uint32_t SerialPort::Write(const char *buffer, int32_t count)
|
||||
{
|
||||
uint32_t retCount = 0;
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viBufWrite(m_portHandle, (ViPBuf)buffer, count, (ViPUInt32)&retCount);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
return retCount;
|
||||
int32_t status = 0;
|
||||
int32_t retVal = serialWrite(m_port, buffer, count, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,11 +155,9 @@ uint32_t SerialPort::Write(const char *buffer, int32_t count)
|
||||
*/
|
||||
void SerialPort::SetTimeout(float timeout)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viSetAttribute(m_portHandle, VI_ATTR_TMO_VALUE, (uint32_t)(timeout * 1e3));
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialSetTimeout(m_port, timeout, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,11 +174,9 @@ void SerialPort::SetTimeout(float timeout)
|
||||
*/
|
||||
void SerialPort::SetReadBufferSize(uint32_t size)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viSetBuf(m_portHandle, VI_READ_BUF, size);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialSetReadBufferSize(m_port, size, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,11 +189,9 @@ void SerialPort::SetReadBufferSize(uint32_t size)
|
||||
*/
|
||||
void SerialPort::SetWriteBufferSize(uint32_t size)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viSetBuf(m_portHandle, VI_WRITE_BUF, size);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialSetWriteBufferSize(m_port, size, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,11 +207,9 @@ void SerialPort::SetWriteBufferSize(uint32_t size)
|
||||
*/
|
||||
void SerialPort::SetWriteBufferMode(SerialPort::WriteBufferMode mode)
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viSetAttribute(m_portHandle, VI_ATTR_WR_BUF_OPER_MODE, mode);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialSetWriteMode(m_port, mode, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -299,11 +220,9 @@ void SerialPort::SetWriteBufferMode(SerialPort::WriteBufferMode mode)
|
||||
*/
|
||||
void SerialPort::Flush()
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viFlush(m_portHandle, VI_WRITE_BUF);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialFlush(m_port, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,11 +232,9 @@ void SerialPort::Flush()
|
||||
*/
|
||||
void SerialPort::Reset()
|
||||
{
|
||||
if (!m_consoleModeEnabled)
|
||||
{
|
||||
ViStatus localStatus = viClear(m_portHandle);
|
||||
wpi_setError(localStatus);
|
||||
}
|
||||
int32_t status = 0;
|
||||
serialClear(m_port, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
}
|
||||
|
||||
//void SerialPort::_internalHandler(uint32_t port, uint32_t eventType, uint32_t event)
|
||||
|
||||
Reference in New Issue
Block a user