// ComPort.java // // Copyright (C) 1998 Henrik Bjorkman. This is free software you // may use, copy and distribute this program freely. But you // must do it on your own risk. No responsibilities taken and // all rights reserved. // // This class is an interface between a java program and a serial // port. It works on Windows NT and it should work on most unix // systems but it does not work on Win95. // // // Credits // // SUNs jdk 1.1.5 was used when developing this program. // // I had help by looking at how these things where done in a program // called "Elgaard Positioning System" when creating this program. // // // History // // 1998-05-20 Created by Henrik Bjorkman // 1998-06-02 Added package. /Henrik // package se.beod.henrik.nav; import java.io.*; class ComPort { String portName; File file; FileInputStream in; static void error(String s) { System.err.println("ComPort: " + s); } static void debug(String s) { //System.out.println("ComPort: " + s); } public ComPort(String port_name) { debug("serialOpen"); try { String init=null; debug("system "+System.getProperty("os.name")); if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")>=0) { debug("dos"); portName="com1"; if ((System.getProperty("os.name").toUpperCase().indexOf("WINDOWS NT")>=0)) { init = "MODE.COM " + portName + " 4800,N,8,1 "; } else { error("Windows 95 is a bag of shit! Set baudrate manually to 4800,n,8,1"); } } else { debug("unix"); portName="/dev/cua0"; init = "/bin/sh " + "-c " + "stty 4800 pass8 -parenb -inlcr ixany -ixon < " + portName; } if (init!=null) { Process pid=Runtime.getRuntime().exec(init); InputStream in = new DataInputStream(pid.getInputStream()); while (true) { int ch=1; ch = in.read(); if (ch<0) break; System.out.print((char)ch); } } file = new File(portName); in = new FileInputStream(file); } catch(IOException e) { error("IOException " + e); } catch(SecurityException e) { error("SecurityException "+ e); } } String readLine() { StringBuffer line=new StringBuffer(); try { while(true) { int ch; ch=in.read(); if ((ch=='\r') || (ch=='\n')) { break; } else if (ch>0) { line.append((char)ch); } else { synchronized (this) {try {this.wait(50);} catch (InterruptedException e) {;}} } } } catch( IOException e ) { error("read " + e ); } debug(line.toString()); return(line.toString()); } void serialClose() { debug("serialClose"); try { if (in != null) { debug("close"); in.close(); in=null; } } catch( IOException e ) { error("close " + e ); } } }