mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Adds Direct port name Serial API (#956)
This commit is contained in:
committed by
Peter Johnson
parent
5175829bab
commit
14228d82f3
@@ -43,6 +43,13 @@ void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status) {
|
||||
if (*status > 0) *status = 0;
|
||||
}
|
||||
|
||||
void HAL_InitializeSerialPortDirect(HAL_SerialPort port, const char* portName,
|
||||
int32_t* status) {
|
||||
*status = viOpen(resourceManagerHandle, const_cast<char*>(portName), VI_NULL,
|
||||
VI_NULL, reinterpret_cast<ViSession*>(&portHandles[port]));
|
||||
if (*status > 0) *status = 0;
|
||||
}
|
||||
|
||||
void HAL_SetSerialBaudRate(HAL_SerialPort port, int32_t baud, int32_t* status) {
|
||||
*status = viSetAttribute(portHandles[port], VI_ATTR_ASRL_BAUD, baud);
|
||||
if (*status > 0) *status = 0;
|
||||
|
||||
@@ -21,6 +21,8 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status);
|
||||
void HAL_InitializeSerialPortDirect(HAL_SerialPort port, const char* portName,
|
||||
int32_t* status);
|
||||
void HAL_SetSerialBaudRate(HAL_SerialPort port, int32_t baud, int32_t* status);
|
||||
void HAL_SetSerialDataBits(HAL_SerialPort port, int32_t bits, int32_t* status);
|
||||
void HAL_SetSerialParity(HAL_SerialPort port, int32_t parity, int32_t* status);
|
||||
|
||||
@@ -16,6 +16,9 @@ void InitializeSerialPort() {}
|
||||
extern "C" {
|
||||
void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status) {}
|
||||
|
||||
void HAL_InitializeSerialPortDirect(HAL_SerialPort port, const char* portName,
|
||||
int32_t* status) {}
|
||||
|
||||
void HAL_SetSerialBaudRate(HAL_SerialPort port, int32_t baud, int32_t* status) {
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,57 @@ SerialPort::SerialPort(int baudRate, Port port, int dataBits,
|
||||
HAL_Report(HALUsageReporting::kResourceType_SerialPort, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of a Serial Port class.
|
||||
*
|
||||
* @param baudRate The baud rate to configure the serial port.
|
||||
* @param port The physical port to use
|
||||
* @param portName The direct port name to use
|
||||
* @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.
|
||||
* @param stopBits The number of stop bits to use as defined by the enum
|
||||
* StopBits.
|
||||
*/
|
||||
SerialPort::SerialPort(int baudRate, llvm::StringRef portName, Port port,
|
||||
int dataBits, SerialPort::Parity parity,
|
||||
SerialPort::StopBits stopBits) {
|
||||
int32_t status = 0;
|
||||
|
||||
m_port = port;
|
||||
|
||||
llvm::SmallVector<char, 64> buf;
|
||||
const char* portNameC = portName.c_str(buf);
|
||||
|
||||
HAL_InitializeSerialPortDirect(static_cast<HAL_SerialPort>(port), portNameC,
|
||||
&status);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <string>
|
||||
|
||||
#include <llvm/StringRef.h>
|
||||
#include <support/deprecated.h>
|
||||
|
||||
#include "ErrorBase.h"
|
||||
|
||||
@@ -56,6 +57,10 @@ class SerialPort : public ErrorBase {
|
||||
|
||||
SerialPort(int baudRate, Port port = kOnboard, int dataBits = 8,
|
||||
Parity parity = kParity_None, StopBits stopBits = kStopBits_One);
|
||||
WPI_DEPRECATED("Will be removed for 2019")
|
||||
SerialPort(int baudRate, llvm::StringRef portName, Port port = kOnboard,
|
||||
int dataBits = 8, Parity parity = kParity_None,
|
||||
StopBits stopBits = kStopBits_One);
|
||||
~SerialPort();
|
||||
|
||||
SerialPort(const SerialPort&) = delete;
|
||||
|
||||
@@ -94,6 +94,42 @@ public class SerialPort {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of a Serial Port class.
|
||||
*
|
||||
* @param baudRate The baud rate to configure the serial port.
|
||||
* @param port The Serial port to use
|
||||
* @param portName The direct portName to use
|
||||
* @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.
|
||||
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
|
||||
* @deprecated Will be removed for 2019
|
||||
*/
|
||||
@Deprecated
|
||||
public SerialPort(final int baudRate, String portName, Port port, final int dataBits,
|
||||
Parity parity, StopBits stopBits) {
|
||||
m_port = (byte) port.value;
|
||||
|
||||
SerialPortJNI.serialInitializePortDirect(m_port, portName);
|
||||
SerialPortJNI.serialSetBaudRate(m_port, baudRate);
|
||||
SerialPortJNI.serialSetDataBits(m_port, (byte) dataBits);
|
||||
SerialPortJNI.serialSetParity(m_port, (byte) parity.value);
|
||||
SerialPortJNI.serialSetStopBits(m_port, (byte) stopBits.value);
|
||||
|
||||
// Set the default read buffer size to 1 to return bytes immediately
|
||||
setReadBufferSize(1);
|
||||
|
||||
// Set the default timeout to 5 seconds.
|
||||
setTimeout(5.0);
|
||||
|
||||
// Don't wait until the buffer is full to transmit.
|
||||
setWriteBufferMode(WriteBufferMode.kFlushOnAccess);
|
||||
|
||||
disableTermination();
|
||||
|
||||
HAL.report(tResourceType.kResourceType_SerialPort, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of a Serial Port class.
|
||||
*
|
||||
|
||||
@@ -10,6 +10,8 @@ package edu.wpi.first.wpilibj.hal;
|
||||
public class SerialPortJNI extends JNIWrapper {
|
||||
public static native void serialInitializePort(byte port);
|
||||
|
||||
public static native void serialInitializePortDirect(byte port, String portName);
|
||||
|
||||
public static native void serialSetBaudRate(byte port, int baud);
|
||||
|
||||
public static native void serialSetDataBits(byte port, byte bits);
|
||||
|
||||
@@ -45,6 +45,25 @@ Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialInitializePort(
|
||||
CheckStatusForceThrow(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_wpilibj_hal_SerialPortJNI
|
||||
* Method: serialInitializePortDirect
|
||||
* Signature: (BLjava/lang/String;)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialInitializePortDirect(
|
||||
JNIEnv* env, jclass, jbyte port, jstring portName) {
|
||||
SERIALJNI_LOG(logDEBUG) << "Calling Serial Initialize Direct";
|
||||
SERIALJNI_LOG(logDEBUG) << "Port = " << (jint)port;
|
||||
JStringRef portNameRef{env, portName};
|
||||
SERIALJNI_LOG(logDEBUG) << "PortName = " << portNameRef.c_str();
|
||||
int32_t status = 0;
|
||||
HAL_InitializeSerialPortDirect(static_cast<HAL_SerialPort>(port),
|
||||
portNameRef.c_str(), &status);
|
||||
SERIALJNI_LOG(logDEBUG) << "Status = " << status;
|
||||
CheckStatusForceThrow(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_wpilibj_hal_SerialPortJNI
|
||||
* Method: serialSetBaudRate
|
||||
@@ -247,7 +266,7 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialRead(
|
||||
llvm::SmallVector<char, 128> recvBuf;
|
||||
recvBuf.resize(size);
|
||||
int32_t status = 0;
|
||||
jint retVal = HAL_ReadSerial(static_cast<HAL_SerialPort>(port), recvBuf.data(),
|
||||
jint retVal = HAL_ReadSerial(static_cast<HAL_SerialPort>(port), recvBuf.data(),
|
||||
size, &status);
|
||||
env->SetByteArrayRegion(dataReceived, 0, size,
|
||||
reinterpret_cast<const jbyte *>(recvBuf.data()));
|
||||
|
||||
Reference in New Issue
Block a user