diff options
| author | Yuval Adam <yuv.adm@gmail.com> | 2011-09-23 12:27:36 +0300 |
|---|---|---|
| committer | Yuval Adam <yuv.adm@gmail.com> | 2011-09-23 12:27:36 +0300 |
| commit | 0d33c8d1c0e6b870260883c71120ef19be8134f2 (patch) | |
| tree | 836d71f7c193f9233fd94485e2f84ff7c68e135b | |
| parent | a9bf1b0d4fd01a1459dd1f41448e76ef578866c8 (diff) | |
getting ready to test first version of BetterRoomba
| -rw-r--r-- | roombacomm-client/src/com/hackingroomba/roombacomm/BetterRoomba.java | 85 |
1 files changed, 80 insertions, 5 deletions
diff --git a/roombacomm-client/src/com/hackingroomba/roombacomm/BetterRoomba.java b/roombacomm-client/src/com/hackingroomba/roombacomm/BetterRoomba.java index 54ea19d..c9bad04 100644 --- a/roombacomm-client/src/com/hackingroomba/roombacomm/BetterRoomba.java +++ b/roombacomm-client/src/com/hackingroomba/roombacomm/BetterRoomba.java @@ -1,14 +1,38 @@ package com.hackingroomba.roombacomm; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; + +import gnu.io.CommPortIdentifier; +import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; public class BetterRoomba implements SerialPortEventListener { - private static int RATE = 115200; + private static final String PORT = "/dev/ttyUSB0"; + private static final int TIMEOUT = 2000; + + private static final int RATE = 115200; + private static final int DATABITS = 8; + private static final int PARITY = SerialPort.PARITY_NONE; + private static final int STOPBITS = SerialPort.STOPBITS_1; - public static void main(String[] args) { + private SerialPort serialPort; + private InputStream input; + private OutputStream output; + + public static void main(String[] args) throws Exception { System.out.println("Starting..."); + BetterRoomba br = new BetterRoomba(); + + br.startup(); + br.send(new byte[] {(byte) 136, (byte) 8}); + Thread.sleep(5000); + br.send(new byte[] {(byte) 136, (byte) 255}); + br.shutdown(); + System.out.println("-END-"); } @@ -17,8 +41,59 @@ public class BetterRoomba implements SerialPortEventListener { // do smthng with e } - public void startup() { - + public void startup() throws Exception { + serialConnect(); + } + + public void shutdown() throws Exception { + try { + if (input != null) + input.close(); + if (output != null) + output.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + try { + if (serialPort != null) + serialPort.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void send(byte[] arr) throws Exception { + output.write(arr); + } + + private void serialConnect() throws Exception { + try { + Enumeration portList = CommPortIdentifier.getPortIdentifiers(); + while (portList.hasMoreElements()) { + CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); + + if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { + System.out.println("Found " + portId.getName()); + + if (portId.getName().equals(PORT)) { + System.out.println("Opening port: "+ portId.getName()); + serialPort = (SerialPort) portId.open("roomba serial", TIMEOUT); + + input = serialPort.getInputStream(); + output = serialPort.getOutputStream(); + + serialPort.setSerialPortParams(RATE, DATABITS, STOPBITS, PARITY); + serialPort.addEventListener(this); + serialPort.notifyOnDataAvailable(true); + + System.out.println("Port opened successfully"); + } + } + } + } catch (Exception e) { + System.out.println(e.getMessage()); + throw e; + } } - } |
