SimpleRead.java

 package main;
 import java.io.*;
 import java.util.*;
 import javax.comm.*;
 
 
 public class SimpleRead implements Runnable, SerialPortEventListener {
         static CommPortIdentifier portId;
 
         static Enumeration portList;
 
         InputStream inputStream;
 
         SerialPort serialPort;
 
         Thread readThread;
 
         String previous = null;

         String[] result = null;
 
         private static double dist;
 
         private static double angle;
 
         private static Gui gui;
 
         public static void main(String[] args) {
                 gui = new Gui();
                 portList = CommPortIdentifier.getPortIdentifiers();
                 while (portList.hasMoreElements()) {
                         portId = (CommPortIdentifier) portList.nextElement();
                         if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                                 if (portId.getName().equals("COM1")) {
                                         SimpleRead reader = new SimpleRead();
                                 }
                         }
                 }
         }
 
         public SimpleRead() {
                 try {
                         serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
                 } catch (PortInUseException e) {
                         e.printStackTrace();
                 }
                 try {
                         inputStream = serialPort.getInputStream();
                 } catch (IOException e) {
                         e.printStackTrace();
                 }
                 try {
                         serialPort.addEventListener(this);
                 } catch (TooManyListenersException e) {
                 }
                 serialPort.notifyOnDataAvailable(true);
                 try {
                         serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                                         SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                 } catch (UnsupportedCommOperationException e) {
                 }
                 readThread = new Thread(this);
                 readThread.start();
         }
 
         public void run() {
                 try {
                         Thread.sleep(20000);
                 } catch (InterruptedException e) {
                 }
         }
 
         public void serialEvent(SerialPortEvent event) {
                 switch (event.getEventType()) {
                 case SerialPortEvent.BI:
                 case SerialPortEvent.OE:
                 case SerialPortEvent.FE:
                 case SerialPortEvent.PE:
                 case SerialPortEvent.CD:
                 case SerialPortEvent.CTS:
                 case SerialPortEvent.DSR:
                 case SerialPortEvent.RI:
                 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                         break;
                 case SerialPortEvent.DATA_AVAILABLE:
                         byte[] readBuffer = new byte[200];
                         byte[] read = null;
                         try {
                                 while (inputStream.available() > 0) {
                                         int numBytes = inputStream.read(readBuffer);
                                         read = new byte[numBytes];
                                         for (int i = 0; i < numBytes; i++) {
                                                 read[i] = readBuffer[i];
                                         }
                                 }
                                 String s = new String(read);
                                 for (int i = 0; i < s.length(); i++) {
                                         if (s.charAt(i) == ';') {
                                                 result = s.split(";");
                                                 if (result.length == 0) {
                                                         result = new String[1];
                                                         result[0] = "";
                                                 }
                                                 break;
                                         }
                                 }
                                 if (result == null) {
                                         if (previous != null) {
                                                 previous = previous.concat(s);
                                         } else {
                                                 previous = s;
                                         }
                                 } else {
                                         if (previous != null) {
                                                 previous = previous.concat(result[0]);
                                         } else {
                                                 previous = result[0];
                                         }
                                 }
                                 if (result != null) {
                                         String[] str = previous.split(":");
                                                 if (str[0].trim().equals("angle")) {
                                                         try{
                                                                 angle = 
							Integer.parseInt
							(str[1].trim());
                                                                 angle = 
							(angle / 230) * 180;
                                                         }catch(NumberFormatException e){
                                                         }
                                                 }else if(str[0].trim().equals("dist")){
                                                         try{
                                                         double volt = 
							Integer.parseInt
							(str[1].trim());
                                                         volt = 
							(volt / 255) * 2.56;
                                                         dist = voltToDist(volt);
                                                         gui.paint();
                                                         }catch(NumberFormatException e){
                                                         }
                                                 }
                                                 if (result.length > 1) {
                                                         previous = result[1];
                                                 } else {
                                                         previous = null;
                                                 }
                                                 result = null;
                                 }
                         } catch (IOException e) {
                         }
                         break;
                 }
         }
 
         private double voltToDist(double volt) {
                 return 78 * volt * volt - 162 * volt + 93.7;
         }
 
         public static double getDist() {
                 return dist;
         }
 
         public static double getAngle() {
                 return angle;
         }
 }