User`s manual

Example – Communicating Through a Serial Port
5-71
7. Open an Input Stream and Determine Number of Bytes to Read
To read the data expected from the oscilloscope in response to the contrast
query, the example opens an input stream by calling the
static method,
InputStream.getInputStream, to obtain an InputStream object for the serial
port. Then, the example calls the method
available on the InputStream object,
in, and assigns the returned number of bytes to numAvail.
in = getInputStream(serialPort);
numAvail = available(in);
8. Create an Input Stream Reader for the Serial Port
The example then calls a java.io.InputStreamReader constructor, with the
InputStream object, in, and assigns the new object to reader.
reader = java.io.InputStreamReader(in);
9. Read Data from Serial Port and Close Reader
The example reads from the serial port, by calling the read method on the
InputStreamReader object reader for each available byte. The read statement
uses MATLAB array concatenation to add each newly read byte to the array of
bytes already read. After reading the data, the example calls
close on reader
to close the input stream reader.
result = [];
for k = 1:numAvail
result = [result read(reader)];
end
close(reader);
10. Close the Serial Port
The example closes the serial port, by calling close on the serialPort object.
close(serialPort);
11. Convert Input Argument to a MATLAB Character Array
The last statement of the example uses the MATLAB function, char, to convert
the array input bytes (integers) to an array of characters:
result = char(result);