Example - Communicating Through a Serial Port
serialexample program uses classes of the Java API
javax.comm package, which support access to communications ports. After defining port configuration variables,
serialexample constructs a
javax.comm.CommPortIdentifier object, to manage the serial communications port.
The program calls the
open method on that object to return an object of the
javax.comm.SerialPort class, which describes the low-level interface to the
COM1 serial port, assumed to be connected to a Tektronix oscilloscope. (The example can be run without an oscilloscope.) The
serialexample program then calls several methods on the
SerialPort object to configure the serial port.
serialexample program uses the I/O package
java.io, to write to and read from the serial port. It calls a
static method to return an
OutputStream object for the serial port. It then passes that object to the constructor for
java.io.OutputStreamWriter. It calls the
write method on the
OutputStreamWriter object to write a command to the serial port, which sets the contrast on the oscilloscope. It calls
write again to write a command that checks the contrast. It then constructs an object of the
java.io.InputStreamWriter class to read from the serial port.
It calls another
static method on the SerialPort object to return an
OutputStream object for the serial port. It calls a method on that object to get the number of bytes to read from the port. It passes the InputStream object to the constructor for
java.io.OutputStreamWriter. Then, if there is data to read, it calls the
read method on the
OutputStreamWriter object to read the contrast data returned by the oscilloscope.
|Note MATLAB also provides built-in serial port support, described in Serial Port I/O.|
|Example - Finding an Internet Protocol Address||Description of Serial Example|