better error reporting on MacOSX, remember to System.exit(0) master
authorAdam Megacz <adam@megacz.com>
Mon, 17 Jan 2011 23:26:43 +0000 (15:26 -0800)
committerAdam Megacz <adam@megacz.com>
Mon, 17 Jan 2011 23:26:43 +0000 (15:26 -0800)
src/com/megacz/eltron/Eltron.java
src/com/megacz/eltron/MailingLabel.java

index d59e73c..c5ba4cf 100644 (file)
@@ -6,21 +6,6 @@ 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 {
 
@@ -33,6 +18,19 @@ public class Eltron {
     }
 
     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();
@@ -43,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;
     }
@@ -54,10 +52,8 @@ 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();
         InputStream in = sp.getInputStream();
         int count = 0;
@@ -132,6 +128,6 @@ public class Eltron {
 
         pw.println("P1");
         pw.flush();
+        System.out.println("done printing.");
     }
-
 }
index 641abd7..575fbf8 100644 (file)
@@ -40,6 +40,9 @@ public class MailingLabel {
             for(int y=0; y<img.getHeight(null); y++)
                 data[x+y*img.getWidth(null)] = img.getRGB(x, y);
         Eltron.print(data, img.getWidth(null), img.getHeight(null));
+
+        // RXTX spawns a thread that never dies
+        System.exit(0);
     }
 
 }