Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix: add SerialPort.read(byte[] buffer) #45

Open
wants to merge 1 commit into
base: 2.8.1-experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,36 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
return returnArray;
}

/*
* Read bytes
*/
JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_read
(JNIEnv *env, jobject object, jlong portHandle, jbyteArray buffer) {

jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
jint bufferSize = env->GetArrayLength(buffer);
int bytesRead, end;
fd_set read_fd_set;

do {
end = 1;
FD_ZERO(&read_fd_set);
FD_SET(portHandle, &read_fd_set);
select(portHandle + 1, &read_fd_set, NULL, NULL, NULL);
if ((bytesRead = read(portHandle, jBuffer, bufferSize)) <= 0) {
if ((bytesRead < 0) && (errno == EINTR))
end = 0;
}

} while (!end);

env->ReleaseByteArrayElements(buffer, jBuffer, 0);
return bytesRead;


}


/* OK */
/*
* Get bytes count in serial port buffers (Input and Output)
Expand Down
8 changes: 8 additions & 0 deletions src/main/cpp/jssc_SerialNativeInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR
JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
(JNIEnv *, jobject, jlong, jint);

/*
* Class: jssc_SerialNativeInterface
* Method: read
* Signature: (J[B)I
*/
JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_read
(JNIEnv *, jobject, jlong, jbyteArray);

/*
* Class: jssc_SerialNativeInterface
* Method: writeBytes
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/scream3r/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public boolean writeIntArray(int[] buffer) throws SerialPortException {
* Read byte array from port
*
* @param byteCount count of bytes for reading
*
*
* @return byte array with "byteCount" length
*
* @throws SerialPortException
Expand All @@ -438,6 +438,20 @@ public byte[] readBytes(int byteCount) throws SerialPortException {
return serialInterface.readBytes(portHandle, byteCount);
}

/**
* Read byte array from port
*
* @param buffer, read all bytes available into it
*
* @return bytes read
*
* @throws SerialPortException
*/
public int read(byte[] buffer) throws SerialPortException {
checkPortOpened("read()");
return serialInterface.read(portHandle, buffer);
}

/**
* Read string from port
*
Expand Down