kopia lustrzana https://github.com/felHR85/UsbSerial
solved inputStream issues and getSerialPorts
rodzic
45071b8484
commit
5c30d0d00f
|
@ -14,8 +14,8 @@ android {
|
|||
|
||||
compileOptions {
|
||||
encoding "UTF-8"
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
targetCompatibility JavaVersion.VERSION_1_7
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@ import android.widget.EditText;
|
|||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.felhr.utils.HexData;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ public class UsbService extends Service implements SerialPortCallback {
|
|||
UsbSerialInterface.STOP_BITS_1,
|
||||
UsbSerialInterface.PARITY_NONE,
|
||||
UsbSerialInterface.FLOW_CONTROL_OFF);
|
||||
if(ret)
|
||||
if(!ret)
|
||||
Toast.makeText(context, "Couldnt open the device", Toast.LENGTH_SHORT).show();
|
||||
} else if (arg1.getAction().equals(ACTION_USB_DETACHED)) {
|
||||
UsbDevice usbDevice = arg1.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||
|
@ -107,8 +107,9 @@ public class UsbService extends Service implements SerialPortCallback {
|
|||
readThreadCOM1 = new ReadThreadCOM(0,
|
||||
serialPorts.get(0).getInputStream());
|
||||
readThreadCOM1.start();
|
||||
}
|
||||
|
||||
}else if(readThreadCOM2 == null){
|
||||
if(readThreadCOM2 == null){
|
||||
readThreadCOM2 = new ReadThreadCOM(1,
|
||||
serialPorts.get(1).getInputStream());
|
||||
readThreadCOM2.start();
|
||||
|
|
|
@ -14,8 +14,8 @@ android {
|
|||
|
||||
compileOptions {
|
||||
encoding "UTF-8"
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
targetCompatibility JavaVersion.VERSION_1_7
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,34 +7,45 @@ public class SerialInputStream extends InputStream
|
|||
{
|
||||
private int timeout = 0;
|
||||
|
||||
private int bufferSize = 16 * 1024;
|
||||
private int maxBufferSize = 16 * 1024;
|
||||
|
||||
private byte[] buffer;
|
||||
private int pointer;
|
||||
private int bufferSize;
|
||||
|
||||
protected final UsbSerialInterface device;
|
||||
|
||||
public SerialInputStream(UsbSerialInterface device)
|
||||
{
|
||||
this.device = device;
|
||||
this.buffer = new byte[bufferSize];
|
||||
this.buffer = new byte[maxBufferSize];
|
||||
this.pointer = 0;
|
||||
this.bufferSize = -1;
|
||||
}
|
||||
|
||||
public SerialInputStream(UsbSerialInterface device, int bufferSize)
|
||||
public SerialInputStream(UsbSerialInterface device, int maxBufferSize)
|
||||
{
|
||||
this.device = device;
|
||||
this.bufferSize = bufferSize;
|
||||
this.buffer = new byte[this.bufferSize];
|
||||
this.maxBufferSize = maxBufferSize;
|
||||
this.buffer = new byte[this.maxBufferSize];
|
||||
this.pointer = 0;
|
||||
this.bufferSize = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read()
|
||||
{
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
int value = checkFromBuffer();
|
||||
if(value >= 0)
|
||||
return value;
|
||||
|
||||
int ret = device.syncRead(buffer, timeout);
|
||||
if(ret >= 0)
|
||||
return buffer[0];
|
||||
else
|
||||
if(ret >= 0) {
|
||||
bufferSize = ret;
|
||||
return buffer[pointer++];
|
||||
}else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -43,7 +54,25 @@ public class SerialInputStream extends InputStream
|
|||
return device.syncRead(b, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
if(bufferSize > 0)
|
||||
return bufferSize - pointer;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
private int checkFromBuffer(){
|
||||
if(bufferSize > 0 && pointer < bufferSize){
|
||||
return buffer[pointer++];
|
||||
}else{
|
||||
pointer = 0;
|
||||
bufferSize = -1;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,8 +71,8 @@ public class SerialPortBuilder {
|
|||
.toList();
|
||||
}else{
|
||||
devices.addAll(Stream.of(getPossibleSerialPorts(context))
|
||||
.map(UsbDeviceStatus::new)
|
||||
.filter(p -> !devices.contains(p))
|
||||
.map(UsbDeviceStatus::new)
|
||||
.toList());
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue