The RS-232 Protocol
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
.jar
file and a native
dynamic link library (see the
javacomm site)
import java.io.*; import java.util.*; import javax.comm.*; /** * Read and print the information transmitted from * a serial GPS receiver * * Notes: (1) The .jar file containing the javax.comm package must * be in the classpath and the dynamic link library * containing the native library must be in the * path. * * (2) Logging does not start until the start() method is called. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class SerialGPSLogger implements Runnable, SerialPortEventListener { private boolean keepRunning; private DataInputStream dis; private InputStream is; private LinkedList queue; private SerialPort serialPort; private Thread controlThread; /** * Explicit Value Constructor * * @param comport The serial port (e.g., "COM1") */ public SerialGPSLogger(String comport) { CommPortIdentifier portID; Enumeration portList; // Get the list of ports portList = CommPortIdentifier.getPortIdentifiers(); // Loop through the ports looking for the requested port while (portList.hasMoreElements()) { portID = (CommPortIdentifier)portList.nextElement(); if ((portID.getPortType() == CommPortIdentifier.PORT_SERIAL) && (portID.getName().equals(comport)) ) { try { // The arguments are the owner of the port and the // timeout in milliseconds serialPort = (SerialPort)portID.open("SerialGPSLogger", 2000); is = serialPort.getInputStream(); dis = new DataInputStream(is); serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (PortInUseException piue) { System.err.println("The serial port is in use!"); System.exit(1); } catch (IOException ioe) { System.err.println("Unable to open an InputStream "+ "for the serial port!"); System.exit(2); } catch (UnsupportedCommOperationException uscoe) { System.err.println("Unable to set the parameters "+ "of the serial port!"); System.exit(3); } break; // Out of the while loop } } queue = new LinkedList(); controlThread = new Thread(this); } /** * Take serial point information off of the queue and * distribute it to the observers (required by Runnable) */ public void run() { String data; while (keepRunning) { // Log all observations that are in the queue while (queue.size() > 0) { data = (String)queue.removeFirst(); System.out.println(data); System.out.flush(); } // Sleep for 10 milliseconds try { controlThread.sleep(10); } catch (InterruptedException ie) { // Resume } } } /** * Handle events generated by the SerialPort * (required by SerialPortEventListener) * * @param event The event generated by the SerialPort */ public void serialEvent(SerialPortEvent event) { String line; switch(event.getEventType()) { case SerialPortEvent.BI: // Break Interrupt case SerialPortEvent.CD: // Carrier Detect case SerialPortEvent.CTS: // Clear to Send case SerialPortEvent.DSR: // Data Set Ready case SerialPortEvent.FE: // Framing Error case SerialPortEvent.OE: // Overrun Error case SerialPortEvent.PE: // Parity Error case SerialPortEvent.RI: // Ring Indicator case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: try { while ( ((line = dis.readLine()) != null) && (!line.equals("")) ) { queue.addLast(line); } } catch (IOException ioe) { // Do nothing } break; // End of cases } } /** * Start the logging process */ public void start() { keepRunning = true; controlThread = new Thread(this); controlThread.start(); try { serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); } catch (TooManyListenersException tmle) { keepRunning = false; System.err.println("Too many listeners on the serial port!"); System.exit(4); } } }