mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-31 02:17:23 +00:00 
			
		
		
		
	new SerialInputOutputManager.start() method
Previously recommended start action `Executors.newSingleThreadExecutor().submit(ioManager)` did not shutdown the Executor, which caused a thread leak. It's still possible to use old style start, as SerialInputOutputManager continues to implement Runnable interface.
This commit is contained in:
		
							parent
							
								
									848d4e7713
								
							
						
					
					
						commit
						128d1a00b1
					
				| @ -81,7 +81,7 @@ then use direct read/write | |||||||
| or direct write + event driven read: | or direct write + event driven read: | ||||||
| ```java | ```java | ||||||
|     usbIoManager = new SerialInputOutputManager(usbSerialPort, this); |     usbIoManager = new SerialInputOutputManager(usbSerialPort, this); | ||||||
|     Executors.newSingleThreadExecutor().submit(usbIoManager); |     usbIoManager.start(); | ||||||
|     ... |     ... | ||||||
|     port.write("hello".getBytes(), WRITE_WAIT_MILLIS); |     port.write("hello".getBytes(), WRITE_WAIT_MILLIS); | ||||||
|      |      | ||||||
|  | |||||||
| @ -38,7 +38,6 @@ import com.hoho.android.usbserial.util.SerialInputOutputManager; | |||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.EnumSet; | import java.util.EnumSet; | ||||||
| import java.util.concurrent.Executors; |  | ||||||
| 
 | 
 | ||||||
