Forced flush before close, monitors return value of close(), and releases exclusive lock of port before close.
This commit is contained in:
parent
ec0acd7ac4
commit
7821161dcc
14
INSTALL
14
INSTALL
|
@ -123,29 +123,29 @@ Maven:
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fazecast</groupId>
|
<groupId>com.fazecast</groupId>
|
||||||
<artifactId>jSerialComm</artifactId>
|
<artifactId>jSerialComm</artifactId>
|
||||||
<version>1.3.8</version>
|
<version>1.3.9</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
Ivy:
|
Ivy:
|
||||||
|
|
||||||
<dependency org="com.fazecast" name="jSerialComm" rev="1.3.8"/>
|
<dependency org="com.fazecast" name="jSerialComm" rev="1.3.9"/>
|
||||||
|
|
||||||
Groovy:
|
Groovy:
|
||||||
|
|
||||||
@Grab(group='com.fazecast', module='jSerialComm', version='1.3.8')
|
@Grab(group='com.fazecast', module='jSerialComm', version='1.3.9')
|
||||||
|
|
||||||
Gradle:
|
Gradle:
|
||||||
|
|
||||||
compile 'com.fazecast:jSerialComm:1.3.8'
|
compile 'com.fazecast:jSerialComm:1.3.9'
|
||||||
|
|
||||||
Buildr:
|
Buildr:
|
||||||
|
|
||||||
compile.with 'com.fazecast:jSerialComm:jar:1.3.8'
|
compile.with 'com.fazecast:jSerialComm:jar:1.3.9'
|
||||||
|
|
||||||
Scala/SBT:
|
Scala/SBT:
|
||||||
|
|
||||||
libraryDependencies += "com.fazecast" % "jSerialComm" % "1.3.8"
|
libraryDependencies += "com.fazecast" % "jSerialComm" % "1.3.9"
|
||||||
|
|
||||||
Leiningen:
|
Leiningen:
|
||||||
|
|
||||||
[com.fazecast/jSerialComm "1.3.8"]
|
[com.fazecast/jSerialComm "1.3.9"]
|
||||||
|
|
|
@ -4,7 +4,7 @@ apply plugin: 'maven'
|
||||||
|
|
||||||
group = 'com.fazecast'
|
group = 'com.fazecast'
|
||||||
archivesBaseName = 'jSerialComm'
|
archivesBaseName = 'jSerialComm'
|
||||||
version = '1.3.8'
|
version = '1.3.9'
|
||||||
|
|
||||||
sourceCompatibility = 1.6
|
sourceCompatibility = 1.6
|
||||||
targetCompatibility = 1.6
|
targetCompatibility = 1.6
|
||||||
|
|
|
@ -4,9 +4,10 @@ include $(CLEAR_VARS)
|
||||||
|
|
||||||
LOCAL_MODULE := jSerialComm
|
LOCAL_MODULE := jSerialComm
|
||||||
LOCAL_SRC_FILES := SerialPort_Android.c AndroidHelperFunctions.c
|
LOCAL_SRC_FILES := SerialPort_Android.c AndroidHelperFunctions.c
|
||||||
|
LOCAL_LDLIBS := -llog
|
||||||
|
|
||||||
include $(BUILD_SHARED_LIBRARY)
|
include $(BUILD_SHARED_LIBRARY)
|
||||||
|
|
||||||
all:
|
all:
|
||||||
cp -rf libs/* ../../resources/Android
|
cp -rf libs/* ../../resources/Android
|
||||||
rm -rf libs obj/* obj
|
rm -rf libs/* libs obj
|
||||||
|
|
|
@ -32,6 +32,8 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <asm/termios.h>
|
#include <asm/termios.h>
|
||||||
#include <asm/ioctls.h>
|
#include <asm/ioctls.h>
|
||||||
|
#include <linux/usbdevice_fs.h>
|
||||||
|
#include <asm/byteorder.h>
|
||||||
#ifndef BOTHER
|
#ifndef BOTHER
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -245,18 +247,23 @@ void setBaudRate(int portFD, int baudRate)
|
||||||
{
|
{
|
||||||
#ifdef BOTHER
|
#ifdef BOTHER
|
||||||
struct termios2 options = { 0 };
|
struct termios2 options = { 0 };
|
||||||
|
|
||||||
|
if (isatty(portFD))
|
||||||
ioctl(portFD, TCGETS2, &options);
|
ioctl(portFD, TCGETS2, &options);
|
||||||
options.c_cflag &= ~CBAUD;
|
options.c_cflag &= ~CBAUD;
|
||||||
options.c_cflag |= BOTHER;
|
options.c_cflag |= BOTHER;
|
||||||
options.c_ispeed = baudRate;
|
options.c_ispeed = baudRate;
|
||||||
options.c_ospeed = baudRate;
|
options.c_ospeed = baudRate;
|
||||||
|
if (isatty(portFD))
|
||||||
ioctl(portFD, TCSETS2, &options);
|
ioctl(portFD, TCSETS2, &options);
|
||||||
#else
|
#else
|
||||||
struct termios options = { 0 };
|
struct termios options = { 0 };
|
||||||
tcgetattr(portFD, &options);
|
if (isatty(portFD))
|
||||||
|
ioctl(portFD, TCGETS, &options);
|
||||||
cfsetispeed(&options, B38400);
|
cfsetispeed(&options, B38400);
|
||||||
cfsetospeed(&options, B38400);
|
cfsetospeed(&options, B38400);
|
||||||
tcsetattr(portFD, TCSANOW, &options);
|
if (isatty(portFD))
|
||||||
|
ioctl(portFD, TCSETS, &options);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* SerialPort_Android.c
|
* SerialPort_Android.c
|
||||||
*
|
*
|
||||||
* Created on: Mar 13, 2015
|
* Created on: Mar 13, 2015
|
||||||
* Last Updated on: May 19, 2015
|
* Last Updated on: Oct 09, 2015
|
||||||
* Author: Will Hedgecock
|
* Author: Will Hedgecock
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012-2015 Fazecast, Inc.
|
* Copyright (C) 2012-2015 Fazecast, Inc.
|
||||||
|
@ -27,7 +27,9 @@
|
||||||
#ifndef CMSPAR
|
#ifndef CMSPAR
|
||||||
#define CMSPAR 010000000000
|
#define CMSPAR 010000000000
|
||||||
#endif
|
#endif
|
||||||
|
#include <android/log.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
@ -35,9 +37,15 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <linux/usbdevice_fs.h>
|
||||||
|
#include <asm/byteorder.h>
|
||||||
#include "com_fazecast_jSerialComm_SerialPort.h"
|
#include "com_fazecast_jSerialComm_SerialPort.h"
|
||||||
#include "AndroidHelperFunctions.h"
|
#include "AndroidHelperFunctions.h"
|
||||||
|
|
||||||
|
// Logging defines
|
||||||
|
#define LOG_TAG "com.fazecast.jSerialComm"
|
||||||
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
|
||||||
|
|
||||||
// Cached class, method, and field IDs
|
// Cached class, method, and field IDs
|
||||||
jclass serialCommClass;
|
jclass serialCommClass;
|
||||||
jmethodID serialCommConstructor;
|
jmethodID serialCommConstructor;
|
||||||
|
@ -104,7 +112,7 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_uninitializeLibrary(JNIEnv *env, jclass serialComm)
|
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_uninitializeLibrary(JNIEnv *env, jclass serialComm)
|
||||||
{
|
{
|
||||||
// Delete the cache global reference
|
// Delete the cached global reference
|
||||||
(*env)->DeleteGlobalRef(env, serialCommClass);
|
(*env)->DeleteGlobalRef(env, serialCommClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +132,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Close the port if there was a problem setting the parameters
|
// Close the port if there was a problem setting the parameters
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
}
|
}
|
||||||
|
@ -150,10 +158,12 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
||||||
tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR);
|
tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR);
|
||||||
|
|
||||||
// Clear any serial port flags
|
// Clear any serial port flags
|
||||||
|
if (isatty(serialPortFD))
|
||||||
fcntl(serialPortFD, F_SETFL, 0);
|
fcntl(serialPortFD, F_SETFL, 0);
|
||||||
|
|
||||||
// Set raw-mode to allow the use of tcsetattr() and ioctl()
|
// Set raw-mode to allow the use of ioctl()
|
||||||
tcgetattr(serialPortFD, &options);
|
if (isatty(serialPortFD))
|
||||||
|
ioctl(serialPortFD, TCGETS, &options);
|
||||||
cfmakeraw(&options);
|
cfmakeraw(&options);
|
||||||
|
|
||||||
// Set updated port parameters
|
// Set updated port parameters
|
||||||
|
@ -175,8 +185,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
int retVal = -1;
|
||||||
ioctl(serialPortFD, TIOCEXCL); // Block non-root users from using this port
|
if (isatty(serialPortFD))
|
||||||
|
retVal = ioctl(serialPortFD, TCSETS, &options);
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||||
|
@ -198,14 +209,17 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowCo
|
||||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||||
|
|
||||||
// Retrieve existing port configuration
|
// Retrieve existing port configuration
|
||||||
tcgetattr(serialPortFD, &options);
|
if (isatty(serialPortFD))
|
||||||
|
ioctl(serialPortFD, TCGETS, &options);
|
||||||
|
|
||||||
// Set updated port parameters
|
// Set updated port parameters
|
||||||
options.c_cflag |= CTSRTSEnabled;
|
options.c_cflag |= CTSRTSEnabled;
|
||||||
options.c_iflag |= XonXoffInEnabled | XonXoffOutEnabled;
|
options.c_iflag |= XonXoffInEnabled | XonXoffOutEnabled;
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
int retVal = -1;
|
||||||
|
if (isatty(serialPortFD))
|
||||||
|
retVal = ioctl(serialPortFD, TCSETS, &options);
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||||
|
@ -223,8 +237,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
|
||||||
|
|
||||||
// Retrieve existing port configuration
|
// Retrieve existing port configuration
|
||||||
struct termios options = { 0 };
|
struct termios options = { 0 };
|
||||||
tcgetattr(serialPortFD, &options);
|
if (isatty(serialPortFD))
|
||||||
|
ioctl(serialPortFD, TCGETS, &options);
|
||||||
int flags = fcntl(serialPortFD, F_GETFL);
|
int flags = fcntl(serialPortFD, F_GETFL);
|
||||||
|
if (flags == -1)
|
||||||
|
return JNI_FALSE;
|
||||||
|
|
||||||
// Set updated port timeouts
|
// Set updated port timeouts
|
||||||
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
|
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
|
||||||
|
@ -265,8 +282,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
fcntl(serialPortFD, F_SETFL, flags);
|
int retVal = fcntl(serialPortFD, F_SETFL, flags);
|
||||||
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
if ((retVal != -1) && isatty(serialPortFD))
|
||||||
|
retVal = ioctl(serialPortFD, TCSETS, &options);
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||||
|
@ -283,24 +301,27 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
|
||||||
int eventsToMonitor = (*env)->GetIntField(env, obj, eventFlagsField);
|
int eventsToMonitor = (*env)->GetIntField(env, obj, eventFlagsField);
|
||||||
|
|
||||||
// Change read timeouts if we are monitoring data received
|
// Change read timeouts if we are monitoring data received
|
||||||
|
jboolean retVal;
|
||||||
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
|
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
|
||||||
{
|
{
|
||||||
struct termios options = { 0 };
|
struct termios options = { 0 };
|
||||||
tcgetattr(serialPortFD, &options);
|
ioctl(serialPortFD, TCGETS, &options);
|
||||||
int flags = fcntl(serialPortFD, F_GETFL);
|
int flags = fcntl(serialPortFD, F_GETFL);
|
||||||
|
if (flags == -1)
|
||||||
|
return JNI_FALSE;
|
||||||
flags &= ~O_NONBLOCK;
|
flags &= ~O_NONBLOCK;
|
||||||
options.c_cc[VMIN] = 0;
|
options.c_cc[VMIN] = 0;
|
||||||
options.c_cc[VTIME] = 10;
|
options.c_cc[VTIME] = 10;
|
||||||
fcntl(serialPortFD, F_SETFL, flags);
|
retVal = ((fcntl(serialPortFD, F_SETFL, flags) == -1) || (ioctl(serialPortFD, TCSETS, &options) == -1)) ?
|
||||||
tcsetattr(serialPortFD, TCSANOW, &options);
|
JNI_FALSE : JNI_TRUE;
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
|
retVal = Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
return JNI_TRUE;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
|
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||||
|
@ -315,7 +336,8 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI
|
||||||
FD_SET(serialPortFD, &waitingSet);
|
FD_SET(serialPortFD, &waitingSet);
|
||||||
|
|
||||||
// Wait for a serial port event
|
// Wait for a serial port event
|
||||||
int retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
|
int retVal;
|
||||||
|
do { retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout); } while ((retVal < 0) && (errno == EINTR));
|
||||||
if (retVal <= 0)
|
if (retVal <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
|
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
|
||||||
|
@ -326,7 +348,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
|
||||||
// Close port
|
// Close port
|
||||||
if (serialPortFD <= 0)
|
if (serialPortFD <= 0)
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -359,10 +381,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
// While there are more bytes we are supposed to read
|
// While there are more bytes we are supposed to read
|
||||||
while (bytesRemaining > 0)
|
while (bytesRemaining > 0)
|
||||||
{
|
{
|
||||||
if ((numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining)) == -1)
|
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -389,10 +412,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
// While there are more bytes we are supposed to read and the timeout has not elapsed
|
// While there are more bytes we are supposed to read and the timeout has not elapsed
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if ((numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining)) == -1)
|
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -411,10 +435,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
else // Semi- or non-blocking specified
|
else // Semi- or non-blocking specified
|
||||||
{
|
{
|
||||||
// Read from port
|
// Read from port
|
||||||
if ((numBytesRead = read(serialPortFD, readBuffer, bytesToRead)) == -1)
|
do { numBytesRead = read(serialPortFD, readBuffer, bytesToRead); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -437,10 +462,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
|
||||||
int numBytesWritten;
|
int numBytesWritten;
|
||||||
|
|
||||||
// Write to port
|
// Write to port
|
||||||
if ((numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite)) == -1)
|
do { numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
|
||||||
|
if (numBytesWritten == -1)
|
||||||
{
|
{
|
||||||
// Problem writing, close port
|
// Problem writing, close port
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
|
|
@ -87,6 +87,14 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_uninitializeLibr
|
||||||
JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative
|
JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative
|
||||||
(JNIEnv *, jobject);
|
(JNIEnv *, jobject);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: com_fazecast_jSerialComm_SerialPort
|
||||||
|
* Method: associateNativeHandle
|
||||||
|
* Signature: (J)Z
|
||||||
|
*/
|
||||||
|
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_associateNativeHandle
|
||||||
|
(JNIEnv *, jobject, jlong);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Class: com_fazecast_jSerialComm_SerialPort
|
* Class: com_fazecast_jSerialComm_SerialPort
|
||||||
* Method: closePortNative
|
* Method: closePortNative
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* SerialPort_Linux.c
|
* SerialPort_Linux.c
|
||||||
*
|
*
|
||||||
* Created on: Feb 25, 2012
|
* Created on: Feb 25, 2012
|
||||||
* Last Updated on: May 04, 2015
|
* Last Updated on: Oct 09, 2015
|
||||||
* Author: Will Hedgecock
|
* Author: Will Hedgecock
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012-2015 Fazecast, Inc.
|
* Copyright (C) 2012-2015 Fazecast, Inc.
|
||||||
|
@ -126,7 +126,9 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Close the port if there was a problem setting the parameters
|
// Close the port if there was a problem setting the parameters
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
}
|
}
|
||||||
|
@ -179,7 +181,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
||||||
ioctl(serialPortFD, TIOCEXCL); // Block non-root users from using this port
|
ioctl(serialPortFD, TIOCEXCL); // Block non-root users from opening this port
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||||
|
@ -228,6 +230,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
|
||||||
struct termios options = { 0 };
|
struct termios options = { 0 };
|
||||||
tcgetattr(serialPortFD, &options);
|
tcgetattr(serialPortFD, &options);
|
||||||
int flags = fcntl(serialPortFD, F_GETFL);
|
int flags = fcntl(serialPortFD, F_GETFL);
|
||||||
|
if (flags == -1)
|
||||||
|
return JNI_FALSE;
|
||||||
|
|
||||||
// Set updated port timeouts
|
// Set updated port timeouts
|
||||||
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
|
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
|
||||||
|
@ -268,8 +272,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
fcntl(serialPortFD, F_SETFL, flags);
|
int retVal = fcntl(serialPortFD, F_SETFL, flags);
|
||||||
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
if (retVal != -1)
|
||||||
|
retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||||
|
@ -286,24 +291,27 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
|
||||||
int eventsToMonitor = (*env)->GetIntField(env, obj, eventFlagsField);
|
int eventsToMonitor = (*env)->GetIntField(env, obj, eventFlagsField);
|
||||||
|
|
||||||
// Change read timeouts if we are monitoring data received
|
// Change read timeouts if we are monitoring data received
|
||||||
|
jboolean retVal;
|
||||||
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
|
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
|
||||||
{
|
{
|
||||||
struct termios options = { 0 };
|
struct termios options = { 0 };
|
||||||
tcgetattr(serialPortFD, &options);
|
tcgetattr(serialPortFD, &options);
|
||||||
int flags = fcntl(serialPortFD, F_GETFL);
|
int flags = fcntl(serialPortFD, F_GETFL);
|
||||||
|
if (flags == -1)
|
||||||
|
return JNI_FALSE;
|
||||||
flags &= ~O_NONBLOCK;
|
flags &= ~O_NONBLOCK;
|
||||||
options.c_cc[VMIN] = 0;
|
options.c_cc[VMIN] = 0;
|
||||||
options.c_cc[VTIME] = 10;
|
options.c_cc[VTIME] = 10;
|
||||||
fcntl(serialPortFD, F_SETFL, flags);
|
retVal = ((fcntl(serialPortFD, F_SETFL, flags) == -1) || (tcsetattr(serialPortFD, TCSANOW, &options) == -1)) ?
|
||||||
tcsetattr(serialPortFD, TCSANOW, &options);
|
JNI_FALSE : JNI_TRUE;
|
||||||
if (baudRateCode == 0) // Set custom baud rate
|
if (baudRateCode == 0) // Set custom baud rate
|
||||||
setBaudRate(serialPortFD, baudRate);
|
setBaudRate(serialPortFD, baudRate);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
|
retVal = Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
return JNI_TRUE;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
|
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||||
|
@ -330,7 +338,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
|
||||||
// Close port
|
// Close port
|
||||||
if (serialPortFD <= 0)
|
if (serialPortFD <= 0)
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
close(serialPortFD);
|
|
||||||
|
// Allow others to open this port
|
||||||
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -366,8 +378,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
if (numBytesRead == -1)
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -397,8 +411,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
if (numBytesRead == -1)
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -420,8 +436,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
do { numBytesRead = read(serialPortFD, readBuffer, bytesToRead); } while ((numBytesRead < 0) && (errno == EINTR));
|
do { numBytesRead = read(serialPortFD, readBuffer, bytesToRead); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
if (numBytesRead == -1)
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -447,8 +465,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
|
||||||
do { numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
|
do { numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
|
||||||
if (numBytesWritten == -1)
|
if (numBytesWritten == -1)
|
||||||
{
|
{
|
||||||
// Problem writing, close port
|
// Problem writing, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* SerialPort_OSX.c
|
* SerialPort_OSX.c
|
||||||
*
|
*
|
||||||
* Created on: Feb 25, 2012
|
* Created on: Feb 25, 2012
|
||||||
* Last Updated on: July 1, 2015
|
* Last Updated on: Oct 09, 2015
|
||||||
* Author: Will Hedgecock
|
* Author: Will Hedgecock
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012-2015 Fazecast, Inc.
|
* Copyright (C) 2012-2015 Fazecast, Inc.
|
||||||
|
@ -177,7 +177,9 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Close the port if there was a problem setting the parameters
|
// Close the port if there was a problem setting the parameters
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
}
|
}
|
||||||
|
@ -273,6 +275,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
|
||||||
struct termios options;
|
struct termios options;
|
||||||
tcgetattr(serialPortFD, &options);
|
tcgetattr(serialPortFD, &options);
|
||||||
int flags = fcntl(serialPortFD, F_GETFL);
|
int flags = fcntl(serialPortFD, F_GETFL);
|
||||||
|
if (flags == -1)
|
||||||
|
return JNI_FALSE;
|
||||||
|
|
||||||
// Set updated port timeouts
|
// Set updated port timeouts
|
||||||
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
|
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
|
||||||
|
@ -313,8 +317,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply changes
|
// Apply changes
|
||||||
fcntl(serialPortFD, F_SETFL, flags);
|
int retVal = fcntl(serialPortFD, F_SETFL, flags);
|
||||||
return (tcsetattr(serialPortFD, TCSANOW, &options) == 0) ? JNI_TRUE : JNI_FALSE;
|
return ((retVal != -1) && (tcsetattr(serialPortFD, TCSANOW, &options) == 0)) ? JNI_TRUE : JNI_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(JNIEnv *env, jobject obj, jlong serialPortFD)
|
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||||
|
@ -332,11 +336,13 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
|
||||||
struct termios options;
|
struct termios options;
|
||||||
tcgetattr(serialPortFD, &options);
|
tcgetattr(serialPortFD, &options);
|
||||||
int flags = fcntl(serialPortFD, F_GETFL);
|
int flags = fcntl(serialPortFD, F_GETFL);
|
||||||
|
if (flags == -1)
|
||||||
|
return JNI_FALSE;
|
||||||
flags &= ~O_NONBLOCK;
|
flags &= ~O_NONBLOCK;
|
||||||
options.c_cc[VMIN] = 0;
|
options.c_cc[VMIN] = 0;
|
||||||
options.c_cc[VTIME] = 10;
|
options.c_cc[VTIME] = 10;
|
||||||
fcntl(serialPortFD, F_SETFL, flags);
|
retVal = ((fcntl(serialPortFD, F_SETFL, flags) != -1) && (tcsetattr(serialPortFD, TCSANOW, &options) != -1)) ?
|
||||||
retVal = (tcsetattr(serialPortFD, TCSANOW, &options) == 0) ? JNI_TRUE : JNI_FALSE;
|
JNI_TRUE : JNI_FALSE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
retVal = Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
|
retVal = Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
|
||||||
|
@ -368,8 +374,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
|
||||||
// Close port
|
// Close port
|
||||||
if (serialPortFD <= 0)
|
if (serialPortFD <= 0)
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
|
|
||||||
|
// Allow others to open the port and close it ourselves
|
||||||
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
tcdrain(serialPortFD);
|
tcdrain(serialPortFD);
|
||||||
close(serialPortFD);
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -405,8 +414,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
if (numBytesRead == -1)
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -436,8 +447,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
if (numBytesRead == -1)
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -459,8 +472,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
do { numBytesRead = read(serialPortFD, readBuffer, bytesToRead); } while ((numBytesRead < 0) && (errno == EINTR));
|
do { numBytesRead = read(serialPortFD, readBuffer, bytesToRead); } while ((numBytesRead < 0) && (errno == EINTR));
|
||||||
if (numBytesRead == -1)
|
if (numBytesRead == -1)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -486,8 +501,10 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
|
||||||
do { numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
|
do { numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
|
||||||
if (numBytesWritten == -1)
|
if (numBytesWritten == -1)
|
||||||
{
|
{
|
||||||
// Problem writing, close port
|
// Problem writing, allow others to open the port and close it ourselves
|
||||||
close(serialPortFD);
|
ioctl(serialPortFD, TIOCNXCL);
|
||||||
|
tcdrain(serialPortFD);
|
||||||
|
while ((close(serialPortFD) == -1) && (errno != EBADF));
|
||||||
serialPortFD = -1;
|
serialPortFD = -1;
|
||||||
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
|
||||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
|
||||||
|
|
|
@ -263,7 +263,8 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Close the port if there was a problem setting the parameters
|
// Close the port if there was a problem setting the parameters
|
||||||
CloseHandle(serialPortHandle);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
}
|
}
|
||||||
|
@ -474,7 +475,8 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI
|
||||||
if (GetLastError() != ERROR_IO_PENDING) // Problem occurred
|
if (GetLastError() != ERROR_IO_PENDING) // Problem occurred
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
CloseHandle(serialPortHandle);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -498,12 +500,12 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
|
||||||
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
|
||||||
// Close port
|
// Close port
|
||||||
BOOL retVal = CloseHandle(serialPortHandle);
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
|
|
||||||
return (retVal == 0) ? JNI_FALSE : JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(JNIEnv *env, jobject obj, jlong serialPortFD)
|
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||||
|
@ -542,7 +544,8 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
if (GetLastError() != ERROR_IO_PENDING) // Problem occurred
|
if (GetLastError() != ERROR_IO_PENDING) // Problem occurred
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
CloseHandle(serialPortHandle);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -550,7 +553,8 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
|
||||||
else if ((result = GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesRead, TRUE)) == FALSE)
|
else if ((result = GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesRead, TRUE)) == FALSE)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
CloseHandle(serialPortHandle);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -586,7 +590,8 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
|
||||||
if (GetLastError() != ERROR_IO_PENDING)
|
if (GetLastError() != ERROR_IO_PENDING)
|
||||||
{
|
{
|
||||||
// Problem writing, close port
|
// Problem writing, close port
|
||||||
CloseHandle(serialPortHandle);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
|
@ -594,7 +599,8 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
|
||||||
else if ((result = GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesWritten, TRUE)) == FALSE)
|
else if ((result = GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesWritten, TRUE)) == FALSE)
|
||||||
{
|
{
|
||||||
// Problem reading, close port
|
// Problem reading, close port
|
||||||
CloseHandle(serialPortHandle);
|
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
|
||||||
|
while (!CloseHandle(serialPortHandle));
|
||||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||||
|
|
|
@ -31,7 +31,7 @@ import java.util.EventListener;
|
||||||
* This interface must be implemented to enable simple event-based serial port I/O.
|
* This interface must be implemented to enable simple event-based serial port I/O.
|
||||||
*
|
*
|
||||||
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
||||||
* @version 1.3.8
|
* @version 1.3.9
|
||||||
* @see java.util.EventListener
|
* @see java.util.EventListener
|
||||||
*/
|
*/
|
||||||
public interface SerialPortDataListener extends EventListener
|
public interface SerialPortDataListener extends EventListener
|
||||||
|
|
|
@ -31,7 +31,7 @@ import java.util.EventObject;
|
||||||
* This class describes an asynchronous serial port event.
|
* This class describes an asynchronous serial port event.
|
||||||
*
|
*
|
||||||
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
||||||
* @version 1.3.8
|
* @version 1.3.9
|
||||||
* @see java.util.EventObject
|
* @see java.util.EventObject
|
||||||
*/
|
*/
|
||||||
public final class SerialPortEvent extends EventObject
|
public final class SerialPortEvent extends EventObject
|
||||||
|
|
|
@ -31,7 +31,7 @@ package com.fazecast.jSerialComm;
|
||||||
* <i>Note</i>: Using this interface will negate any serial port read timeout settings since they make no sense in an asynchronous context.
|
* <i>Note</i>: Using this interface will negate any serial port read timeout settings since they make no sense in an asynchronous context.
|
||||||
*
|
*
|
||||||
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
||||||
* @version 1.3.8
|
* @version 1.3.9
|
||||||
* @see com.fazecast.jSerialComm.SerialPortDataListener
|
* @see com.fazecast.jSerialComm.SerialPortDataListener
|
||||||
* @see java.util.EventListener
|
* @see java.util.EventListener
|
||||||
*/
|
*/
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -32,7 +32,7 @@ import java.util.Scanner;
|
||||||
* This class provides a test case for the jSerialComm library.
|
* This class provides a test case for the jSerialComm library.
|
||||||
*
|
*
|
||||||
* @author Will Hedgecock <will.hedgecock@gmail.com>
|
* @author Will Hedgecock <will.hedgecock@gmail.com>
|
||||||
* @version 1.3.8
|
* @version 1.3.9
|
||||||
* @see java.io.InputStream
|
* @see java.io.InputStream
|
||||||
* @see java.io.OutputStream
|
* @see java.io.OutputStream
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue