kopia lustrzana https://github.com/felHR85/UsbSerial
Minor changes
rodzic
8c53a8dfe6
commit
5f3871f51b
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
|
@ -108,7 +108,9 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
|||
@Override
|
||||
public int read()
|
||||
{
|
||||
// TODO
|
||||
// Input parameter, callback reference
|
||||
// callback reference needed to be passed to WorkingThread
|
||||
requestIN.queue(serialBuffer.getReadBuffer(), SerialBuffer.DEFAULT_READ_BUFFER_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ import java.nio.ByteBuffer;
|
|||
|
||||
public class SerialBuffer
|
||||
{
|
||||
private static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
|
||||
private static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||
public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
|
||||
public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||
private ByteBuffer writeBuffer;
|
||||
private ByteBuffer readBuffer;
|
||||
private Object mReadLock;
|
||||
|
@ -45,9 +45,7 @@ public class SerialBuffer
|
|||
{
|
||||
synchronized(mWriteLock)
|
||||
{
|
||||
ByteBuffer buff = writeBuffer;
|
||||
writeBuffer.clear();
|
||||
return buff;
|
||||
return writeBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,14 +63,22 @@ public class SerialBuffer
|
|||
}
|
||||
}
|
||||
|
||||
public byte[] getReadBuffer()
|
||||
public ByteBuffer getReadBuffer()
|
||||
{
|
||||
synchronized(mReadLock)
|
||||
{
|
||||
byte[] data = readBuffer.array();
|
||||
readBuffer.clear();
|
||||
return data;
|
||||
return readBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
public void clearWriteBuffer()
|
||||
{
|
||||
writeBuffer.clear();
|
||||
}
|
||||
|
||||
public void clearReadBuffer()
|
||||
{
|
||||
readBuffer.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.felhr.usbserial;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
|
@ -49,17 +51,26 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
protected class WorkerThread extends Thread
|
||||
{
|
||||
private UsbReadCallback callback;
|
||||
private AtomicBoolean working;
|
||||
|
||||
public WorkerThread()
|
||||
{
|
||||
working.set(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
UsbRequest request = connection.requestWait();
|
||||
if(request.getEndpoint().getDirection() == UsbConstants.USB_DIR_IN) // Read
|
||||
while(working.get())
|
||||
{
|
||||
|
||||
}else // Write
|
||||
{
|
||||
|
||||
UsbRequest request = connection.requestWait();
|
||||
if(request.getEndpoint().getDirection() == UsbConstants.USB_DIR_IN) // Read
|
||||
{
|
||||
|
||||
}else // Write
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,6 +83,11 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
{
|
||||
callback.onReceivedData(data);
|
||||
}
|
||||
|
||||
public void stopWorkingThread()
|
||||
{
|
||||
working.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
public interface UsbReadCallback
|
||||
|
|
Ładowanie…
Reference in New Issue