Added termios structure checking while port opening, and "Permission denied" exception support
This commit is contained in:
parent
82a01329a6
commit
c2c52e1e5c
|
@ -43,7 +43,7 @@
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
#include "../jssc_SerialNativeInterface.h"
|
#include "../jssc_SerialNativeInterface.h"
|
||||||
|
|
||||||
//#include <iostream.h> //-lCstd use for Solaris linker
|
#include <iostream> //-lCstd use for Solaris linker
|
||||||
|
|
||||||
|
|
||||||
/* OK */
|
/* OK */
|
||||||
|
@ -53,12 +53,21 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
|
||||||
jint hComm;
|
jint hComm;
|
||||||
hComm = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
|
hComm = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||||
if(hComm != -1){
|
if(hComm != -1){
|
||||||
#if defined TIOCEXCL && !defined __SunOS
|
//since 2.2.0 ->
|
||||||
ioctl(hComm, TIOCEXCL);//since 0.9
|
termios *settings = new termios();
|
||||||
#endif
|
if(tcgetattr(hComm, settings) == 0){
|
||||||
int flags = fcntl(hComm, F_GETFL, 0);
|
//<- since 2.2.0
|
||||||
flags &= ~O_NDELAY;
|
#if defined TIOCEXCL && !defined __SunOS
|
||||||
fcntl(hComm, F_SETFL, flags);
|
ioctl(hComm, TIOCEXCL);//since 0.9
|
||||||
|
#endif
|
||||||
|
int flags = fcntl(hComm, F_GETFL, 0);
|
||||||
|
flags &= ~O_NDELAY;
|
||||||
|
fcntl(hComm, F_SETFL, flags);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hComm = -2;
|
||||||
|
}
|
||||||
|
delete settings;
|
||||||
}
|
}
|
||||||
else {//since 0.9 ->
|
else {//since 0.9 ->
|
||||||
if(errno == EBUSY){//Port busy
|
if(errno == EBUSY){//Port busy
|
||||||
|
@ -67,6 +76,12 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
|
||||||
else if(errno == ENOENT){//Port not found
|
else if(errno == ENOENT){//Port not found
|
||||||
hComm = -2;
|
hComm = -2;
|
||||||
}
|
}
|
||||||
|
else if(errno == EACCES){//Permission denied
|
||||||
|
hComm = -3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hComm = -2;
|
||||||
|
}
|
||||||
}//<- since 0.9
|
}//<- since 0.9
|
||||||
env->ReleaseStringUTFChars(portName, port);
|
env->ReleaseStringUTFChars(portName, port);
|
||||||
return hComm;
|
return hComm;
|
||||||
|
|
Loading…
Reference in New Issue