per instance

This commit is contained in:
rusefillc 2022-02-13 14:07:11 -05:00
parent 60b90199bf
commit 80d9ff55a6
1 changed files with 96 additions and 35 deletions

View File

@ -54,6 +54,10 @@ public final class SerialPort
static private final String tmpdirAppIdProperty = "fazecast.jSerialComm.appid"; static private final String tmpdirAppIdProperty = "fazecast.jSerialComm.appid";
static private volatile boolean isAndroid = false; static private volatile boolean isAndroid = false;
static private volatile boolean isWindows = false; static private volatile boolean isWindows = false;
/**
* See https://stackoverflow.com/questions/442564/avoid-synchronizedthis-in-java for pros and cons
*/
private final Object lock = new Object();
static static
{ {
// Determine the temporary file directory for Java and remove any previous versions of this library // Determine the temporary file directory for Java and remove any previous versions of this library
@ -582,8 +586,9 @@ public final class SerialPort
* @param deviceReceiveQueueSize The requested size in bytes of the internal device driver's input queue (no effect on Linux/OSX) * @param deviceReceiveQueueSize The requested size in bytes of the internal device driver's input queue (no effect on Linux/OSX)
* @return Whether the port was successfully opened with a valid configuration. * @return Whether the port was successfully opened with a valid configuration.
*/ */
public final synchronized boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize) public final boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize)
{ {
synchronized (lock) {
// Synchronize this method to the class scope as well // Synchronize this method to the class scope as well
synchronized (SerialPort.class) synchronized (SerialPort.class)
{ {
@ -639,6 +644,7 @@ public final class SerialPort
return (portHandle != 0); return (portHandle != 0);
} }
} }
}
/** /**
* Opens this serial port for reading and writing with an optional delay time. * Opens this serial port for reading and writing with an optional delay time.
@ -682,8 +688,9 @@ public final class SerialPort
* *
* @return Whether the port was successfully closed. * @return Whether the port was successfully closed.
*/ */
public final synchronized boolean closePort() public final boolean closePort()
{ {
synchronized (lock) {
// Synchronize this method to the class scope as well // Synchronize this method to the class scope as well
synchronized (SerialPort.class) synchronized (SerialPort.class)
{ {
@ -694,13 +701,18 @@ public final class SerialPort
return (portHandle == 0); return (portHandle == 0);
} }
} }
}
/** /**
* Returns whether the port is currently open and available for communication. * Returns whether the port is currently open and available for communication.
* *
* @return Whether the port is opened. * @return Whether the port is opened.
*/ */
public final synchronized boolean isOpen() { return (portHandle != 0); } public final boolean isOpen() {
synchronized (lock) {
return (portHandle != 0);
}
}
/** /**
* Disables the library from calling any of the underlying device driver configuration methods. * Disables the library from calling any of the underlying device driver configuration methods.
@ -709,7 +721,11 @@ public final class SerialPort
* with buggy device drivers. In that case, this function <b>must</b> be called before attempting to * with buggy device drivers. In that case, this function <b>must</b> be called before attempting to
* open the port. * open the port.
*/ */
public final synchronized void disablePortConfiguration() { disableConfig = true; } public final void disablePortConfiguration() {
synchronized (lock) {
disableConfig = true;
}
}
/** /**
* Disables the library from obtaining an exclusive lock on the serial port. * Disables the library from obtaining an exclusive lock on the serial port.
@ -717,7 +733,11 @@ public final class SerialPort
* This function should never be called except on very specific systems which do not support obtaining * This function should never be called except on very specific systems which do not support obtaining
* exclusive locks on system resources. * exclusive locks on system resources.
*/ */
public final synchronized void disableExclusiveLock() { disableExclusiveLock = true; } public final void disableExclusiveLock() {
synchronized (lock) {
disableExclusiveLock = true;
}
}
/** /**
* Allows the library to request elevation of the current user's permissions for use in making certain * Allows the library to request elevation of the current user's permissions for use in making certain
@ -740,7 +760,11 @@ public final class SerialPort
* situations, this function may make it easier for your application to automatically apply these * situations, this function may make it easier for your application to automatically apply these
* necessary changes. * necessary changes.
*/ */
public final synchronized void allowElevatedPermissionsRequest() { requestElevatedPermissions = true; } public final void allowElevatedPermissionsRequest() {
synchronized (lock) {
requestElevatedPermissions = true;
}
}
/** /**
* Returns the source code line location of the latest error encountered during execution of * Returns the source code line location of the latest error encountered during execution of
@ -751,7 +775,11 @@ public final class SerialPort
* *
* @return Source line of latest native code error. * @return Source line of latest native code error.
*/ */
public final synchronized int getLastErrorLocation() { return getLastErrorLocation(portHandle); } public final int getLastErrorLocation() {
synchronized (lock) {
return getLastErrorLocation(portHandle);
}
}
/** /**
* Returns the error number returned by the most recent native source code line that failed execution. * Returns the error number returned by the most recent native source code line that failed execution.
@ -761,7 +789,11 @@ public final class SerialPort
* *
* @return Error number of the latest native code error. * @return Error number of the latest native code error.
*/ */
public final synchronized int getLastErrorCode() { return getLastErrorCode(portHandle); } public final int getLastErrorCode() {
synchronized (lock) {
return getLastErrorCode(portHandle);
}
}
// 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
@ -1035,8 +1067,9 @@ public final class SerialPort
* @see SerialPortMessageListener * @see SerialPortMessageListener
* @see SerialPortMessageListenerWithExceptions * @see SerialPortMessageListenerWithExceptions
*/ */
public final synchronized boolean addDataListener(SerialPortDataListener listener) public final boolean addDataListener(SerialPortDataListener listener)
{ {
synchronized (lock) {
if (userDataListener != null) if (userDataListener != null)
return false; return false;
userDataListener = listener; userDataListener = listener;
@ -1054,12 +1087,14 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Removes the associated {@link SerialPortDataListener} from the serial port interface. * Removes the associated {@link SerialPortDataListener} from the serial port interface.
*/ */
public final synchronized void removeDataListener() public final void removeDataListener()
{ {
synchronized (lock) {
eventFlags = 0; eventFlags = 0;
if (serialEventListener != null) if (serialEventListener != null)
{ {
@ -1070,6 +1105,7 @@ public final class SerialPort
} }
userDataListener = null; userDataListener = null;
} }
}
/** /**
* Returns an {@link java.io.InputStream} object associated with this serial port. * Returns an {@link java.io.InputStream} object associated with this serial port.
@ -1157,14 +1193,16 @@ public final class SerialPort
* *
* @return Whether the IO buffers were (or will be) successfully flushed. * @return Whether the IO buffers were (or will be) successfully flushed.
*/ */
public final synchronized boolean flushIOBuffers() public final boolean flushIOBuffers()
{ {
synchronized (lock) {
autoFlushIOBuffers = true; autoFlushIOBuffers = true;
if (portHandle != 0) if (portHandle != 0)
return flushRxTxBuffers(portHandle); return flushRxTxBuffers(portHandle);
return true; return true;
} }
}
/** /**
* Sets all serial port parameters at one time. * Sets all serial port parameters at one time.
@ -1232,8 +1270,9 @@ public final class SerialPort
* @see #MARK_PARITY * @see #MARK_PARITY
* @see #SPACE_PARITY * @see #SPACE_PARITY
*/ */
public final synchronized boolean setComPortParameters(int newBaudRate, int newDataBits, int newStopBits, int newParity, boolean useRS485Mode) public final boolean setComPortParameters(int newBaudRate, int newDataBits, int newStopBits, int newParity, boolean useRS485Mode)
{ {
synchronized (lock) {
baudRate = newBaudRate; baudRate = newBaudRate;
dataBits = newDataBits; dataBits = newDataBits;
stopBits = newStopBits; stopBits = newStopBits;
@ -1248,6 +1287,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Sets the serial port read and write timeout parameters. * Sets the serial port read and write timeout parameters.
@ -1298,8 +1338,9 @@ public final class SerialPort
* @param newWriteTimeout The number of milliseconds of inactivity to tolerate before returning from a {@link #writeBytes(byte[],long)} call (effective only on Windows). * @param newWriteTimeout The number of milliseconds of inactivity to tolerate before returning from a {@link #writeBytes(byte[],long)} call (effective only on Windows).
* @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened). * @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened).
*/ */
public final synchronized boolean setComPortTimeouts(int newTimeoutMode, int newReadTimeout, int newWriteTimeout) public final boolean setComPortTimeouts(int newTimeoutMode, int newReadTimeout, int newWriteTimeout)
{ {
synchronized (lock) {
timeoutMode = newTimeoutMode; timeoutMode = newTimeoutMode;
if (isWindows) if (isWindows)
{ {
@ -1319,6 +1360,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Sets the desired baud rate for this serial port. * Sets the desired baud rate for this serial port.
@ -1328,8 +1370,9 @@ public final class SerialPort
* @param newBaudRate The desired baud rate for this serial port. * @param newBaudRate The desired baud rate for this serial port.
* @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened). * @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened).
*/ */
public final synchronized boolean setBaudRate(int newBaudRate) public final boolean setBaudRate(int newBaudRate)
{ {
synchronized (lock) {
baudRate = newBaudRate; baudRate = newBaudRate;
if (portHandle != 0) if (portHandle != 0)
@ -1340,6 +1383,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Sets the desired number of data bits per word. * Sets the desired number of data bits per word.
@ -1349,8 +1393,9 @@ public final class SerialPort
* @param newDataBits The desired number of data bits per word. * @param newDataBits The desired number of data bits per word.
* @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened). * @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened).
*/ */
public final synchronized boolean setNumDataBits(int newDataBits) public final boolean setNumDataBits(int newDataBits)
{ {
synchronized (lock) {
dataBits = newDataBits; dataBits = newDataBits;
if (portHandle != 0) if (portHandle != 0)
@ -1361,6 +1406,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Sets the desired number of stop bits per word. * Sets the desired number of stop bits per word.
@ -1376,8 +1422,9 @@ public final class SerialPort
* @see #ONE_POINT_FIVE_STOP_BITS * @see #ONE_POINT_FIVE_STOP_BITS
* @see #TWO_STOP_BITS * @see #TWO_STOP_BITS
*/ */
public final synchronized boolean setNumStopBits(int newStopBits) public final boolean setNumStopBits(int newStopBits)
{ {
synchronized (lock) {
stopBits = newStopBits; stopBits = newStopBits;
if (portHandle != 0) if (portHandle != 0)
@ -1388,6 +1435,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Specifies what kind of flow control to enable for this serial port. * Specifies what kind of flow control to enable for this serial port.
@ -1424,8 +1472,9 @@ public final class SerialPort
* @see #FLOW_CONTROL_XONXOFF_IN_ENABLED * @see #FLOW_CONTROL_XONXOFF_IN_ENABLED
* @see #FLOW_CONTROL_XONXOFF_OUT_ENABLED * @see #FLOW_CONTROL_XONXOFF_OUT_ENABLED
*/ */
public final synchronized boolean setFlowControl(int newFlowControlSettings) public final boolean setFlowControl(int newFlowControlSettings)
{ {
synchronized (lock) {
flowControl = newFlowControlSettings; flowControl = newFlowControlSettings;
if (portHandle != 0) if (portHandle != 0)
@ -1436,6 +1485,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Sets the desired parity error-detection scheme to be used. * Sets the desired parity error-detection scheme to be used.
@ -1451,18 +1501,23 @@ public final class SerialPort
* @see #MARK_PARITY * @see #MARK_PARITY
* @see #SPACE_PARITY * @see #SPACE_PARITY
*/ */
public final synchronized boolean setParity(int newParity) public final boolean setParity(int newParity)
{ {
synchronized (lock) {
parity = newParity; parity = newParity;
if (portHandle != 0) if (portHandle != 0) {
{
if (safetySleepTimeMS > 0) if (safetySleepTimeMS > 0)
try { Thread.sleep(safetySleepTimeMS); } catch (Exception e) { Thread.currentThread().interrupt(); } try {
Thread.sleep(safetySleepTimeMS);
} catch (Exception e) {
Thread.currentThread().interrupt();
}
return configPort(portHandle); return configPort(portHandle);
} }
return true; return true;
} }
}
/** /**
* Sets RS-485 mode and its parameters for the device. This is a wrapper around * Sets RS-485 mode and its parameters for the device. This is a wrapper around
@ -1477,10 +1532,12 @@ public final class SerialPort
* @param delayAfterSendMicroseconds The time to wait after sending the last data bit before disabling transmit mode (effective only on Linux). * @param delayAfterSendMicroseconds The time to wait after sending the last data bit before disabling transmit mode (effective only on Linux).
* @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened). * @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened).
*/ */
public final synchronized boolean setRs485ModeParameters(boolean useRS485Mode, boolean rs485RtsActiveHigh, int delayBeforeSendMicroseconds, int delayAfterSendMicroseconds) public final boolean setRs485ModeParameters(boolean useRS485Mode, boolean rs485RtsActiveHigh, int delayBeforeSendMicroseconds, int delayAfterSendMicroseconds)
{ {
synchronized (lock) {
return setRs485ModeParameters(useRS485Mode, rs485RtsActiveHigh, false, false, delayBeforeSendMicroseconds, delayAfterSendMicroseconds); return setRs485ModeParameters(useRS485Mode, rs485RtsActiveHigh, false, false, delayBeforeSendMicroseconds, delayAfterSendMicroseconds);
} }
}
/** /**
* Sets RS-485 mode and its parameters for the device. * Sets RS-485 mode and its parameters for the device.
@ -1506,8 +1563,9 @@ public final class SerialPort
* @param delayAfterSendMicroseconds The time to wait after sending the last data bit before disabling transmit mode (effective only on Linux). * @param delayAfterSendMicroseconds The time to wait after sending the last data bit before disabling transmit mode (effective only on Linux).
* @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened). * @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened).
*/ */
public final synchronized boolean setRs485ModeParameters(boolean useRS485Mode, boolean rs485RtsActiveHigh, boolean enableTermination, boolean rxDuringTx,int delayBeforeSendMicroseconds, int delayAfterSendMicroseconds) public final boolean setRs485ModeParameters(boolean useRS485Mode, boolean rs485RtsActiveHigh, boolean enableTermination, boolean rxDuringTx,int delayBeforeSendMicroseconds, int delayAfterSendMicroseconds)
{ {
synchronized (lock) {
rs485Mode = useRS485Mode; rs485Mode = useRS485Mode;
rs485ActiveHigh = rs485RtsActiveHigh; rs485ActiveHigh = rs485RtsActiveHigh;
rs485EnableTermination = enableTermination; rs485EnableTermination = enableTermination;
@ -1523,6 +1581,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Sets custom XON/XOFF flow control characters for the device. * Sets custom XON/XOFF flow control characters for the device.
@ -1535,8 +1594,9 @@ public final class SerialPort
* @param xoffStopCharacter The decimal-based character to use as an XOFF signal. * @param xoffStopCharacter The decimal-based character to use as an XOFF signal.
* @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened). * @return Whether the port configuration is valid or disallowed on this system (only meaningful after the port is already opened).
*/ */
public final synchronized boolean setXonXoffCharacters(byte xonStartCharacter, byte xoffStopCharacter) public final boolean setXonXoffCharacters(byte xonStartCharacter, byte xoffStopCharacter)
{ {
synchronized (lock) {
xonStartChar = xonStartCharacter; xonStartChar = xonStartCharacter;
xoffStopChar = xoffStopCharacter; xoffStopChar = xoffStopCharacter;
@ -1548,6 +1608,7 @@ public final class SerialPort
} }
return true; return true;
} }
}
/** /**
* Gets a descriptive string representing this serial port or the device connected to it. * Gets a descriptive string representing this serial port or the device connected to it.