From 6b6e5d9530cce0e1581d4d58a5444596586e6767 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 29 Oct 2014 13:44:20 -0400 Subject: [PATCH] Move SerialPort to HAL and add SerialPort support for Java Squashed commit of the following: commit 4359262e2fef69b1646e3d76641fe622a78dfd89 Author: Kevin O'Connor 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 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 --- hal/include/HAL/Errors.hpp | 13 + hal/include/HAL/HAL.hpp | 1 + hal/include/HAL/SerialPort.hpp | 29 ++ hal/lib/Athena/HAL.cpp | 27 +- hal/lib/Athena/SerialPort.cpp | 146 +++++++++ .../include => hal/lib/Athena}/visa/visa.h | 0 .../lib/Athena}/visa/visatype.h | 0 wpilibc/wpilibC++Devices/include/SerialPort.h | 6 +- wpilibc/wpilibC++Devices/src/SerialPort.cpp | 203 ++++-------- wpilibj/wpilibJavaDevices/pom.xml | 4 - .../edu/wpi/first/wpilibj/SerialPort.java | 218 ++++++++----- .../wpi/first/wpilibj/hal/SerialPortJNI.java | 24 ++ .../java/edu/wpi/first/wpilibj/visa/Visa.java | 161 ---------- .../wpi/first/wpilibj/visa/VisaException.java | 196 ------------ .../edu/wpi/first/wpilibj/visa/package.html | 10 - wpilibj/wpilibJavaJNI/lib/SerialPortJNI.cpp | 288 ++++++++++++++++++ wpilibj/wpilibJavaJNI/pom.xml | 1 + 17 files changed, 729 insertions(+), 598 deletions(-) create mode 100644 hal/include/HAL/SerialPort.hpp create mode 100644 hal/lib/Athena/SerialPort.cpp rename {wpilibc/wpilibC++Devices/include => hal/lib/Athena}/visa/visa.h (100%) rename {wpilibc/wpilibC++Devices/include => hal/lib/Athena}/visa/visatype.h (100%) create mode 100644 wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/SerialPortJNI.java delete mode 100644 wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/Visa.java delete mode 100644 wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/VisaException.java delete mode 100644 wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/package.html create mode 100644 wpilibj/wpilibJavaJNI/lib/SerialPortJNI.cpp diff --git a/hal/include/HAL/Errors.hpp b/hal/include/HAL/Errors.hpp index 785b5bfae2..ed890d2325 100644 --- a/hal/include/HAL/Errors.hpp +++ b/hal/include/HAL/Errors.hpp @@ -45,3 +45,16 @@ #define ANALOG_TRIGGER_PULSE_OUTPUT_ERROR_MESSAGE "HAL: Attempted to read AnalogTrigger pulse output." #define PARAMETER_OUT_OF_RANGE -1028 #define PARAMETER_OUT_OF_RANGE_MESSAGE "HAL: A parameter is out of range." + +#define VI_ERROR_SYSTEM_ERROR_MESSAGE "HAL - VISA: System Error"; +#define VI_ERROR_INV_OBJECT_MESSAGE "HAL - VISA: Invalid Object" +#define VI_ERROR_RSRC_LOCKED_MESSAGE "HAL - VISA: Resource Locked" +#define VI_ERROR_RSRC_NFOUND_MESSAGE "HAL - VISA: Resource Not Found" +#define VI_ERROR_INV_RSRC_NAME_MESSAGE "HAL - VISA: Invalid Resource Name" +#define VI_ERROR_QUEUE_OVERFLOW_MESSAGE "HAL - VISA: Queue Overflow" +#define VI_ERROR_IO_MESSAGE "HAL - VISA: General IO Error" +#define VI_ERROR_ASRL_PARITY_MESSAGE "HAL - VISA: Parity Error" +#define VI_ERROR_ASRL_FRAMING_MESSAGE "HAL - VISA: Framing Error" +#define VI_ERROR_ASRL_OVERRUN_MESSAGE "HAL - VISA: Buffer Overrun Error" +#define VI_ERROR_RSRC_BUSY_MESSAGE "HAL - VISA: Resource Busy" +#define VI_ERROR_INV_PARAMETER_MESSAGE "HAL - VISA: Invalid Parameter" diff --git a/hal/include/HAL/HAL.hpp b/hal/include/HAL/HAL.hpp index 934d6d49d4..b3771fb422 100644 --- a/hal/include/HAL/HAL.hpp +++ b/hal/include/HAL/HAL.hpp @@ -23,6 +23,7 @@ #include "Errors.hpp" #include "PDP.hpp" #include "Power.hpp" +#include "SerialPort.hpp" #include "Utilities.hpp" #include "Semaphore.hpp" diff --git a/hal/include/HAL/SerialPort.hpp b/hal/include/HAL/SerialPort.hpp new file mode 100644 index 0000000000..07079f9a31 --- /dev/null +++ b/hal/include/HAL/SerialPort.hpp @@ -0,0 +1,29 @@ +#pragma once + +#ifdef __vxworks +#include +#else +#include +#endif + +extern "C" +{ + void serialInitializePort(uint8_t port, int32_t *status); + void serialSetBaudRate(uint8_t port, uint32_t baud, int32_t *status); + void serialSetDataBits(uint8_t port, uint8_t bits, int32_t *status); + void serialSetParity(uint8_t port, uint8_t parity, int32_t *status); + void serialSetStopBits(uint8_t port, uint8_t stopBits, int32_t *status); + void serialSetWriteMode(uint8_t port, uint8_t mode, int32_t *status); + void serialSetFlowControl(uint8_t port, uint8_t flow, int32_t *status); + void serialSetTimeout(uint8_t port, float timeout, int32_t *status); + void serialEnableTermination(uint8_t port, char terminator, int32_t *status); + void serialDisableTermination(uint8_t port, int32_t *status); + void serialSetReadBufferSize(uint8_t port, uint32_t size, int32_t *status); + void serialSetWriteBufferSize(uint8_t port, uint32_t size, int32_t *status); + int32_t serialGetBytesReceived(uint8_t port, int32_t *status); + uint32_t serialRead(uint8_t port, char* buffer, int32_t count, int32_t *status); + uint32_t serialWrite(uint8_t port, const char *buffer, int32_t count, int32_t *status); + void serialFlush(uint8_t port, int32_t *status); + void serialClear(uint8_t port, int32_t *status); + void serialClose(uint8_t port, int32_t *status); +} \ No newline at end of file diff --git a/hal/lib/Athena/HAL.cpp b/hal/lib/Athena/HAL.cpp index f16b6c5f59..8512c1ae86 100644 --- a/hal/lib/Athena/HAL.cpp +++ b/hal/lib/Athena/HAL.cpp @@ -3,6 +3,7 @@ #include "Port.h" #include "HAL/Errors.hpp" #include "ctre/ctre.h" +#include "visa/visa.h" #include "ChipObject.h" #include "NetworkCommunication/FRCComm.h" #include "NetworkCommunication/UsageReporting.h" @@ -101,7 +102,31 @@ const char* getHALErrorMessage(int32_t code) case ERR_CANSessionMux_NotAllowed: return ERR_CANSessionMux_NotAllowed_MESSAGE; case ERR_CANSessionMux_NotInitialized: - return ERR_CANSessionMux_NotInitialized_MESSAGE; + return ERR_CANSessionMux_NotInitialized_MESSAGE; + case VI_ERROR_SYSTEM_ERROR: + return VI_ERROR_SYSTEM_ERROR_MESSAGE; + case VI_ERROR_INV_OBJECT: + return VI_ERROR_INV_OBJECT_MESSAGE; + case VI_ERROR_RSRC_LOCKED: + return VI_ERROR_RSRC_LOCKED_MESSAGE; + case VI_ERROR_RSRC_NFOUND: + return VI_ERROR_RSRC_NFOUND_MESSAGE; + case VI_ERROR_INV_RSRC_NAME: + return VI_ERROR_INV_RSRC_NAME_MESSAGE; + case VI_ERROR_QUEUE_OVERFLOW: + return VI_ERROR_QUEUE_OVERFLOW_MESSAGE; + case VI_ERROR_IO: + return VI_ERROR_IO_MESSAGE; + case VI_ERROR_ASRL_PARITY: + return VI_ERROR_ASRL_PARITY_MESSAGE; + case VI_ERROR_ASRL_FRAMING: + return VI_ERROR_ASRL_FRAMING_MESSAGE; + case VI_ERROR_ASRL_OVERRUN: + return VI_ERROR_ASRL_OVERRUN_MESSAGE; + case VI_ERROR_RSRC_BUSY: + return VI_ERROR_RSRC_BUSY_MESSAGE; + case VI_ERROR_INV_PARAMETER: + return VI_ERROR_INV_PARAMETER_MESSAGE; default: return "Unknown error status"; } diff --git a/hal/lib/Athena/SerialPort.cpp b/hal/lib/Athena/SerialPort.cpp new file mode 100644 index 0000000000..6584f991dc --- /dev/null +++ b/hal/lib/Athena/SerialPort.cpp @@ -0,0 +1,146 @@ +#include "HAL/SerialPort.hpp" +#include "visa/visa.h" + + +uint32_t m_resourceManagerHandle; +uint32_t m_portHandle[2]; + +void serialInitializePort(uint8_t port, int32_t *status) { + char const * portName; + + if (m_resourceManagerHandle ==0) + viOpenDefaultRM((ViSession*)&m_resourceManagerHandle); + + if(port == 0) + portName = "ASRL1::INSTR"; + else + portName = "ASRL2::INSTR"; + + *status = viOpen(m_resourceManagerHandle, const_cast(portName), VI_NULL, VI_NULL, (ViSession*)&m_portHandle[port]); + if(*status > 0) + *status = 0; +} + +void serialSetBaudRate(uint8_t port, uint32_t baud, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_BAUD, baud); + if(*status > 0) + *status = 0; +} + +void serialSetDataBits(uint8_t port, uint8_t bits, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_DATA_BITS, bits); + if(*status > 0) + *status = 0; +} + +void serialSetParity(uint8_t port, uint8_t parity, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_PARITY, parity); + if(*status > 0) + *status = 0; +} + +void serialSetStopBits(uint8_t port, uint8_t stopBits, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_STOP_BITS, stopBits); + if(*status > 0) + *status = 0; +} + +void serialSetWriteMode(uint8_t port, uint8_t mode, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_WR_BUF_OPER_MODE, mode); + if(*status > 0) + *status = 0; +} + +void serialSetFlowControl(uint8_t port, uint8_t flow, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_FLOW_CNTRL, flow); + if(*status > 0) + *status = 0; +} + +void serialSetTimeout(uint8_t port, float timeout, int32_t *status) { + *status = viSetAttribute(m_portHandle[port], VI_ATTR_TMO_VALUE, (uint32_t)(timeout * 1e3)); + if(*status > 0) + *status = 0; +} + +void serialEnableTermination(uint8_t port, char terminator, int32_t *status) { + viSetAttribute(m_portHandle[port], VI_ATTR_TERMCHAR_EN, VI_TRUE); + viSetAttribute(m_portHandle[port], VI_ATTR_TERMCHAR, terminator); + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_END_IN, VI_ASRL_END_TERMCHAR); + if(*status > 0) + *status = 0; +} + +void serialDisableTermination(uint8_t port, int32_t *status) { + viSetAttribute(m_portHandle[port], VI_ATTR_TERMCHAR_EN, VI_FALSE); + *status = viSetAttribute(m_portHandle[port], VI_ATTR_ASRL_END_IN, VI_ASRL_END_NONE); + if(*status > 0) + *status = 0; +} + +void serialSetReadBufferSize(uint8_t port, uint32_t size, int32_t *status) { + *status = viSetBuf(m_portHandle[port], VI_READ_BUF, size); + if(*status > 0) + *status = 0; +} + +void serialSetWriteBufferSize(uint8_t port, uint32_t size, int32_t *status) { + *status = viSetBuf(m_portHandle[port], VI_WRITE_BUF, size); + if(*status > 0) + *status = 0; +} + +int32_t serialGetBytesReceived(uint8_t port, int32_t *status) { + int32_t bytes = 0; + + *status = viGetAttribute(m_portHandle[port], VI_ATTR_ASRL_AVAIL_NUM, &bytes); + if(*status > 0) + *status = 0; + return bytes; +} + +uint32_t serialRead(uint8_t port, char* buffer, int32_t count, int32_t *status) { + uint32_t retCount = 0; + + *status = viRead(m_portHandle[port], (ViPBuf)buffer, count, (ViPUInt32)&retCount); + + if(*status == VI_ERROR_IO || *status == VI_ERROR_ASRL_OVERRUN || *status == VI_ERROR_ASRL_FRAMING || *status == VI_ERROR_ASRL_PARITY) + { + int32_t localStatus = 0; + serialClear(port, &localStatus); + } + + if(*status == VI_ERROR_TMO || *status > 0) + *status = 0; + return retCount; +} + +uint32_t serialWrite(uint8_t port, const char *buffer, int32_t count, int32_t *status) { + uint32_t retCount = 0; + + *status = viWrite(m_portHandle[port], (ViPBuf)buffer, count, (ViPUInt32)&retCount); + + if(*status > 0) + *status = 0; + return retCount; +} + +void serialFlush(uint8_t port, int32_t *status) { + *status = viFlush(m_portHandle[port], VI_WRITE_BUF); + if(*status > 0) + *status = 0; +} + +void serialClear(uint8_t port, int32_t *status) { + *status = viClear(m_portHandle[port]); + if(*status > 0) + *status = 0; +} + +void serialClose(uint8_t port, int32_t *status) { + *status = viClose(m_portHandle[port]); + if(*status > 0) + *status = 0; +} + + \ No newline at end of file diff --git a/wpilibc/wpilibC++Devices/include/visa/visa.h b/hal/lib/Athena/visa/visa.h similarity index 100% rename from wpilibc/wpilibC++Devices/include/visa/visa.h rename to hal/lib/Athena/visa/visa.h diff --git a/wpilibc/wpilibC++Devices/include/visa/visatype.h b/hal/lib/Athena/visa/visatype.h similarity index 100% rename from wpilibc/wpilibC++Devices/include/visa/visatype.h rename to hal/lib/Athena/visa/visatype.h diff --git a/wpilibc/wpilibC++Devices/include/SerialPort.h b/wpilibc/wpilibC++Devices/include/SerialPort.h index d610eabf2a..18af15897d 100644 --- a/wpilibc/wpilibC++Devices/include/SerialPort.h +++ b/wpilibc/wpilibC++Devices/include/SerialPort.h @@ -27,16 +27,15 @@ public: enum StopBits {kStopBits_One=10, kStopBits_OnePointFive=15, kStopBits_Two=20}; enum FlowControl {kFlowControl_None=0, kFlowControl_XonXoff=1, kFlowControl_RtsCts=2, kFlowControl_DtrDsr=4}; enum WriteBufferMode {kFlushOnAccess=1, kFlushWhenFull=2}; + enum Port {kOnboard=0, kMXP=1}; - SerialPort(uint32_t baudRate, uint8_t dataBits = 8, Parity parity = kParity_None, + SerialPort(uint32_t baudRate, Port port = kOnboard, uint8_t dataBits = 8, Parity parity = kParity_None, StopBits stopBits = kStopBits_One); ~SerialPort(); void SetFlowControl(FlowControl flowControl); void EnableTermination(char terminator = '\n'); void DisableTermination(); int32_t GetBytesReceived(); - void Printf(const char *writeFmt, ...); - void Scanf(const char *readFmt, ...); uint32_t Read(char *buffer, int32_t count); uint32_t Write(const char *buffer, int32_t count); void SetTimeout(float timeout); @@ -50,6 +49,7 @@ private: uint32_t m_resourceManagerHandle; uint32_t m_portHandle; bool m_consoleModeEnabled; + uint8_t m_port; DISALLOW_COPY_AND_ASSIGN(SerialPort); }; diff --git a/wpilibc/wpilibC++Devices/src/SerialPort.cpp b/wpilibc/wpilibC++Devices/src/SerialPort.cpp index 9ff5da1592..b529fd142f 100644 --- a/wpilibc/wpilibC++Devices/src/SerialPort.cpp +++ b/wpilibc/wpilibC++Devices/src/SerialPort.cpp @@ -7,7 +7,7 @@ #include "SerialPort.h" //#include "NetworkCommunication/UsageReporting.h" -#include "visa/visa.h" +#include "HAL/HAL.hpp" #include //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("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) diff --git a/wpilibj/wpilibJavaDevices/pom.xml b/wpilibj/wpilibJavaDevices/pom.xml index 0cc5c20f73..7e5e63975e 100644 --- a/wpilibj/wpilibJavaDevices/pom.xml +++ b/wpilibj/wpilibJavaDevices/pom.xml @@ -65,8 +65,6 @@ edu/wpi/first/wpilibj/image/ edu/wpi/first/wpilibj/camera/ - edu/wpi/first/wpilibj/visa/ - edu/wpi/first/wpilibj/SerialPort.java @@ -81,8 +79,6 @@ edu/wpi/first/wpilibj/image/ edu/wpi/first/wpilibj/camera/ - edu/wpi/first/wpilibj/visa/ - edu/wpi/first/wpilibj/SerialPort.java diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/SerialPort.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/SerialPort.java index bf9e0cf9e7..ed2d0926a9 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/SerialPort.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/SerialPort.java @@ -8,8 +8,15 @@ package edu.wpi.first.wpilibj; import java.io.UnsupportedEncodingException; -import edu.wpi.first.wpilibj.visa.Visa; -import edu.wpi.first.wpilibj.visa.VisaException; +import java.nio.ByteOrder; +import java.nio.IntBuffer; +import java.nio.ByteBuffer; + +import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType; +import edu.wpi.first.wpilibj.communication.UsageReporting; +import edu.wpi.first.wpilibj.hal.HALLibrary; +import edu.wpi.first.wpilibj.hal.HALUtil; +import edu.wpi.first.wpilibj.hal.SerialPortJNI; /** * Driver for the RS-232 serial port on the RoboRIO. @@ -25,8 +32,22 @@ import edu.wpi.first.wpilibj.visa.VisaException; */ public class SerialPort { - private int m_resourceManagerHandle; - private int m_portHandle; + private byte m_port; + + public enum Port { + kOnboard(0), + kMXP(1); + + private int value; + + private Port(int value){ + this.value = value; + } + + public int getValue(){ + return this.value; + } + }; /** * Represents the parity to use for serial communications @@ -166,22 +187,22 @@ public class SerialPort { * @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. */ - public SerialPort(final int baudRate, final int dataBits, Parity parity, StopBits stopBits) throws VisaException { - m_resourceManagerHandle = 0; - m_portHandle = 0; - - m_resourceManagerHandle = Visa.viOpenDefaultRM(); - - m_portHandle = Visa.viOpen(m_resourceManagerHandle, "ASRL1::INSTR", 0, 0); - - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_BAUD, baudRate); - - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_DATA_BITS, dataBits); - - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_PARITY, parity.value); - - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_STOP_BITS, stopBits.value); - + public SerialPort(final int baudRate, Port port, final int dataBits, Parity parity, StopBits stopBits) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + m_port = (byte) port.getValue(); + + SerialPortJNI.serialInitializePort(m_port, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + SerialPortJNI.serialSetBaudRate(m_port, baudRate, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + SerialPortJNI.serialSetDataBits(m_port, (byte) dataBits, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + SerialPortJNI.serialSetParity(m_port, (byte) parity.value, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + SerialPortJNI.serialSetStopBits(m_port, (byte) stopBits.value, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + // Set the default read buffer size to 1 to return bytes immediately setReadBufferSize(1); @@ -193,11 +214,7 @@ public class SerialPort { disableTermination(); - //viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this); - //viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL); - - // XXX: Resource Reporting Fixes -// UsageReporting.report(UsageReporting.kResourceType_SerialPort,0); + UsageReporting.report(tResourceType.kResourceType_SerialPort,0); } /** @@ -207,8 +224,8 @@ public class SerialPort { * @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits. * @param parity Select the type of parity checking to use. */ - public SerialPort(final int baudRate, final int dataBits, Parity parity) throws VisaException { - this(baudRate, dataBits, parity, StopBits.kOne); + public SerialPort(final int baudRate, Port port, final int dataBits, Parity parity) { + this(baudRate, port, dataBits, parity, StopBits.kOne); } /** @@ -218,8 +235,8 @@ public class SerialPort { * @param baudRate The baud rate to configure the serial port. * @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits. */ - public SerialPort(final int baudRate, final int dataBits) throws VisaException { - this(baudRate, dataBits, Parity.kNone, StopBits.kOne); + public SerialPort(final int baudRate, Port port, final int dataBits) { + this(baudRate, port, dataBits, Parity.kNone, StopBits.kOne); } /** @@ -228,17 +245,18 @@ public class SerialPort { * * @param baudRate The baud rate to configure the serial port. */ - public SerialPort(final int baudRate) throws VisaException { - this(baudRate, 8, Parity.kNone, StopBits.kOne); + public SerialPort(final int baudRate, Port port) { + this(baudRate, port, 8, Parity.kNone, StopBits.kOne); } /** * Destructor. */ public void free() { - //viUninstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this); - Visa.viClose(m_portHandle); - Visa.viClose(m_resourceManagerHandle); + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialClose(m_port, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -247,8 +265,11 @@ public class SerialPort { * By default, flow control is disabled. * @param flowControl */ - public void setFlowControl(FlowControl flowControl) throws VisaException { - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_FLOW_CNTRL, flowControl.value); + public void setFlowControl(FlowControl flowControl) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialSetFlowControl(m_port, (byte) flowControl.value, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -260,14 +281,15 @@ public class SerialPort { * * @param terminator The character to use for termination. */ - public void enableTermination(char terminator) throws VisaException { - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TERMCHAR_EN, true); - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TERMCHAR, terminator); - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_END_IN, Visa.VI_ASRL_END_TERMCHAR); + public void enableTermination(char terminator) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialEnableTermination(m_port, terminator, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** - * Enable termination and specify the termination character. + * Enable termination with the default terminator '\n' * * Termination is currently only implemented for receive. * When the the terminator is received, the read() or readString() will return @@ -275,16 +297,18 @@ public class SerialPort { * * The default terminator is '\n' */ - public void enableTermination() throws VisaException { + public void enableTermination() { this.enableTermination('\n'); } /** * Disable termination behavior. */ - public void disableTermination() throws VisaException { - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TERMCHAR_EN, false); - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_END_IN, Visa.VI_ASRL_END_NONE); + public void disableTermination() { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialDisableTermination(m_port, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -292,20 +316,13 @@ public class SerialPort { * * @return The number of bytes available to read. */ - public int getBytesReceived() throws VisaException { - return Visa.viGetAttribute(m_portHandle, Visa.VI_ATTR_ASRL_AVAIL_NUM); - } - - /** - * Output formatted text to the serial port. - * - * @deprecated use write(string.getBytes()) instead - * @bug All pointer-based parameters seem to return an error. - * - * @param write A string to write - */ - public void print(String write) throws VisaException { - Visa.viVPrintf(m_portHandle, write); + public int getBytesReceived() { + int retVal = 0; + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + retVal = SerialPortJNI.serialGetBytesRecieved(m_port, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + return retVal; } /** @@ -313,7 +330,7 @@ public class SerialPort { * * @return The read string */ - public String readString() throws VisaException { + public String readString() { return readString(getBytesReceived()); } @@ -323,8 +340,8 @@ public class SerialPort { * @param count the number of characters to read into the string * @return The read string */ - public String readString(int count) throws VisaException { - byte[] out = Visa.viBufRead(m_portHandle, count); + public String readString(int count) { + byte[] out = read(count); try { return new String(out, 0, count, "US-ASCII"); } catch (UnsupportedEncodingException ex) { @@ -339,20 +356,43 @@ public class SerialPort { * @param count The maximum number of bytes to read. * @return An array of the read bytes */ - public byte[] read(final int count) throws VisaException { - return Visa.viBufRead(m_portHandle, count); + public byte[] read(final int count) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + ByteBuffer dataReceivedBuffer = ByteBuffer.allocateDirect(count); + SerialPortJNI.serialRead(m_port, dataReceivedBuffer, count, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + byte[] retVal = new byte[count]; + dataReceivedBuffer.get(retVal); + return retVal; } /** - * Write raw bytes to the buffer. + * Write raw bytes to the serial port. * - * @param buffer the buffer to read the bytes from. + * @param buffer The buffer of bytes to write. * @param count The maximum number of bytes to write. * @return The number of bytes actually written into the port. */ - public int write(byte[] buffer, int count) throws VisaException { - return Visa.viBufWrite(m_portHandle, buffer, count); + public int write(byte[] buffer, int count) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(count); + dataToSendBuffer.put(buffer); + int retVal = SerialPortJNI.serialWrite(m_port, dataToSendBuffer, count, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + return retVal; } + + /** + * Write a string to the serial port + * + * @param data The string to write to the serial port. + * @return The number of bytes actually written into the port. + */ + public int writeString(String data) { + return write(data.getBytes(), data.length()); + } /** * Configure the timeout of the serial port. @@ -363,8 +403,11 @@ public class SerialPort { * * @param timeout The number of seconds to to wait for I/O. */ - public void setTimeout(double timeout) throws VisaException { - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_TMO_VALUE, (int) (timeout * 1e3)); + public void setTimeout(double timeout) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialSetTimeout(m_port, (float)timeout, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -379,8 +422,11 @@ public class SerialPort { * * @param size The read buffer size. */ - void setReadBufferSize(int size) throws VisaException { - Visa.viSetBuf(m_portHandle, Visa.VI_READ_BUF, size); + public void setReadBufferSize(int size) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialSetReadBufferSize(m_port, size, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -391,8 +437,11 @@ public class SerialPort { * * @param size The write buffer size. */ - void setWriteBufferSize(int size) throws VisaException { - Visa.viSetBuf(m_portHandle, Visa.VI_WRITE_BUF, size); + public void setWriteBufferSize(int size) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialSetWriteBufferSize(m_port, size, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -406,8 +455,11 @@ public class SerialPort { * * @param mode The write buffer mode. */ - public void setWriteBufferMode(WriteBufferMode mode) throws VisaException { - Visa.viSetAttribute(m_portHandle, Visa.VI_ATTR_WR_BUF_OPER_MODE, mode.value); + public void setWriteBufferMode(WriteBufferMode mode) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialSetWriteMode(m_port, (byte)mode.value, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -416,8 +468,11 @@ public class SerialPort { * This is used when setWriteBufferMode() is set to kFlushWhenFull to force a * flush before the buffer is full. */ - public void flush() throws VisaException { - Visa.viFlush(m_portHandle, Visa.VI_WRITE_BUF); + public void flush() { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialFlush(m_port, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } /** @@ -425,7 +480,10 @@ public class SerialPort { * * Empty the transmit and receive buffers in the device and formatted I/O. */ - public void reset() throws VisaException { - Visa.viClear(m_portHandle); + public void reset() { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + SerialPortJNI.serialClear(m_port, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); } } diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/SerialPortJNI.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/SerialPortJNI.java new file mode 100644 index 0000000000..7d5252d4a0 --- /dev/null +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/SerialPortJNI.java @@ -0,0 +1,24 @@ +package edu.wpi.first.wpilibj.hal; +import java.nio.IntBuffer; +import java.nio.ByteBuffer; + +public class SerialPortJNI extends JNIWrapper { + public static native void serialInitializePort(byte port, IntBuffer status); + public static native void serialSetBaudRate(byte port, int baud, IntBuffer status); + public static native void serialSetDataBits(byte port, byte bits, IntBuffer status); + public static native void serialSetParity(byte port, byte parity, IntBuffer status); + public static native void serialSetStopBits(byte port, byte stopBits, IntBuffer status); + public static native void serialSetWriteMode(byte port, byte mode, IntBuffer status); + public static native void serialSetFlowControl(byte port, byte flow, IntBuffer status); + public static native void serialSetTimeout(byte port, float timeout, IntBuffer status); + public static native void serialEnableTermination(byte port, char terminator, IntBuffer status); + public static native void serialDisableTermination(byte port, IntBuffer status); + public static native void serialSetReadBufferSize(byte port, int size, IntBuffer status); + public static native void serialSetWriteBufferSize(byte port, int size, IntBuffer status); + public static native int serialGetBytesRecieved(byte port, IntBuffer status); + public static native int serialRead(byte port, ByteBuffer buffer, int count, IntBuffer status); + public static native int serialWrite(byte port, ByteBuffer buffer, int count, IntBuffer status); + public static native void serialFlush(byte port, IntBuffer status); + public static native void serialClear(byte port, IntBuffer status); + public static native void serialClose(byte port, IntBuffer status); +} \ No newline at end of file diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/Visa.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/Visa.java deleted file mode 100644 index d81edf0f50..0000000000 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/Visa.java +++ /dev/null @@ -1,161 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj.visa; - -import com.sun.jna.Function; -import com.sun.jna.NativeLibrary; -import com.sun.jna.Pointer; -import com.sun.jna.ptr.IntByReference; - -/** - * This port of visa.h includes only the functions and definitions used in - * SerialPort.java - * @author dtjones - */ -public class Visa { - - /** - * Stores the status of the last operation. - */ - private static int status; - - /** - * temp buffer for getting values by references - */ - private static final IntByReference pValue = new IntByReference(0); - - public static final int VI_ATTR_ASRL_BAUD = (0x3FFF0021); - public static final int VI_ATTR_ASRL_DATA_BITS = (0x3FFF0022); - public static final int VI_ATTR_ASRL_PARITY = (0x3FFF0023); - public static final int VI_ATTR_ASRL_STOP_BITS = (0x3FFF0024); - public static final int VI_ATTR_ASRL_FLOW_CNTRL = (0x3FFF0025); - public static final int VI_ATTR_TERMCHAR_EN = (0x3FFF0038); - public static final int VI_ATTR_TERMCHAR = (0x3FFF0018); - public static final int VI_ATTR_ASRL_END_IN = (0x3FFF00B3); - public static final int VI_ASRL_END_TERMCHAR = 2; - public static final int VI_ASRL_END_NONE = 0; - public static final int VI_ATTR_ASRL_AVAIL_NUM = (0x3FFF00AC); - public static final int VI_SUCCESS_TERM_CHAR = (0x3FFF0005); - public static final int VI_SUCCESS_MAX_CNT = (0x3FFF0006); - public static final int VI_ATTR_TMO_VALUE = (0x3FFF001A); - public static final int VI_ATTR_WR_BUF_OPER_MODE = (0x3FFF002D); - public static final short VI_READ_BUF = 1; - public static final short VI_WRITE_BUF = 2; - - private static final Function viOpenDefaultRMFn = NativeLibrary.getDefaultInstance().getFunction("viOpenDefaultRM"); - public synchronized static int viOpenDefaultRM() throws VisaException { - status = viOpenDefaultRMFn.call1(pValue.getPointer()); - assertCleanStatus("viOpenDefaultRM"); - return pValue.getValue(); - } - - private static final Function viOpenFn = NativeLibrary.getDefaultInstance().getFunction("viOpen"); - public synchronized static int viOpen(int sesn, String name, int mode, - int timeout) throws VisaException { - Pointer pName = new Pointer(name.length()); - pName.setBytes(0, name.getBytes(), 0, name.length()); - status = viOpenFn.call5(sesn, pName, mode, timeout, pValue.getPointer()); - assertCleanStatus("viOpen"); - pName.free(); - return pValue.getValue(); - } - - private static final Function viSetAttributeFn = NativeLibrary.getDefaultInstance().getFunction("viSetAttribute"); - public static void viSetAttribute(int vi, int attrName, int attrValue) throws VisaException { - status = viSetAttributeFn.call3(vi, attrName, attrValue); - assertCleanStatus("viSetAttribute"); - } - - public static void viSetAttribute(int vi, int attrName, boolean attrValue) throws VisaException { - status = viSetAttributeFn.call3(vi, attrName, attrValue?1:0); - assertCleanStatus("viSetAttribute"); - } - - private static final Function viSetBufFn = NativeLibrary.getDefaultInstance().getFunction("viSetBuf"); - public static void viSetBuf(int vi, short buffer, int size) throws VisaException { - status = viSetBufFn.call3(vi, buffer, size); - assertCleanStatus("viSetBuf"); - } - - private static final Function viCloseFn = NativeLibrary.getDefaultInstance().getFunction("viClose"); - public static void viClose(int vi) { - viCloseFn.call1(vi); - } - - private static final Function viGetAttributeFn = NativeLibrary.getDefaultInstance().getFunction("viGetAttribute"); - public synchronized static int viGetAttribute(int vi, int attrName) throws VisaException { - status = viGetAttributeFn.call3(vi, attrName, pValue.getPointer()); - assertCleanStatus("viGetAttribute"); - return pValue.getValue(); - } - - //Doesn't work correctly. Correct parameters are vi, a format string, and a list of args - private static final Function viVPrintfFn = NativeLibrary.getDefaultInstance().getFunction("viVPrintf"); - public static void viVPrintf(int vi, String write) throws VisaException { - Pointer string = new Pointer(write.length()); - string.setBytes(0, write.getBytes(), 0, write.length()); - status = viVPrintfFn.call2(vi, string); - string.free(); - assertCleanStatus("viPrintf"); - } - - private static final Function viBufReadFn = NativeLibrary.getDefaultInstance().getFunction("viBufRead"); - public static byte[] viBufRead(int vi, int cnt) throws VisaException { - Pointer bytes = new Pointer(cnt); - Pointer retCnt = new Pointer(4); - status = viBufReadFn.call4(vi, bytes, cnt, retCnt); - switch (status) { - case VI_SUCCESS_TERM_CHAR: - case VI_SUCCESS_MAX_CNT: - status = 0; - break; - default: - assertCleanStatus("viBufRead"); - } - - byte[] toReturn = new byte[cnt]; - bytes.getBytes(0, toReturn, 0, cnt); - bytes.free(); - return toReturn; - } - - private static final Function viBufWriteFn = NativeLibrary.getDefaultInstance().getFunction("viBufWrite"); - public synchronized static int viBufWrite(int vi, byte[] buf, int cnt) throws VisaException { - Pointer string = new Pointer(buf.length); - string.setBytes(0, buf, 0, buf.length); - status = viBufWriteFn.call4(vi, string, cnt, pValue.getPointer()); - assertCleanStatus("viBufWrite"); - string.free(); - return pValue.getValue(); - } - - private static final Function viFlushFn = NativeLibrary.getDefaultInstance().getFunction("viFlush"); - public static void viFlush(int vi, short mask) throws VisaException { - status = viFlushFn.call2(vi, mask); - assertCleanStatus("viFlush"); - } - - private static final Function viClearFn = NativeLibrary.getDefaultInstance().getFunction("viClear"); - public static void viClear(int vi) throws VisaException { - viClearFn.call1(vi); - assertCleanStatus("viClear"); - } - - protected static final void assertCleanStatus(String function) throws VisaException { - if (status < 0) { - throw new VisaException(function, status); - } else if (status != 0) { - System.out.println(VisaException.makeMessage(function, status)); - status = 0; - } - } - - private Visa() { - /* do not instantiate utility class */ - } -} diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/VisaException.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/VisaException.java deleted file mode 100644 index 27db6502a1..0000000000 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/VisaException.java +++ /dev/null @@ -1,196 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package edu.wpi.first.wpilibj.visa; - -/** - * Exception class which looks up visa error codes - * @author dtjones - */ -public class VisaException extends Exception { - - /** - * Create a new VisaException - * @param function the name of the function which returned the status code - * @param errorCode the status code returned by the function - */ - public VisaException(String function, int errorCode) { - super(makeMessage(function, errorCode)); - } - - public static String makeMessage(String function, int errorCode) { - return lookUpCode(errorCode) + " in function " + function; - } - - public static String lookUpCode(int errorCode) { - - switch (errorCode) { - case -1073807360: - return "VI_ERROR_SYSTEM_ERROR"; - case -1073807346: - return "VI_ERROR_INV_OBJECT"; - case -1073807345: - return "VI_ERROR_RSRC_LOCKED"; - case -1073807344: - return "VI_ERROR_INV_EXPR"; - case -1073807343: - return "VI_ERROR_RSRC_NFOUND"; - case -1073807342: - return "VI_ERROR_INV_RSRC_NAME"; - case -1073807341: - return "VI_ERROR_INV_ACC_MODE"; - case -1073807339: - return "VI_ERROR_TMO"; - case -1073807338: - return "VI_ERROR_CLOSING_FAILED"; - case -1073807333: - return "VI_ERROR_INV_DEGREE"; - case -1073807332: - return "VI_ERROR_INV_JOB_ID"; - case -1073807331: - return "VI_ERROR_NSUP_ATTR"; - case -1073807330: - return "VI_ERROR_NSUP_ATTR_STATE"; - case -1073807329: - return "VI_ERROR_ATTR_READONLY"; - case -1073807328: - return "VI_ERROR_INV_LOCK_TYPE"; - case -1073807327: - return "VI_ERROR_INV_ACCESS_KEY"; - case -1073807322: - return "VI_ERROR_INV_EVENT"; - case -1073807321: - return "VI_ERROR_INV_MECH"; - case -1073807320: - return "VI_ERROR_HNDLR_NINSTALLED"; - case -1073807319: - return "VI_ERROR_INV_HNDLR_REF"; - case -1073807318: - return "VI_ERROR_INV_CONTEXT"; - case -1073807315: - return "VI_ERROR_QUEUE_OVERFLOW"; - case -1073807313: - return "VI_ERROR_NENABLED"; - case -1073807312: - return "VI_ERROR_ABORT"; - case -1073807308: - return "VI_ERROR_RAW_WR_PROT_VIOL"; - case -1073807307: - return "VI_ERROR_RAW_RD_PROT_VIOL"; - case -1073807306: - return "VI_ERROR_OUTP_PROT_VIOL"; - case -1073807305: - return "VI_ERROR_INP_PROT_VIOL"; - case -1073807304: - return "VI_ERROR_BERR"; - case -1073807303: - return "VI_ERROR_IN_PROGRESS"; - case -1073807302: - return "VI_ERROR_INV_SETUP"; - case -1073807301: - return "VI_ERROR_QUEUE_ERROR"; - case -1073807300: - return "VI_ERROR_ALLOC"; - case -1073807299: - return "VI_ERROR_INV_MASK"; - case -1073807298: - return "VI_ERROR_IO"; - case -1073807297: - return "VI_ERROR_INV_FMT"; - case -1073807295: - return "VI_ERROR_NSUP_FMT"; - case -1073807294: - return "VI_ERROR_LINE_IN_USE"; - case -1073807290: - return "VI_ERROR_NSUP_MODE"; - case -1073807286: - return "VI_ERROR_SRQ_NOCCURRED"; - case -1073807282: - return "VI_ERROR_INV_SPACE"; - case -1073807279: - return "VI_ERROR_INV_OFFSET"; - case -1073807278: - return "VI_ERROR_INV_WIDTH"; - case -1073807276: - return "VI_ERROR_NSUP_OFFSET"; - case -1073807275: - return "VI_ERROR_NSUP_VAR_WIDTH"; - case -1073807273: - return "VI_ERROR_WINDOW_NMAPPED"; - case -1073807271: - return "VI_ERROR_RESP_PENDING"; - case -1073807265: - return "VI_ERROR_NLISTENERS"; - case -1073807264: - return "VI_ERROR_NCIC"; - case -1073807263: - return "VI_ERROR_NSYS_CNTLR"; - case -1073807257: - return "VI_ERROR_NSUP_OPER"; - case -1073807256: - return "VI_ERROR_INTR_PENDING"; - case -1073807254: - return "VI_ERROR_ASRL_PARITY"; - case -1073807253: - return "VI_ERROR_ASRL_FRAMING"; - case -1073807252: - return "VI_ERROR_ASRL_OVERRUN"; - case -1073807250: - return "VI_ERROR_TRIG_NMAPPED"; - case -1073807248: - return "VI_ERROR_NSUP_ALIGN_OFFSET"; - case -1073807247: - return "VI_ERROR_USER_BUF"; - case -1073807246: - return "VI_ERROR_RSRC_BUSY"; - case -1073807242: - return "VI_ERROR_NSUP_WIDTH"; - case -1073807240: - return "VI_ERROR_INV_PARAMETER"; - case -1073807239: - return "VI_ERROR_INV_PROT"; - case -1073807237: - return "VI_ERROR_INV_SIZE"; - case -1073807232: - return "VI_ERROR_WINDOW_MAPPED"; - case -1073807231: - return "VI_ERROR_NIMPL_OPER"; - case -1073807229: - return "VI_ERROR_INV_LENGTH"; - case -1073807215: - return "VI_ERROR_INV_MODE"; - case -1073807204: - return "VI_ERROR_SESN_NLOCKED"; - case -1073807203: - return "VI_ERROR_MEM_NSHARED"; - case -1073807202: - return "VI_ERROR_LIBRARY_NFOUND"; - case -1073807201: - return "VI_ERROR_NSUP_INTR"; - case -1073807200: - return "VI_ERROR_INV_LINE"; - case -1073807199: - return "VI_ERROR_FILE_ACCESS"; - case -1073807198: - return "VI_ERROR_FILE_IO"; - case -1073807197: - return "VI_ERROR_NSUP_LINE"; - case -1073807196: - return "VI_ERROR_NSUP_MECH"; - case -1073807195: - return "VI_ERROR_INTF_NUM_NCONFIG"; - case -1073807194: - return "VI_ERROR_CONN_LOST"; - case -1073807193: - return "VI_ERROR_MACHINE_NAVAIL"; - case -1073807192: - return "VI_ERROR_NPERMISSION"; - default: - return "Unknown error code"; - } - } -} diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/package.html b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/package.html deleted file mode 100644 index 12096aaf68..0000000000 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/visa/package.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - VISA - - - - Provides classes to access I/O functions in visa.h. - - diff --git a/wpilibj/wpilibJavaJNI/lib/SerialPortJNI.cpp b/wpilibj/wpilibJavaJNI/lib/SerialPortJNI.cpp new file mode 100644 index 0000000000..3b52cbb68c --- /dev/null +++ b/wpilibj/wpilibJavaJNI/lib/SerialPortJNI.cpp @@ -0,0 +1,288 @@ +#include +#include +#include "Log.hpp" + +#include "edu_wpi_first_wpilibj_hal_SerialPortJNI.h" + +#include "HAL/SerialPort.hpp" + +// set the logging level +TLogLevel serialJNILogLevel = logWARNING; + +#define SERIALJNI_LOG(level) \ + if (level > serialJNILogLevel) ; \ + else Log().Get(level) + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialInitializePort + * Signature: (BLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialInitializePort + (JNIEnv * env, jclass, jbyte port, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Calling Serial Initialize"; + SERIALJNI_LOG(logDEBUG) << "Port = " << (jint) port; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialInitializePort(port, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetBaudRate + * Signature: (BILjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetBaudRate + (JNIEnv * env, jclass, jbyte port, jint rate, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Baud Rate"; + SERIALJNI_LOG(logDEBUG) << "Baud: " << rate; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetBaudRate(port, rate, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetDataBits + * Signature: (BBLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetDataBits + (JNIEnv * env, jclass, jbyte port, jbyte bits, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Data Bits"; + SERIALJNI_LOG(logDEBUG) << "Data Bits: " << bits; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetDataBits(port, bits, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetParity + * Signature: (BBLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetParity + (JNIEnv * env, jclass, jbyte port, jbyte parity, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Parity"; + SERIALJNI_LOG(logDEBUG) << "Parity: " << parity; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetParity(port, parity, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetStopBits + * Signature: (BBLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetStopBits + (JNIEnv * env, jclass, jbyte port, jbyte bits, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Stop Bits"; + SERIALJNI_LOG(logDEBUG) << "Stop Bits: " << bits; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetStopBits(port, bits, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetWriteMode + * Signature: (BBLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetWriteMode + (JNIEnv * env, jclass, jbyte port, jbyte mode, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Write Mode"; + SERIALJNI_LOG(logDEBUG) << "Write mode: " << mode; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetWriteMode(port, mode, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetFlowControl + * Signature: (BBLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetFlowControl + (JNIEnv * env, jclass, jbyte port, jbyte flow, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Flow Control"; + SERIALJNI_LOG(logDEBUG) << "Flow Control: " << flow; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetFlowControl(port, flow, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetTimeout + * Signature: (BFLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetTimeout + (JNIEnv * env, jclass, jbyte port, jfloat timeout, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Timeout"; + SERIALJNI_LOG(logDEBUG) << "Timeout: " << timeout; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetTimeout(port, timeout, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialEnableTermination + * Signature: (BCLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialEnableTermination + (JNIEnv * env, jclass, jbyte port, jchar terminator, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Enable Termination"; + SERIALJNI_LOG(logDEBUG) << "Terminator: " << terminator; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialEnableTermination(port, terminator, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialDisableTermination + * Signature: (BLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialDisableTermination + (JNIEnv * env, jclass, jbyte port, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Disable termination"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialDisableTermination(port, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetReadBufferSize + * Signature: (BILjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetReadBufferSize + (JNIEnv * env, jclass, jbyte port, jint size, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Read Buffer Size"; + SERIALJNI_LOG(logDEBUG) << "Size: " << size; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetReadBufferSize(port, size, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialSetWriteBufferSize + * Signature: (BILjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialSetWriteBufferSize + (JNIEnv * env, jclass, jbyte port, jint size, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Setting Serial Write Buffer Size"; + SERIALJNI_LOG(logDEBUG) << "Size: " << size; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialSetWriteBufferSize(port, size, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialGetBytesRecieved + * Signature: (BLjava/nio/IntBuffer;)I + */ +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialGetBytesRecieved + (JNIEnv * env, jclass, jbyte port, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Serial Get Bytes Received"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jint retVal = serialGetBytesReceived(port, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; + return retVal; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialRead + * Signature: (BLjava/nio/ByteBuffer;ILjava/nio/IntBuffer;)I + */ +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialRead + (JNIEnv * env, jclass, jbyte port, jobject dataReceived, jint size, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Serial Read"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jbyte * dataReceivedPtr = NULL; + dataReceivedPtr = (jbyte*)env->GetDirectBufferAddress(dataReceived); + jint retVal = serialRead(port, (char*)dataReceivedPtr, size, statusPtr); + SERIALJNI_LOG(logDEBUG) << "ReturnValue = " << retVal; + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; + return retVal; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialWrite + * Signature: (BLjava/nio/ByteBuffer;ILjava/nio/IntBuffer;)I + */ +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialWrite + (JNIEnv * env, jclass, jbyte port, jobject dataToSend, jint size, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Serial Write"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jbyte * dataToSendPtr = NULL; + if(dataToSend != 0){ + dataToSendPtr = (jbyte*)env->GetDirectBufferAddress(dataToSend); + } + jint retVal = serialWrite(port, (const char*)dataToSendPtr, size, statusPtr); + SERIALJNI_LOG(logDEBUG) << "ReturnValue = " << retVal; + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; + return retVal; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialFlush + * Signature: (BLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialFlush + (JNIEnv * env, jclass, jbyte port, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Serial Flush"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialFlush(port, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialClear + * Signature: (BLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialClear + (JNIEnv * env, jclass, jbyte port, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Serial Clear"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialClear(port, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} + +/* + * Class: edu_wpi_first_wpilibj_hal_SerialPortJNI + * Method: serialClose + * Signature: (BLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialClose + (JNIEnv * env, jclass, jbyte port, jobject status) +{ + SERIALJNI_LOG(logDEBUG) << "Serial Close"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + serialClose(port, statusPtr); + SERIALJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +} diff --git a/wpilibj/wpilibJavaJNI/pom.xml b/wpilibj/wpilibJavaJNI/pom.xml index 6da2c6629c..ce97c5a616 100644 --- a/wpilibj/wpilibJavaJNI/pom.xml +++ b/wpilibj/wpilibJavaJNI/pom.xml @@ -127,6 +127,7 @@ this default location, specify a value for the 'embeddedJDKHome' property at the edu.wpi.first.wpilibj.hal.CompressorJNI edu.wpi.first.wpilibj.hal.PDPJNI edu.wpi.first.wpilibj.hal.PowerJNI + edu.wpi.first.wpilibj.hal.SerialPortJNI