forked from scream3r/java-simple-serial-connector
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from hiddenalpha/report-write-bytes-errors-as…
…-exceptions-20231108 Report writeBytes errors via java exceptions
- Loading branch information
Showing
5 changed files
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,11 @@ | |
* e-mail: [email protected] | ||
* web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ | ||
*/ | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <stdio.h> | ||
#include <fcntl.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <sys/ioctl.h> | ||
#include <termios.h> | ||
|
@@ -527,11 +529,21 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR | |
*/ | ||
JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes | ||
(JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){ | ||
jboolean ret = JNI_FALSE; | ||
jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE); | ||
jint bufferSize = env->GetArrayLength(buffer); | ||
jint result = write(portHandle, jBuffer, (size_t)bufferSize); | ||
if( result == -1 ){ | ||
int err = errno; /*bakup errno*/ | ||
jclass exClz = env->FindClass("java/io/IOException"); | ||
assert(exClz != NULL); | ||
env->ThrowNew(exClz, strerror(err)); | ||
goto Finally; | ||
} | ||
ret = (result == bufferSize) ? JNI_TRUE : JNI_FALSE; | ||
Finally: | ||
env->ReleaseByteArrayElements(buffer, jBuffer, 0); | ||
return result == bufferSize ? JNI_TRUE : JNI_FALSE; | ||
return ret; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters