2021-01-26 18:53:37 +00:00
|
|
|
package com.radio.codec2talkie.transport;
|
|
|
|
|
2021-10-24 09:25:07 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2021-10-24 18:00:03 +00:00
|
|
|
import com.hoho.android.usbserial.driver.SerialTimeoutException;
|
2021-01-26 18:53:37 +00:00
|
|
|
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class UsbSerial implements Transport {
|
|
|
|
|
|
|
|
private final int RX_TIMEOUT = 100;
|
|
|
|
private final int TX_TIMEOUT = 2000;
|
|
|
|
|
|
|
|
private final UsbSerialPort _usbPort;
|
|
|
|
|
|
|
|
public UsbSerial(UsbSerialPort usbPort) {
|
|
|
|
_usbPort = usbPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int read(byte[] data) throws IOException {
|
|
|
|
return _usbPort.read(data, RX_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-27 08:57:36 +00:00
|
|
|
public int write(byte[] data) throws IOException {
|
2021-10-24 18:00:03 +00:00
|
|
|
try {
|
|
|
|
_usbPort.write(data, TX_TIMEOUT);
|
|
|
|
return data.length;
|
|
|
|
} catch (SerialTimeoutException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-26 18:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
_usbPort.close();
|
|
|
|
}
|
|
|
|
}
|