Skip to content

6. InputStream and OutputStream I O

Felipe Herranz edited this page Nov 18, 2018 · 1 revision

InputStream and OutputStream example

Remember to check the examplestreams module for a complete example

Use

Since UsbSerial 5.0.0 is possible to use InputStream and OutStream I/O through SerialInputStream and SerialOutputStream. Be aware that can only be used with the synchronous api

serialPort.syncOpen();
...
SerialInputStream serialInputStream = serialPort.getInputStream();
SerialOutputStream serialOutputStream = serialPort.getOutputStream();
...
int value = serialInputStream.read(); // Read one byte at a time
...
byte[] buffer = new byte[100];
int bytesRead = serialInputStream.read(buffer); //Read bytes from the input stream and put them into the buffer.
...
serialOutputStream.write(buffer);

Notice that both SerialInputStream and SerialOutputStream implement a setTimeout(int timeout) method (0 by default that means infinite.