kopia lustrzana https://github.com/felHR85/UsbSerial
Asynchronous write operation (tests needed)
rodzic
5ae3c05250
commit
1b4c2dac77
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.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
|
@ -55,6 +55,7 @@ public class BLED112SerialDevice extends UsbSerialDevice
|
|||
{
|
||||
// Restart the working thread if it has been killed before and get and claim interface
|
||||
restartWorkingThread();
|
||||
restartWriteThread();
|
||||
mInterface = device.getInterface(1); // BLED112 Interface 0: Communications | Interface 1: CDC Data
|
||||
|
||||
if(connection.claimInterface(mInterface, true))
|
||||
|
@ -88,15 +89,15 @@ public class BLED112SerialDevice extends UsbSerialDevice
|
|||
requestIN = new UsbRequest();
|
||||
requestIN.initialize(connection, inEndpoint);
|
||||
|
||||
// Pass reference to the thread
|
||||
// Pass references to the threads
|
||||
workerThread.setUsbRequest(requestIN);
|
||||
|
||||
writeThread.setUsbEndpoint(outEndpoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buffer)
|
||||
{
|
||||
connection.bulkTransfer(outEndpoint, buffer, buffer.length, USB_TIMEOUT);
|
||||
serialBuffer.putWriteBuffer(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -112,6 +113,7 @@ public class BLED112SerialDevice extends UsbSerialDevice
|
|||
{
|
||||
setControlCommand(BLED112_SET_CONTROL_LINE_STATE, BLED112_DISCONNECT_CONTROL_LINE , null);
|
||||
killWorkingThread();
|
||||
killWriteThread();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
|
|
|
@ -59,8 +59,9 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
|||
public void open()
|
||||
{
|
||||
|
||||
// Restart the working thread if it has been killed before and get and claim interface
|
||||
// Restart the working thread and writeThread if it has been killed before and get and claim interface
|
||||
restartWorkingThread();
|
||||
restartWriteThread();
|
||||
mInterface = device.getInterface(0); // CP2102 has only one interface
|
||||
|
||||
if(connection.claimInterface(mInterface, true))
|
||||
|
@ -98,14 +99,15 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
|||
requestIN = new UsbRequest();
|
||||
requestIN.initialize(connection, inEndpoint);
|
||||
|
||||
// Pass references to the thread
|
||||
// Pass references to the threads
|
||||
workerThread.setUsbRequest(requestIN);
|
||||
writeThread.setUsbEndpoint(outEndpoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buffer)
|
||||
{
|
||||
connection.bulkTransfer(outEndpoint, buffer, buffer.length, USB_TIMEOUT);
|
||||
serialBuffer.putWriteBuffer(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -121,6 +123,7 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
|||
{
|
||||
setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_DISABLE, null);
|
||||
killWorkingThread();
|
||||
killWriteThread();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
|
|
|
@ -68,6 +68,11 @@ public class SerialBuffer
|
|||
writeBuffer.put(data);
|
||||
}
|
||||
|
||||
public void resetWriteBuffer()
|
||||
{
|
||||
writeBuffer.reset();
|
||||
}
|
||||
|
||||
private class WriteBuffer
|
||||
{
|
||||
private byte[] writeBuffer;
|
||||
|
@ -104,6 +109,11 @@ public class SerialBuffer
|
|||
position = -1;
|
||||
return dst;
|
||||
}
|
||||
|
||||
public synchronized void reset()
|
||||
{
|
||||
position = -1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
protected SerialBuffer serialBuffer;
|
||||
|
||||
protected WorkerThread workerThread;
|
||||
protected WriteThread writeThread;
|
||||
|
||||
public UsbSerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
||||
{
|
||||
|
@ -28,7 +29,9 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
this.connection = connection;
|
||||
serialBuffer = new SerialBuffer();
|
||||
workerThread = new WorkerThread();
|
||||
writeThread = new WriteThread();
|
||||
workerThread.start();
|
||||
writeThread.start();
|
||||
}
|
||||
|
||||
// Common Usb Serial Operations (I/O Asynchronous)
|
||||
|
@ -105,6 +108,37 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
protected class WriteThread extends Thread
|
||||
{
|
||||
private UsbEndpoint outEndpoint;
|
||||
private AtomicBoolean working;
|
||||
|
||||
public WriteThread()
|
||||
{
|
||||
working = new AtomicBoolean(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while(working.get())
|
||||
{
|
||||
byte[] data = serialBuffer.getWriteBuffer();
|
||||
connection.bulkTransfer(outEndpoint, data, data.length, USB_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
public void setUsbEndpoint(UsbEndpoint outEndpoint)
|
||||
{
|
||||
this.outEndpoint = outEndpoint;
|
||||
}
|
||||
|
||||
public void stopWriteThread()
|
||||
{
|
||||
working.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Kill workingThread; This must be called when closing a device
|
||||
|
@ -129,4 +163,23 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
workerThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
protected void killWriteThread()
|
||||
{
|
||||
if(writeThread != null)
|
||||
{
|
||||
writeThread.stopWriteThread();
|
||||
writeThread = null;
|
||||
serialBuffer.resetWriteBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
protected void restartWriteThread()
|
||||
{
|
||||
if(writeThread == null)
|
||||
{
|
||||
writeThread = new WriteThread();
|
||||
writeThread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue