diff --git a/src/native/nsp32.dll b/src/native/nsp32.dll new file mode 100755 index 0000000..6dfdb23 Binary files /dev/null and b/src/native/nsp32.dll differ diff --git a/src/org/hwo/ByteArrayHelper.java b/src/org/hwo/ByteArrayHelper.java new file mode 100644 index 0000000..97293f3 --- /dev/null +++ b/src/org/hwo/ByteArrayHelper.java @@ -0,0 +1,28 @@ +package org.hwo; + +public class ByteArrayHelper { + + public ByteArrayHelper() { + } + + + public static byte[] unbox(Byte[] bytes){ + byte[] t = new byte[ bytes.length ]; + + for (int i=0;i tokens = new LinkedList(); + + if (includePortName){ + tokens.add(String.format("PN=%s",this.portName)); + } + + tokens.add(String.format("B=%d",this.baudRate)); + tokens.add(String.format("P=%s",this.parity.getLetter())); + tokens.add(String.format("H=%s", this.handShake.getLetter())); + tokens.add(String.format("SB=%d", (this.stopbit2) ? 2 : 1 )); + + return StringHelper.join(tokens, ";"); + } + + public boolean parseSettings(String settings){ + String[] tokens = settings.split(";"); + + for (String token: tokens){ + String[] st = token.split("=", 2); + + if (st.length == 2){ + if (st[0].equals("PN")){ + this.setPortName(st[1]); + } + if (st[0].equals("B")){ + this.setBaudRate(Integer.parseInt(st[1])); + } + if (st[0].equals("P")){ + this.setParity( Parity.fromLetter(st[1])); + } + if (st[0].equals("H")){ + this.setHandShake( HandShake.fromLetter(st[1])); + } + if (st[0].equals("SB")){ + this.setStopBit2( st[1].equals("2")); + } + } + + } + return true; + } + private static native long nsp_alloc(); @@ -176,9 +239,14 @@ public class NewSerialPort { public class NewSerialPortInputStream extends InputStream { @Override - public int read() throws IOException { + public int read() throws IOException { + if (autoOpen && !isOpen()) + open(); + int ch = nsp_read(nsp); if (ch < 0){ + if (ch == nsp_RET_TIMEOUT) + return -1; NewSerialPort.this.close(); throw new IOException(String.format("nsp_read()=%d", ch)); }; @@ -190,6 +258,9 @@ public class NewSerialPort { { @Override public void write(int arg0){ + if (autoOpen && !isOpen()) + open(); + if (nsp_write(nsp, arg0)<0) NewSerialPort.this.close(); } diff --git a/src/org/hwo/io/NewSerialPort/Parity.java b/src/org/hwo/io/NewSerialPort/Parity.java index 7c1a604..bc5f740 100644 --- a/src/org/hwo/io/NewSerialPort/Parity.java +++ b/src/org/hwo/io/NewSerialPort/Parity.java @@ -8,7 +8,29 @@ public enum Parity { this.value = v; } + public static Parity fromLetter(String letter){ + if (letter.equals("N")) + return NONE; + if (letter.equals("E")) + return EVEN; + if (letter.equals("O")) + return ODD; + return null; + } + public int getValue(){ return this.value; } + + public String getLetter(){ + switch (this.value){ + case 0: + return "N"; + case 1: + return "E"; + case 2: + return "O"; + } + return ""; + } } diff --git a/src/org/hwo/nativeloader/NativeLoader.java b/src/org/hwo/nativeloader/NativeLoader.java index 531a65b..6a364ba 100644 --- a/src/org/hwo/nativeloader/NativeLoader.java +++ b/src/org/hwo/nativeloader/NativeLoader.java @@ -47,33 +47,53 @@ public class NativeLoader { } - public static void loadLibrary(String libName) - { - if (OsDetect.getOperatingSystem()==OsType.LINUX){ - System.err.println("Linux erkannt. Patche java.library.path"); - System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path"))); - } - - + public static boolean tryLoad(String libName){ try { System.loadLibrary(libName); - return; + return true; } catch (UnsatisfiedLinkError e) { - System.err.println("Native Library " + libName + " failed to load."); + System.err.println(""); + System.err.println(""); + System.err.println("** tryLoad(...) A *****************************"); + System.err.println(""); + e.printStackTrace(); + + + try + { + System.load(libName); + return true; + } catch (UnsatisfiedLinkError ee) + { + System.err.println(""); + System.err.println(""); + System.err.println("** tryLoad(...) B *****************************"); + System.err.println(""); + ee.printStackTrace(); + } } + return false; + } + + public static void loadLibrary(String libName) + { + //if (OsDetect.getOperatingSystem()==OsType.LINUX){ + // System.err.println("Linux erkannt. Patche java.library.path"); + System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path"))); + //} - String platformLibName = calculateLibraryName(libName); - - try - { - System.loadLibrary("./"+platformLibName); + + if (tryLoad(libName)) + return; + + String platformLibName = calculateLibraryName(libName); + if (tryLoad(platformLibName)) + return; + + if (tryLoad(new File("").getAbsolutePath() + File.separator + platformLibName)) return; - } catch (UnsatisfiedLinkError e) - { - System.err.println("Native Library " + platformLibName + " failed to load."); - } String resourcePath; @@ -104,6 +124,7 @@ public class NativeLoader { System.load(tempLibFile.getAbsolutePath()); } catch (Exception e) { + e.printStackTrace(); throw new RuntimeException(e); }