mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
SerialPort: Use byte[] instead of ByteBuffer in JNI.
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
|
||||
import edu.wpi.first.wpilibj.hal.HAL;
|
||||
@@ -252,10 +251,13 @@ public class SerialPort {
|
||||
* @return An array of the read bytes
|
||||
*/
|
||||
public byte[] read(final int count) {
|
||||
ByteBuffer dataReceivedBuffer = ByteBuffer.allocateDirect(count);
|
||||
byte[] dataReceivedBuffer = new byte[count];
|
||||
int gotten = SerialPortJNI.serialRead(m_port, dataReceivedBuffer, count);
|
||||
if (gotten == count) {
|
||||
return dataReceivedBuffer;
|
||||
}
|
||||
byte[] retVal = new byte[gotten];
|
||||
dataReceivedBuffer.get(retVal);
|
||||
System.arraycopy(dataReceivedBuffer, 0, retVal, 0, gotten);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -267,9 +269,10 @@ public class SerialPort {
|
||||
* @return The number of bytes actually written into the port.
|
||||
*/
|
||||
public int write(byte[] buffer, int count) {
|
||||
ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(count);
|
||||
dataToSendBuffer.put(buffer, 0, count);
|
||||
return SerialPortJNI.serialWrite(m_port, dataToSendBuffer, count);
|
||||
if (buffer.length < count) {
|
||||
throw new IllegalArgumentException("buffer is too small, must be at least " + count);
|
||||
}
|
||||
return SerialPortJNI.serialWrite(m_port, buffer, count);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class OSSerialPortJNI extends JNIWrapper {
|
||||
public static native void serialInitializePort(byte port);
|
||||
|
||||
@@ -36,9 +34,9 @@ public class OSSerialPortJNI extends JNIWrapper {
|
||||
|
||||
public static native int serialGetBytesReceived(byte port);
|
||||
|
||||
public static native int serialRead(byte port, ByteBuffer buffer, int count);
|
||||
public static native int serialRead(byte port, byte[] buffer, int count);
|
||||
|
||||
public static native int serialWrite(byte port, ByteBuffer buffer, int count);
|
||||
public static native int serialWrite(byte port, byte[] buffer, int count);
|
||||
|
||||
public static native void serialFlush(byte port);
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SerialPortJNI extends JNIWrapper {
|
||||
public static native void serialInitializePort(byte port);
|
||||
|
||||
@@ -36,9 +34,9 @@ public class SerialPortJNI extends JNIWrapper {
|
||||
|
||||
public static native int serialGetBytesReceived(byte port);
|
||||
|
||||
public static native int serialRead(byte port, ByteBuffer buffer, int count);
|
||||
public static native int serialRead(byte port, byte[] buffer, int count);
|
||||
|
||||
public static native int serialWrite(byte port, ByteBuffer buffer, int count);
|
||||
public static native int serialWrite(byte port, byte[] buffer, int count);
|
||||
|
||||
public static native void serialFlush(byte port);
|
||||
|
||||
|
||||
@@ -13,8 +13,10 @@
|
||||
|
||||
#include "HAL/OSSerialPort.h"
|
||||
#include "HALUtil.h"
|
||||
#include "support/jni_util.h"
|
||||
|
||||
using namespace frc;
|
||||
using namespace wpi::java;
|
||||
|
||||
// set the logging level
|
||||
TLogLevel osserialJNILogLevel = logWARNING;
|
||||
@@ -237,16 +239,18 @@ Java_edu_wpi_first_wpilibj_hal_OSSerialPortJNI_serialGetBytesReceived(
|
||||
/*
|
||||
* Class: edu_wpi_first_wpilibj_hal_OSSerialPortJNI
|
||||
* Method: serialRead
|
||||
* Signature: (BLjava/nio/ByteBuffer;I)I
|
||||
* Signature: (B[BI)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_OSSerialPortJNI_serialRead(
|
||||
JNIEnv* env, jclass, jbyte port, jobject dataReceived, jint size) {
|
||||
JNIEnv* env, jclass, jbyte port, jbyteArray dataReceived, jint size) {
|
||||
SERIALJNI_LOG(logDEBUG) << "Serial Read";
|
||||
jbyte* dataReceivedPtr = nullptr;
|
||||
dataReceivedPtr = (jbyte*)env->GetDirectBufferAddress(dataReceived);
|
||||
llvm::SmallVector<char, 128> recvBuf;
|
||||
recvBuf.resize(size);
|
||||
int32_t status = 0;
|
||||
jint retVal = HAL_ReadOSSerial(static_cast<HAL_SerialPort>(port), reinterpret_cast<char*>(dataReceivedPtr),
|
||||
jint retVal = HAL_ReadOSSerial(static_cast<HAL_SerialPort>(port), recvBuf.data(),
|
||||
size, &status);
|
||||
env->SetByteArrayRegion(dataReceived, 0, size,
|
||||
reinterpret_cast<const jbyte *>(recvBuf.data()));
|
||||
SERIALJNI_LOG(logDEBUG) << "ReturnValue = " << retVal;
|
||||
SERIALJNI_LOG(logDEBUG) << "Status = " << status;
|
||||
CheckStatus(env, status);
|
||||
@@ -256,18 +260,17 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_OSSerialPortJNI_serialRead
|
||||
/*
|
||||
* Class: edu_wpi_first_wpilibj_hal_OSSerialPortJNI
|
||||
* Method: serialWrite
|
||||
* Signature: (BLjava/nio/ByteBuffer;I)I
|
||||
* Signature: (B[BI)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_OSSerialPortJNI_serialWrite(
|
||||
JNIEnv* env, jclass, jbyte port, jobject dataToSend, jint size) {
|
||||
JNIEnv* env, jclass, jbyte port, jbyteArray dataToSend, jint size) {
|
||||
SERIALJNI_LOG(logDEBUG) << "Serial Write";
|
||||
jbyte* dataToSendPtr = nullptr;
|
||||
if (dataToSend != 0) {
|
||||
dataToSendPtr = (jbyte*)env->GetDirectBufferAddress(dataToSend);
|
||||
}
|
||||
int32_t status = 0;
|
||||
jint retVal = HAL_WriteOSSerial(static_cast<HAL_SerialPort>(port), reinterpret_cast<char*>(dataToSendPtr),
|
||||
size, &status);
|
||||
jint retVal =
|
||||
HAL_WriteOSSerial(static_cast<HAL_SerialPort>(port),
|
||||
reinterpret_cast<const char *>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
size, &status);
|
||||
SERIALJNI_LOG(logDEBUG) << "ReturnValue = " << retVal;
|
||||
SERIALJNI_LOG(logDEBUG) << "Status = " << status;
|
||||
CheckStatus(env, status);
|
||||
|
||||
@@ -13,8 +13,10 @@
|
||||
|
||||
#include "HAL/SerialPort.h"
|
||||
#include "HALUtil.h"
|
||||
#include "support/jni_util.h"
|
||||
|
||||
using namespace frc;
|
||||
using namespace wpi::java;
|
||||
|
||||
// set the logging level
|
||||
TLogLevel serialJNILogLevel = logWARNING;
|
||||
@@ -237,16 +239,18 @@ Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialGetBytesReceived(
|
||||
/*
|
||||
* Class: edu_wpi_first_wpilibj_hal_SerialPortJNI
|
||||
* Method: serialRead
|
||||
* Signature: (BLjava/nio/ByteBuffer;I)I
|
||||
* Signature: (B[BI)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialRead(
|
||||
JNIEnv* env, jclass, jbyte port, jobject dataReceived, jint size) {
|
||||
JNIEnv* env, jclass, jbyte port, jbyteArray dataReceived, jint size) {
|
||||
SERIALJNI_LOG(logDEBUG) << "Serial Read";
|
||||
jbyte* dataReceivedPtr = nullptr;
|
||||
dataReceivedPtr = (jbyte*)env->GetDirectBufferAddress(dataReceived);
|
||||
llvm::SmallVector<char, 128> recvBuf;
|
||||
recvBuf.resize(size);
|
||||
int32_t status = 0;
|
||||
jint retVal = HAL_ReadSerial(static_cast<HAL_SerialPort>(port), reinterpret_cast<char*>(dataReceivedPtr),
|
||||
jint retVal = HAL_ReadSerial(static_cast<HAL_SerialPort>(port), recvBuf.data(),
|
||||
size, &status);
|
||||
env->SetByteArrayRegion(dataReceived, 0, size,
|
||||
reinterpret_cast<const jbyte *>(recvBuf.data()));
|
||||
SERIALJNI_LOG(logDEBUG) << "ReturnValue = " << retVal;
|
||||
SERIALJNI_LOG(logDEBUG) << "Status = " << status;
|
||||
CheckStatus(env, status);
|
||||
@@ -256,18 +260,17 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialRead(
|
||||
/*
|
||||
* Class: edu_wpi_first_wpilibj_hal_SerialPortJNI
|
||||
* Method: serialWrite
|
||||
* Signature: (BLjava/nio/ByteBuffer;I)I
|
||||
* Signature: (B[BI)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialWrite(
|
||||
JNIEnv* env, jclass, jbyte port, jobject dataToSend, jint size) {
|
||||
JNIEnv* env, jclass, jbyte port, jbyteArray dataToSend, jint size) {
|
||||
SERIALJNI_LOG(logDEBUG) << "Serial Write";
|
||||
jbyte* dataToSendPtr = nullptr;
|
||||
if (dataToSend != 0) {
|
||||
dataToSendPtr = (jbyte*)env->GetDirectBufferAddress(dataToSend);
|
||||
}
|
||||
int32_t status = 0;
|
||||
jint retVal = HAL_WriteSerial(static_cast<HAL_SerialPort>(port), reinterpret_cast<char*>(dataToSendPtr),
|
||||
size, &status);
|
||||
jint retVal =
|
||||
HAL_WriteSerial(static_cast<HAL_SerialPort>(port),
|
||||
reinterpret_cast<const char *>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
size, &status);
|
||||
SERIALJNI_LOG(logDEBUG) << "ReturnValue = " << retVal;
|
||||
SERIALJNI_LOG(logDEBUG) << "Status = " << status;
|
||||
CheckStatus(env, status);
|
||||
|
||||
Reference in New Issue
Block a user