Codes for SerialReadByEvents
/* * SerialReadByEvents.java * * Created on September 22, 2011, 1:21 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ /** * */ import java.awt.*; import java.io.*; import javax.comm.*; import java.util.*; /** * Read from a Serial port, notifying when data arrives. * Simulation of part of an event-logging service. */ public class SerialReadByEvents extends CommPortOpen implements SerialPortEventListener { public static void main(String[] argv) throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException { new SerialReadByEvents(null).converse(); } /* Constructor */ public SerialReadByEvents(Frame f) throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException { super(f); } protected BufferedReader ifile; byte ans[] = new byte[255]; /** * Hold the conversation. */ protected void converse() throws IOException { 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)); // } int i=0; public void serialEvent(SerialPortEvent ev) { String line; try { is = new DataInputStream(thePort.getInputStream()); ifile = new BufferedReader(new InputStreamReader(is)); System.out.println(ifile.read()+" dfvvvvvvft "+ (i++)); } catch (IOException e) { System.err.println("Read failed"); System.exit(1); } catch (IOException ex) { System.err.println("IO Error " + ex); } } }
my error is the java program will only get the input from the serial port the first time, all subsequent inputs are ignored and was not displayed on the console.. need help on this.. thanks...