package uk.ac.open.punchingbag; import java.util.Enumeration; import java.util.HashSet; import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.swing.event.EventListenerList; import gnu.io.*; public class Arduino implements Runnable { InputStream in; OutputStream out; private EventListenerList readListenerList = new EventListenerList(); private EventListenerList writeListenerList = new EventListenerList(); public void addSerialWriteListener(SerialWriteListener l) { writeListenerList.add(SerialWriteListener.class, l); } public void addSerialReadListener(SerialReadListener l) { readListenerList.add(SerialReadListener.class, l); } static void listPorts() { System.out.println("Listing Comm ports:"); java.util.Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); while ( portEnum.hasMoreElements() ) { CommPortIdentifier portIdentifier = portEnum.nextElement(); System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) ); } } static String getPortTypeName ( int portType ) { switch ( portType ) { case CommPortIdentifier.PORT_I2C: return "I2C"; case CommPortIdentifier.PORT_PARALLEL: return "Parallel"; case CommPortIdentifier.PORT_RAW: return "Raw"; case CommPortIdentifier.PORT_RS485: return "RS485"; case CommPortIdentifier.PORT_SERIAL: return "Serial"; default: return "unknown type"; } } public String getID() throws IOException { byte[] idBytes = {0x49, 0x44}; this.write(idBytes); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } int id; while ((id = this.read()) == -1) {} if (id == 66) { return "B"; } else if (id == 76) { return "L"; } else { return "ERROR " + id; } } /** * @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used. */ public static HashSet getAvailableSerialPorts() { HashSet h = new HashSet(); Enumeration thePorts = CommPortIdentifier.getPortIdentifiers(); while (thePorts.hasMoreElements()) { CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement(); switch (com.getPortType()) { case CommPortIdentifier.PORT_SERIAL: try { CommPort thePort = com.open("CommUtil", 50); thePort.close(); h.add(com); //System.out.println("Found a port: " + com.getPortType()); } catch (PortInUseException e) { System.out.println("Port, " + com.getName() + ", is in use."); } catch (Exception e) { System.err.println("Failed to open port " + com.getName()); e.printStackTrace(); } } } return h; } void connect ( String portName ) throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if ( portIdentifier.isCurrentlyOwned() ) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); if ( commPort instanceof SerialPort ) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); in = serialPort.getInputStream(); out = serialPort.getOutputStream(); //(new Thread(this)).start(); } else { System.out.println("Error: Only serial ports are handled by this example."); } } } /** */ public class SerialReader implements Runnable { InputStream in; public SerialReader ( InputStream in ) { this.in = in; } public void run () { byte[] buffer = new byte[1024]; int len = -1; try { while ( ( len = this.in.read(buffer)) > -1 ) { System.out.print(new String(buffer,0,len)); input(new String(buffer,0,len)); } } catch ( IOException e ) { e.printStackTrace(); } } } public static void input(String string) { } /** */ public class SerialWriter implements Runnable { OutputStream out; public SerialWriter ( OutputStream out ) { this.out = out; } public void run () { try { int c = 0; while ( ( c = System.in.read()) > -1 ) { this.out.write(c); } } catch ( IOException e ) { e.printStackTrace(); } } } public void run() { byte[] buffer = new byte[1024]; int len = -1; try { while ( ( len = this.in.read(buffer)) > -1 ) { System.out.print(new String(buffer,0,len)); //input(new String(buffer,0,len)); } } catch ( IOException e ) { e.printStackTrace(); } } public void write(byte[] bytes) throws IOException { out.write(bytes); SerialWriteListener[] sListeners = writeListenerList .getListeners(SerialWriteListener.class); for (int i = 0; i < writeListenerList.getListenerCount(); i++) { sListeners[i].serialWriteEvent(bytes); } } public void write(byte b) throws IOException { out.write(b); SerialWriteListener[] sListeners = writeListenerList .getListeners(SerialWriteListener.class); for (int i = 0; i < writeListenerList.getListenerCount(); i++) { sListeners[i].serialWriteEvent(b); } } public int read() throws IOException { int read = in.read(); SerialReadListener[] sListeners = readListenerList .getListeners(SerialReadListener.class); for (int i = 0; i < readListenerList.getListenerCount(); i++) { sListeners[i].serialReadEvent(read); } return read; } }