better error reporting on MacOSX, remember to System.exit(0)
[eltron.git] / src / com / megacz / eltron / Eltron.java
index ff030f4..c5ba4cf 100644 (file)
@@ -6,30 +6,31 @@ import java.io.*;
 import java.util.*;
 import gnu.io.*;
 
-/*
-
-If you're on a mac and you get the error below, you need to create
-/var/lock and make it writable.
-
-Exception in thread "main" gnu.io.PortInUseException: No such file or directory in open
-at gnu.io.RXTXPort.open(Native Method)
-at gnu.io.RXTXPort.<init>(RXTXPort.java:84)
-at com.megacz.eltron.Eltron.detectPort(Eltron.java:26)
-at com.megacz.eltron.Eltron.print(Eltron.java:37)
-at com.megacz.eltron.Eltron.main(Eltron.java:14)
-
-
- */
-
 /** code to control an Eltron */
 public class Eltron {
 
     public static void main(String[] s) throws Exception {
         Picture p = new Picture(System.in);
         print(p.data, p.width, p.height);
+
+        // RXTX spawns a thread that never dies
+        System.exit(0);
     }
 
     public static SerialPort detectPort() throws Exception {
+        if (System.getProperty("os.name").toLowerCase().indexOf("mac")==-1)
+            return detectPort_();
+        try {
+            return detectPort_();
+        } catch (gnu.io.PortInUseException e) {
+            if (e.toString().indexOf("No such file or directory in open") != -1) {
+                System.err.println("Please make sure that /var/lock/ exists and is writable");
+            }
+            throw e;
+        }
+
+    }
+    public static SerialPort detectPort_() throws Exception {
         Enumeration e = CommPortIdentifier.getPortIdentifiers();
         while(e.hasMoreElements()) {
             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
@@ -40,7 +41,7 @@ public class Eltron {
                 return ret;
             }
         }
-        SerialPort ret = new RXTXPort("/dev/ttyS0");
+        SerialPort ret = new RXTXPort("/dev/ttyUSB0");
         Log.info(Eltron.class, "returning " + ret);
         return ret;
     }
@@ -50,6 +51,7 @@ public class Eltron {
 
         // you'll need to run this once at 9600 to tell the built-in eeprom to switch to 38,400kbps
         //sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+
         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
 
         OutputStream out = sp.getOutputStream();
@@ -126,6 +128,6 @@ public class Eltron {
 
         pw.println("P1");
         pw.flush();
+        System.out.println("done printing.");
     }
-
 }