diff options
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/Arduino.java')
-rw-r--r-- | PunchingBag/src/uk/ac/open/punchingbag/Arduino.java | 353 |
1 files changed, 172 insertions, 181 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java b/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java index 78d6726..0421d0e 100644 --- a/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java @@ -1,4 +1,5 @@ package uk.ac.open.punchingbag; + import java.util.Enumeration; import java.util.HashSet; @@ -16,10 +17,12 @@ import javax.swing.event.EventListenerList; import gnu.io.*; public class Arduino implements Runnable { + + private InputStream in; + private OutputStream out; - InputStream in; - OutputStream out; - + private boolean connected = false; + private EventListenerList readListenerList = new EventListenerList(); private EventListenerList writeListenerList = new EventListenerList(); @@ -30,189 +33,177 @@ public class Arduino implements Runnable { public void addSerialReadListener(SerialReadListener l) { readListenerList.add(SerialReadListener.class, l); } - - static void listPorts() - { - System.out.println("Listing Comm ports:"); - java.util.Enumeration<CommPortIdentifier> 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 { + + static void listPorts() { + System.out.println("Listing Comm ports:"); + java.util.Enumeration<CommPortIdentifier> 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<CommPortIdentifier> getAvailableSerialPorts() { - HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>(); - 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(); - } - } - } + 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<CommPortIdentifier> getAvailableSerialPorts() { + HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>(); + 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; + } + + boolean connect(String portName) throws Exception { + CommPortIdentifier portIdentifier = CommPortIdentifier + .getPortIdentifier(portName); + if (portIdentifier.isCurrentlyOwned()) { + System.out.println("Error: Port is currently in use"); + return false; + } 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(); + connected = true; + return true; + } else { + System.out + .println("Error: Only serial ports are handled by this example."); + return false; + } + } + } + + /** */ + 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 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 boolean connected() { + return connected; + } + + /** */ + 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 @@ -221,7 +212,7 @@ public class Arduino implements Runnable { sListeners[i].serialWriteEvent(bytes); } } - + public void write(byte b) throws IOException { out.write(b); SerialWriteListener[] sListeners = writeListenerList @@ -230,7 +221,7 @@ public class Arduino implements Runnable { sListeners[i].serialWriteEvent(b); } } - + public int read() throws IOException { int read = in.read(); SerialReadListener[] sListeners = readListenerList |