Add option to disable exclusive lock
This commit is contained in:
parent
cf5f700ec0
commit
f0ff8c39cc
|
@ -64,6 +64,7 @@ jfieldID parityField;
|
||||||
jfieldID flowControlField;
|
jfieldID flowControlField;
|
||||||
jfieldID sendDeviceQueueSizeField;
|
jfieldID sendDeviceQueueSizeField;
|
||||||
jfieldID receiveDeviceQueueSizeField;
|
jfieldID receiveDeviceQueueSizeField;
|
||||||
|
jfieldID disableExclusiveLockField;
|
||||||
jfieldID rs485ModeField;
|
jfieldID rs485ModeField;
|
||||||
jfieldID rs485ActiveHighField;
|
jfieldID rs485ActiveHighField;
|
||||||
jfieldID rs485DelayBeforeField;
|
jfieldID rs485DelayBeforeField;
|
||||||
|
@ -229,6 +230,7 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar
|
||||||
flowControlField = (*env)->GetFieldID(env, serialCommClass, "flowControl", "I");
|
flowControlField = (*env)->GetFieldID(env, serialCommClass, "flowControl", "I");
|
||||||
sendDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "sendDeviceQueueSize", "I");
|
sendDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "sendDeviceQueueSize", "I");
|
||||||
receiveDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "receiveDeviceQueueSize", "I");
|
receiveDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "receiveDeviceQueueSize", "I");
|
||||||
|
disableExclusiveLockField = (*env)->GetFieldID(env, serialCommClass, "disableExclusiveLock", "Z");
|
||||||
rs485ModeField = (*env)->GetFieldID(env, serialCommClass, "rs485Mode", "Z");
|
rs485ModeField = (*env)->GetFieldID(env, serialCommClass, "rs485Mode", "Z");
|
||||||
rs485ActiveHighField = (*env)->GetFieldID(env, serialCommClass, "rs485ActiveHigh", "Z");
|
rs485ActiveHighField = (*env)->GetFieldID(env, serialCommClass, "rs485ActiveHigh", "Z");
|
||||||
rs485DelayBeforeField = (*env)->GetFieldID(env, serialCommClass, "rs485DelayBefore", "I");
|
rs485DelayBeforeField = (*env)->GetFieldID(env, serialCommClass, "rs485DelayBefore", "I");
|
||||||
|
@ -252,13 +254,14 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
||||||
unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField);
|
unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField);
|
||||||
unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField);
|
unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField);
|
||||||
unsigned char rs485ModeEnabled = (*env)->GetBooleanField(env, obj, rs485ModeField);
|
unsigned char rs485ModeEnabled = (*env)->GetBooleanField(env, obj, rs485ModeField);
|
||||||
|
unsigned char disableExclusiveLock = (*env)->GetBooleanField(env, obj, disableExclusiveLockField);
|
||||||
|
|
||||||
// Try to open existing serial port with read/write access
|
// Try to open existing serial port with read/write access
|
||||||
int serialPortFD = -1;
|
int serialPortFD = -1;
|
||||||
if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0)
|
if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0)
|
||||||
{
|
{
|
||||||
// Ensure that multiple root users cannot access the device simultaneously
|
// Ensure that multiple root users cannot access the device simultaneously
|
||||||
if (flock(serialPortFD, LOCK_EX | LOCK_NB) == -1)
|
if (!disableExclusiveLock && (flock(serialPortFD, LOCK_EX | LOCK_NB) == -1))
|
||||||
{
|
{
|
||||||
while ((close(serialPortFD) == -1) && (errno == EINTR))
|
while ((close(serialPortFD) == -1) && (errno == EINTR))
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
|
@ -393,8 +393,8 @@ public final class SerialPort
|
||||||
private volatile SerialPortDataListener userDataListener = null;
|
private volatile SerialPortDataListener userDataListener = null;
|
||||||
private volatile SerialPortEventListener serialEventListener = null;
|
private volatile SerialPortEventListener serialEventListener = null;
|
||||||
private volatile String comPort, friendlyName, portDescription;
|
private volatile String comPort, friendlyName, portDescription;
|
||||||
private volatile boolean eventListenerRunning = false, disableConfig = false, rs485Mode = false;
|
private volatile boolean eventListenerRunning = false, disableConfig = false, disableExclusiveLock = false;
|
||||||
private volatile boolean rs485ActiveHigh = true, isRtsEnabled = true, isDtrEnabled = true;
|
private volatile boolean rs485Mode = false, rs485ActiveHigh = true, isRtsEnabled = true, isDtrEnabled = true;
|
||||||
private SerialPortInputStream inputStream = null;
|
private SerialPortInputStream inputStream = null;
|
||||||
private SerialPortOutputStream outputStream = null;
|
private SerialPortOutputStream outputStream = null;
|
||||||
|
|
||||||
|
@ -520,6 +520,14 @@ public final class SerialPort
|
||||||
*/
|
*/
|
||||||
public final synchronized void disablePortConfiguration() { disableConfig = true; }
|
public final synchronized void disablePortConfiguration() { disableConfig = true; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables the library from obtaining an exclusive lock on the serial port.
|
||||||
|
* <p>
|
||||||
|
* This function should never be called except on very specific systems which do not support obtaining
|
||||||
|
* exclusive locks on system resources.
|
||||||
|
*/
|
||||||
|
public final synchronized void disableExclusiveLock() { disableExclusiveLock = true; }
|
||||||
|
|
||||||
// Serial Port Setup Methods
|
// Serial Port Setup Methods
|
||||||
private static native void initializeLibrary(); // Initializes the JNI code
|
private static native void initializeLibrary(); // Initializes the JNI code
|
||||||
private static native void uninitializeLibrary(); // Un-initializes the JNI code
|
private static native void uninitializeLibrary(); // Un-initializes the JNI code
|
||||||
|
|
Loading…
Reference in New Issue