FileDocCategorySizeDatePackage
SerialReadByEvents.javaAPI DocExample1722Sat Nov 25 12:55:36 GMT 2000None

SerialReadByEvents

public class SerialReadByEvents extends CommPortOpen implements SerialPortEventListener
Read from a Serial port, notifying when data arrives. Simulation of part of an event-logging service.
version
$Id: SerialReadByEvents.java,v 1.2 2000/11/25 17:55:37 ian Exp $
author
Ian F. Darwin, ian@darwinsys.com

Fields Summary
protected BufferedReader
ifile
Constructors Summary
public SerialReadByEvents(Frame f)

		
		super(f);
	
Methods Summary
protected voidconverse()
Hold the conversation.


		if (!(thePort instanceof SerialPort)) {
			System.err.println("But I wanted a SERIAL port!");
			System.exit(1);
		}
		// Tell the Comm API that we want serial events.
		((SerialPort)thePort).notifyOnDataAvailable(true);
		try {
			((SerialPort)thePort).addEventListener(this);
		} catch (TooManyListenersException ev) {
			// "CantHappen" error
			System.err.println("Too many listeners(!) " + ev);
			System.exit(0);
		}
	
		// Make a reader for the input file.
		ifile = new BufferedReader(new InputStreamReader(is));

		//
	
public static voidmain(java.lang.String[] argv)


		new SerialReadByEvents(null).converse();
	
public voidserialEvent(SerialPortEvent ev)

		String line;
		try {
			line = ifile.readLine();
			if (line == null) {
				System.out.println("EOF on serial port.");
				System.exit(0);
			}
			os.println(line);
		} catch (IOException ex) {
			System.err.println("IO Error " + ex);
		}