in src/main/java/com/fazecast/jSerialComm/SerialPort.java [592:650]
public final boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize)
{
configurationLock.lock();
try
{
// Set the send/receive internal buffer sizes, and return true if already opened
safetySleepTimeMS = safetySleepTime;
if (deviceSendQueueSize > 0)
sendDeviceQueueSize = deviceSendQueueSize;
if (deviceReceiveQueueSize > 0)
receiveDeviceQueueSize = deviceReceiveQueueSize;
if (portHandle != 0)
return (androidPort != null) ? androidPort.configPort(this) : configPort(portHandle);
// Force a sleep to ensure that the port does not become unusable due to rapid closing/opening on the part of the user
if (safetySleepTimeMS > 0)
try { Thread.sleep(safetySleepTimeMS); } catch (Exception e) { Thread.currentThread().interrupt(); }
// If this is an Android root application, we must explicitly allow serial port access to the library
File portFile = isAndroidDelete ? new File(comPort) : null;
if (portFile != null && (!portFile.canRead() || !portFile.canWrite()))
{
Process process = null;
try
{
process = Runtime.getRuntime().exec(new String[]{"su"});
DataOutputStream writer = new DataOutputStream(process.getOutputStream());
writer.writeBytes("chmod 666 " + comPort + "\n");
writer.writeBytes("exit\n");
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (reader.readLine() != null);
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
finally
{
if (process == null)
return false;
try { process.waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
try { process.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getErrorStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
}
}
// Natively open the serial port, and start an event-based listener if registered
portHandle = (androidPort != null) ? androidPort.openPortNative(this) : openPortNative();
if ((portHandle != 0) && (serialEventListener != null))
serialEventListener.startListening();
autoFlushIOBuffers = false;
return (portHandle != 0);
}
finally { configurationLock.unlock(); }
}