mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-30 18:07:21 +00:00 
			
		
		
		
	Merge pull request #188 from kai-morich/ch340-async
enable async read for CH340 as in CDC driver to prevent data loss at high baud rates
This commit is contained in:
		
						commit
						d7147201de
					
				| @ -25,9 +25,12 @@ import android.hardware.usb.UsbDevice; | |||||||
| import android.hardware.usb.UsbDeviceConnection; | import android.hardware.usb.UsbDeviceConnection; | ||||||
| import android.hardware.usb.UsbEndpoint; | import android.hardware.usb.UsbEndpoint; | ||||||
| import android.hardware.usb.UsbInterface; | import android.hardware.usb.UsbInterface; | ||||||
|  | import android.hardware.usb.UsbRequest; | ||||||
|  | import android.os.Build; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.nio.ByteBuffer; | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
| import java.util.LinkedHashMap; | import java.util.LinkedHashMap; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @ -70,11 +73,13 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | |||||||
| 		private boolean dtr = false; | 		private boolean dtr = false; | ||||||
| 		private boolean rts = false; | 		private boolean rts = false; | ||||||
| 
 | 
 | ||||||
|  | 		private final boolean mEnableAsyncReads; | ||||||
| 		private UsbEndpoint mReadEndpoint; | 		private UsbEndpoint mReadEndpoint; | ||||||
| 		private UsbEndpoint mWriteEndpoint; | 		private UsbEndpoint mWriteEndpoint; | ||||||
| 
 | 
 | ||||||
| 		public Ch340SerialPort(UsbDevice device, int portNumber) { | 		public Ch340SerialPort(UsbDevice device, int portNumber) { | ||||||
| 			super(device, portNumber); | 			super(device, portNumber); | ||||||
|  | 			mEnableAsyncReads = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		@Override | 		@Override | ||||||
| @ -112,6 +117,11 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | |||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 
 | 
 | ||||||
|  | 				if (mEnableAsyncReads) { | ||||||
|  | 					Log.d(TAG, "Async reads enabled"); | ||||||
|  | 				} else { | ||||||
|  | 					Log.d(TAG, "Async reads disabled."); | ||||||
|  | 				} | ||||||
| 
 | 
 | ||||||
| 				initialize(); | 				initialize(); | ||||||
| 				setBaudRate(DEFAULT_BAUD_RATE); | 				setBaudRate(DEFAULT_BAUD_RATE); | ||||||
| @ -146,6 +156,32 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | |||||||
| 
 | 
 | ||||||
| 		@Override | 		@Override | ||||||
| 		public int read(byte[] dest, int timeoutMillis) throws IOException { | 		public int read(byte[] dest, int timeoutMillis) throws IOException { | ||||||
|  | 			if (mEnableAsyncReads) { | ||||||
|  | 				final UsbRequest request = new UsbRequest(); | ||||||
|  | 				try { | ||||||
|  | 					request.initialize(mConnection, mReadEndpoint); | ||||||
|  | 					final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||||
|  | 					if (!request.queue(buf, dest.length)) { | ||||||
|  | 						throw new IOException("Error queueing request."); | ||||||
|  | 					} | ||||||
|  | 
 | ||||||
|  | 					final UsbRequest response = mConnection.requestWait(); | ||||||
|  | 					if (response == null) { | ||||||
|  | 						throw new IOException("Null response"); | ||||||
|  | 					} | ||||||
|  | 
 | ||||||
|  | 					final int nread = buf.position(); | ||||||
|  | 					if (nread > 0) { | ||||||
|  | 						//Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length))); | ||||||
|  | 						return nread; | ||||||
|  | 					} else { | ||||||
|  | 						return 0; | ||||||
|  | 					} | ||||||
|  | 				} finally { | ||||||
|  | 					request.close(); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
| 			final int numBytesRead; | 			final int numBytesRead; | ||||||
| 			synchronized (mReadBufferLock) { | 			synchronized (mReadBufferLock) { | ||||||
| 				int readAmt = Math.min(dest.length, mReadBuffer.length); | 				int readAmt = Math.min(dest.length, mReadBuffer.length); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user