Page 45 - The Definitive Guide to Building Java Robots
P. 45
Preston_5564C02.fm Page 26 Wednesday, September 14, 2005 5:42 AM
26 CHAPTER 2 ■ SERIAL COMMUNICATION
public void setTimeout(int timeout) {
// not used
}
public void setDTR(boolean dtr) {
serialPort.setDTR(dtr);
}
public String getName() {
return serialPort.getName();
}
}
Section Summary
For this section, I created a simpler-to-use serial port. I did this by simplifying the construction
of the port to take parameters of just the serial port id (1,2,3,…,n) and a baud rate as an int.
Second, I modified the input and output streams to take parameters and return data useful to
me in robotics: byte[] and strings. Finally, I created an interface to the serial port called the
JSerialPort, which can be used in later sections to force the same behavior for multiple serial
port implementations.
The interface and class I created in this section were
• JSerialPort.java: This is the interface that will specify behavior for all serial ports.
• StandardSerialPort.java: This class is the simpler version of the Java API SerialPort.
The only thing that the StandardSerialPort does not do is handle concurrent serial port
usage. That will be the topic of the next section.
2.2 Concurrent Serial Port Usage
If we connect more than one thing to a serial port, we’ll run into many PortInUseExceptions.
We want to avoid that, otherwise our programs will be exiting when we don’t want them to.
(See Table 2-2.)