User`s manual
5 Calling Java from MATLAB
5-70
4. Configure the Serial Port
The next three statements call configuration methods on the SerialPort object
serialPort. The first statement calls setSerialPortParams to set the baud
rate, data bits, stop bits, and parity. The next two statements call
setFlowControlMode to set the flow control, and then enableReceiveTimeout
to set the timeout for receiving data.
setSerialPortParams(serialPort, SerialPort_BAUD_9600,...
SerialPort_DATABITS_8, SerialPort_STOPBITS_1,...
SerialPort_PARITY_NONE);
setFlowControlMode(serialPort, SerialPort_FLOWCTRL_NONE);
enableReceiveTimeout(serialPort, 1000);
5. Set Up an Output Stream Writer
The example then calls a constructor to create and open a
java.io.OutputStreamWriter object. The constructor call passes the
java.io.OutputStream object, returned by a call to the getOutputStream
method
serialPort, and assigns the OutputStreamWriter object to out.
out = java.io.OutputStreamWriter(getOutputStream(serialPort));
6. Write Data to Serial Port and Close Output Stream
The example writes a string to the serial port, by calling write on the object
out. The string is formed by concatenating (with MATLAB [ ] syntax) a
command to set the oscilloscope’s contrast to 45, with the command terminator
that is required by the instrument. The next statement calls
flush on out to
flush the output stream.
write(out, ['Display:Contrast 45' terminator]);
flush(out);
Then, the example again calls write on out to send another string to the serial
port. This string is a query command, to determine the oscilloscope’s contrast
setting, concatenated with the command terminator. The example then calls
close on the output stream.
write(out, ['Display:Contrast?' terminator]);
close(out);