package awa.imu; import java.io.*; import java.util.StringTokenizer; import javax.comm.*; /** * @author awa * */ public class RS232Reader extends Thread { /** How long to wait for the open to finish up. */ public static final int TIMEOUTSECONDS = 10; /** The baud rate to use. */ public static final int BAUD = 115200; private DataInputStream is = null; /** The chosen Port Identifier */ private CommPortIdentifier thePortID; /** The chosen Port itself */ private CommPort thePort; private IMUControlPanel parent; private boolean stop = false; public RS232Reader(IMUControlPanel panel) { parent = panel; } public void run() { try { thePortID = CommPortIdentifier.getPortIdentifier("COM1"); // Now actually open the port. // This form of openPort takes an Application Name and a timeout. System.out.print("Trying to open " + thePortID.getName() + "..."); thePort = thePortID.open("DarwinSys DataComm", TIMEOUTSECONDS * 1000); SerialPort myPort = (SerialPort) thePort; // set up the serial port myPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // Get the input stream is = new DataInputStream(thePort.getInputStream()); System.out.println(" done."); String line; while (!stop && (line = is.readLine()) != null) { //System.out.println("read: " + line); StringTokenizer st = new StringTokenizer(line, ","); if (st.countTokens() == 8) { String sname = st.nextToken(); String time = st.nextToken(); String pitch = st.nextToken(); String roll = st.nextToken(); String yaw = st.nextToken(); String accy = st.nextToken(); String accx = st.nextToken(); String accz = st.nextToken(); parent.setIMUValues(pitch, roll, yaw, accy, accx, accz); } } } catch (IOException e) { System.out.println(); System.err.println("Can't open input stream: write-only"); is = null; } catch (Throwable t) { System.out.println(); t.printStackTrace(); //Ensure the application exits with an error condition. } System.out.println("closing " + thePortID.getName() + "..."); thePort.close(); } public void stopPlay() { stop = true; } }