Adds Direct port name Serial API (#956)

This commit is contained in:
Thad House
2018-03-05 19:41:09 -08:00
committed by Peter Johnson
parent 5175829bab
commit 14228d82f3
8 changed files with 126 additions and 1 deletions

View File

@@ -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.
*

View File

@@ -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);

View File

@@ -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()));