// serial comms library // does port identification // opens channel at 1200 baud for talking to serial port. // // Fred Martin / computer science / UML // Tue Sep 24 12:29:23 2002 // updated Sat Oct 4 14:00:53 2003 // made close method public import java.io.*; import java.util.*; import javax.comm.*; public class Serial { private SerialPort sp; private String portName; private String[] portList; private OutputStream output; private InputStream input; static final private boolean DEBUG = false; // just try to open the requested port. if fail, get any... // should save time since not iterating thru whole list. public Serial(String desiredSP) { CommPortIdentifier portID; try { portID= CommPortIdentifier.getPortIdentifier(desiredSP); sp = (SerialPort)(portID.open("Serial", 50)); portName = desiredSP; String[] ports = new String[1]; ports[0] = desiredSP; portList = ports; portInit(); // init w/o opening port } catch (Exception e) { System.out.println("could not open requested port " + desiredSP); loadPortList(); if (portList.length > 0) { portName = portList[0]; // choose 1st one initially //System.out.println("new Serial with SP " + portName); init(); } else { System.out.println("no SPs available!"); portName = ""; } System.out.println("opened port " + getPortName() + " instead."); } } public Serial() { loadPortList(); if (portList.length > 0) { portName = portList[0]; // choose 1st one initially //System.out.println("new Serial with SP " + portName); init(); } else { System.out.println("no SPs available!"); portName = ""; } } protected void finalize() { try { output.flush(); } catch (Exception e) { e.printStackTrace(); } close(); } private void init() { try { sp= (SerialPort)(CommPortIdentifier.getPortIdentifier(portName).open("Serial",50)); //System.out.println("created SerialPort object"); portInit(); } catch (Exception ioe) { ioe.printStackTrace(); } } // sp object must already exist private void portInit() { try { sp.setSerialPortParams(1200, sp.DATABITS_8, sp.STOPBITS_1, sp.PARITY_NONE); //System.out.println("setSerialPortParams"); sp.enableReceiveTimeout(15); //System.out.println("enableReceiveTimeout"); output=sp.getOutputStream(); //System.out.println("getOutputStream"); input=sp.getInputStream(); //System.out.println("getInputStream"); } catch (Exception ioe) { ioe.printStackTrace(); } } public void setPortName(String name) { close(); portName = name; init(); } public String getPortName() { return portName; } public void close() { try { sp.close(); } catch (Exception ioe) { System.out.println(ioe); } } public void flush() { try { output.flush(); } catch (Exception ioe) { System.out.println(ioe); } } public int get() { int b= -1; try { b=input.read(); } catch (Exception ioe) { ioe.printStackTrace(); } // System.out.println("get is returning " + b); return b; } public void put(int b) throws IOException { output.write(b); } public void put(byte[] buf) throws IOException { output.write(buf); } private void loadPortList() { Enumeration sportsAll = null; Vector v = new Vector(); SerialPort sp; CommPortIdentifier portID; String currString; try { sportsAll= CommPortIdentifier.getPortIdentifiers(); } catch (Exception e) { System.out.println(e); } while (sportsAll.hasMoreElements()) { try { currString=((CommPortIdentifier)(sportsAll.nextElement())).getName(); // System.out.println("currString is " + currString); portID= CommPortIdentifier.getPortIdentifier(currString); if (portID.getPortType() == portID.PORT_SERIAL) { sp=(SerialPort)(portID.open("Serial", 50)); sp.close(); v.addElement(currString); } } catch (Exception e) { } } portList = new String[v.size()]; v.copyInto(portList); } public String[] portsAvailable() { if (portList.length>0) { return portList; } else { String[] result= new String[1]; result[0]=""; return result; } } } /* * Local variables: * tab-width: 4 * End: */