import java.io.InputStream; import javax.comm.CommPortIdentifier; import javax.comm.SerialPort; /** * Read data from the HandyCricket and display it as a scrolling bar chart. * This code depends on the Java Communications API available at * http://java.sun.com/products/javacomm. **/ public class TextReader { public static void main(String[] args) { try { CommPortIdentifier id = CommPortIdentifier.getPortIdentifier("COM2"); SerialPort port = (SerialPort) id.open("Reader", /*timeout=*/ 20000); port.setSerialPortParams(/*baudrate=*/ 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream in = port.getInputStream(); while (true) { int value = 80 - (in.read() * 80 / 256); while (value-- > 0) System.out.print("-"); System.out.println(""); } } catch (Exception e) { e.printStackTrace(System.out); } } }