User`s manual
Example – Communicating Through a Serial Port
5-69
Description of Serial Example
The major tasks performed by serialexample are:
1. Define Variables for Serial Port Configuration and Output
The first five statements define variables for configuring the serial port. The
first statement defines the baud rate to be 9600, the second defines number of
data bits to be 8, and the third defines the number of stop bits to be 1. The
fourth statement defines parity to be off, and the fifth statement defines flow
control (handshaking) to be off.
SerialPort_BAUD_9600 = 9600;
SerialPort_DATABITS_8 = 8;
SerialPort_STOPBITS_1 = 1;
SerialPort_PARITY_NONE = 0;
SerialPort_FLOWCTRL_NONE = 0;
The last variable definition sets the terminator character for writing to the
serial port, to a carriage return.
terminator = char(13);
2. Create a CommPortIdentifier Object
Instead of constructors, the javax.comm.CommPortIdentifier class has
static methods that return an instance of the class. The example calls one of
these,
getPortIdentifier, to return a CommPortIdentifier object for port
COM1.
commPort = ...
javax.comm.CommPortIdentifier.getPortIdentifier('COM1');
3. Open the Serial Port
The example opens the serial port, by calling open on the CommPortIdentifier
object
commPort. The open call returns a SerialPort object, assigning it to
serialPort. The first argument to open is the name (owner) for the port, the
second argument is the name for the port, and the third argument is the
number of milliseconds to wait for the open.
serialPort = open(commPort, 'serial', 1000);