可能性の1つは、InputStream
を拡張し、JSSC SerialPortEventListener
インターフェイスを実装するカスタムクラスを提供することです。このクラスは、シリアルポートからデータを受け取り、バッファに格納します。また、バッファからデータを取得する方法はread()
です。
:
private class PortOutputStream extends OutputStream {
SerialPort serialPort = null;
public PortOutputStream(SerialPort port) {
serialPort = port;
}
@Override
public void write(int data) throws IOException,SerialPortException {
if (serialPort != null && serialPort.isOpened()) {
serialPort.writeByte((byte)(data & 0xFF));
} else {
Log.e(TAG, "cannot write to serial port.");
throw new IOException("serial port is closed.");
}
// you may also override write(byte[], int, int)
}
あなたXmodem
オブジェクトをインスタンス化する前に、あなたは両方のストリームを作成する必要があります。
private class PortInputStream extends InputStream implements SerialPortEventListener {
CircularBuffer buffer = new CircularBuffer(); //backed by a byte[]
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
// exception handling omitted
buffer.write(serialPort.readBytes(event.getEventValue()));
}
}
@Override
public int read() throws IOException {
int data = -1;
try {
data = buffer.read();
} catch (InterruptedException e) {
Log.e(TAG, "interrupted while waiting for serial data.");
}
return data;
}
@Override
public int available() throws IOException {
return buffer.getLength();
}
同様に、あなたはOutputStream
を拡張し、シリアルインタフェースへの書き込みカスタムPortOutputStream
クラスを提供することができます
// omitted serialPort initialization
InputStream pin = new PortInputStream();
serialPort.addEventListener(pin, SerialPort.MASK_RXCHAR);
OutPutStream pon = new PortOutputStream(serialPort);
Xmodem xmodem = new Xmodem(pin,pon);