| public class TerminalFragment extends Fragment implements SerialInputOutputManager.Listener { | public class TerminalFragment extends Fragment implements SerialInputOutputManager.Listener { | ||||||
| 
 | 
 | ||||||
| @ -229,7 +228,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag | |||||||
|             usbSerialPort.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); |             usbSerialPort.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|             if(withIoManager) { |             if(withIoManager) { | ||||||
|                 usbIoManager = new SerialInputOutputManager(usbSerialPort, this); |                 usbIoManager = new SerialInputOutputManager(usbSerialPort, this); | ||||||
|                 Executors.newSingleThreadExecutor().submit(usbIoManager); |                 usbIoManager.start(); | ||||||
|             } |             } | ||||||
|             status("connected"); |             status("connected"); | ||||||
|             connected = true; |             connected = true; | ||||||
|  | |||||||
| @ -996,7 +996,7 @@ public class DeviceTest { | |||||||
| 
 | 
 | ||||||
|         usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_START)); |         usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_START)); | ||||||
|         usb.ioManager.setReadBufferSize(8); |         usb.ioManager.setReadBufferSize(8); | ||||||
|         usb.startIoManager(); |         usb.ioManager.start(); | ||||||
|         usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE); |         usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         telnet.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE); |         telnet.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         try { usb.serialPort.purgeHwBuffers(true, true); } catch(Exception ignored) { purge = false; } |         try { usb.serialPort.purgeHwBuffers(true, true); } catch(Exception ignored) { purge = false; } | ||||||
| @ -1156,7 +1156,7 @@ public class DeviceTest { | |||||||
|         usb.ioManager.setReadTimeout(readTimeout); |         usb.ioManager.setReadTimeout(readTimeout); | ||||||
|         if(readBufferSize > 0) |         if(readBufferSize > 0) | ||||||
|             usb.ioManager.setReadBufferSize(readBufferSize); |             usb.ioManager.setReadBufferSize(readBufferSize); | ||||||
|         usb.startIoManager(); |         usb.ioManager.start(); | ||||||
|         usb.setParameters(baudrate, 8, 1, UsbSerialPort.PARITY_NONE); |         usb.setParameters(baudrate, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         telnet.setParameters(baudrate, 8, 1, UsbSerialPort.PARITY_NONE); |         telnet.setParameters(baudrate, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| 
 | 
 | ||||||
| @ -1354,8 +1354,14 @@ public class DeviceTest { | |||||||
|         usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); |         usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); |         telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         usb.ioManager.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |         usb.ioManager.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); | ||||||
|         usb.startIoManager(); |         usb.ioManager.start(); | ||||||
|         usb.waitForIoManagerStarted(); |         usb.waitForIoManagerStarted(); | ||||||
|  |         assertTrue("iomanager thread", usb.hasIoManagerThread()); | ||||||
|  |         try { | ||||||
|  |             usb.ioManager.start(); | ||||||
|  |             fail("already running error expected"); | ||||||
|  |         } catch (IllegalStateException ignored) { | ||||||
|  |         } | ||||||
|         try { |         try { | ||||||
|             usb.ioManager.run(); |             usb.ioManager.run(); | ||||||
|             fail("already running error expected"); |             fail("already running error expected"); | ||||||
| @ -1413,7 +1419,25 @@ public class DeviceTest { | |||||||
|         telnet.write("d".getBytes()); |         telnet.write("d".getBytes()); | ||||||
|         assertThat(usb.read(1), equalTo("d".getBytes())); |         assertThat(usb.read(1), equalTo("d".getBytes())); | ||||||
| 
 | 
 | ||||||
|  |         usb.close(); | ||||||
|  |         for (int i = 0; i < 100 && usb.hasIoManagerThread(); i++) { | ||||||
|  |             Thread.sleep(1); | ||||||
|  |         } | ||||||
|  |         assertFalse("iomanager thread", usb.hasIoManagerThread()); | ||||||
|         SerialInputOutputManager.DEBUG = false; |         SerialInputOutputManager.DEBUG = false; | ||||||
|  | 
 | ||||||
|  |         // legacy start | ||||||
|  |         usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_START)); // creates new IoManager | ||||||
|  |         usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         usb.ioManager.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); | ||||||
|  |         Executors.newSingleThreadExecutor().submit(usb.ioManager); | ||||||
|  |         usb.waitForIoManagerStarted(); | ||||||
|  |         try { | ||||||
|  |             usb.ioManager.start(); | ||||||
|  |             fail("already running error expected"); | ||||||
|  |         } catch (IllegalStateException ignored) { | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| @ -1438,7 +1462,7 @@ public class DeviceTest { | |||||||
|         // with timeout: write after timeout |         // with timeout: write after timeout | ||||||
|         usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_START)); |         usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_START)); | ||||||
|         usb.ioManager.setReadTimeout(100); |         usb.ioManager.setReadTimeout(100); | ||||||
|         usb.startIoManager(); |         usb.ioManager.start(); | ||||||
|         usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); |         usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); |         telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|         usb.ioManager.writeAsync(buf); |         usb.ioManager.writeAsync(buf); | ||||||
|  | |||||||
| @ -160,10 +160,6 @@ public class UsbWrapper implements SerialInputOutputManager.Listener { | |||||||
|         readError = null; |         readError = null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void startIoManager() { |  | ||||||
|         Executors.newSingleThreadExecutor().submit(ioManager); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public void waitForIoManagerStarted() throws IOException { |     public void waitForIoManagerStarted() throws IOException { | ||||||
|         for (int i = 0; i < 100; i++) { |         for (int i = 0; i < 100; i++) { | ||||||
|             if (SerialInputOutputManager.State.STOPPED != ioManager.getState()) return; |             if (SerialInputOutputManager.State.STOPPED != ioManager.getState()) return; | ||||||
| @ -176,6 +172,14 @@ public class UsbWrapper implements SerialInputOutputManager.Listener { | |||||||
|         throw new IOException("IoManager not started"); |         throw new IOException("IoManager not started"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public boolean hasIoManagerThread() { | ||||||
|  |         for (Thread thread : Thread.getAllStackTraces().keySet()) { | ||||||
|  |             if (thread.getName().equals(SerialInputOutputManager.class.getSimpleName())) | ||||||
|  |                 return true; | ||||||
|  |         } | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     // wait full time |     // wait full time | ||||||
|     public byte[] read() throws Exception { |     public byte[] read() throws Exception { | ||||||
|         return read(-1, -1, -1); |         return read(-1, -1, -1); | ||||||
|  | |||||||
| @ -80,7 +80,7 @@ public class SerialInputOutputManager implements Runnable { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * setThreadPriority. By default use higher priority than UI thread to prevent data loss |      * setThreadPriority. By default a higher priority than UI thread is used to prevent data loss | ||||||
|      * |      * | ||||||
|      * @param threadPriority  see {@link Process#setThreadPriority(int)} |      * @param threadPriority  see {@link Process#setThreadPriority(int)} | ||||||
|      * */ |      * */ | ||||||
| @ -142,8 +142,8 @@ public class SerialInputOutputManager implements Runnable { | |||||||
|         return mWriteBuffer.capacity(); |         return mWriteBuffer.capacity(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /* |     /** | ||||||
|      * when writeAsync is used, it is recommended to use readTimeout != 0, |      * when using writeAsync, it is recommended to use readTimeout != 0, | ||||||
|      * else the write will be delayed until read data is available |      * else the write will be delayed until read data is available | ||||||
|      */ |      */ | ||||||
|     public void writeAsync(byte[] data) { |     public void writeAsync(byte[] data) { | ||||||
| @ -152,6 +152,21 @@ public class SerialInputOutputManager implements Runnable { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * start SerialInputOutputManager in separate thread | ||||||
|  |      */ | ||||||
|  |     public void start() { | ||||||
|  |         if(mState != State.STOPPED) | ||||||
|  |             throw new IllegalStateException("already started"); | ||||||
|  |         new Thread(this, this.getClass().getSimpleName()).start(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * stop SerialInputOutputManager thread | ||||||
|  |      * | ||||||
|  |      * when using readTimeout == 0 (default), additionally use usbSerialPort.close() to | ||||||
|  |      * interrupt blocking read | ||||||
|  |      */ | ||||||
|     public synchronized void stop() { |     public synchronized void stop() { | ||||||
|         if (getState() == State.RUNNING) { |         if (getState() == State.RUNNING) { | ||||||
|             Log.i(TAG, "Stop requested"); |             Log.i(TAG, "Stop requested"); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user