forked from LupusNobilis/java-org.hwo
NSP: Win32+, Updates
parent
72fac42725
commit
e33059d1d0
Binary file not shown.
|
@ -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<t.length;i++)
|
||||
t[i] = bytes[i];
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
public static Byte[] box(byte[] bytes){
|
||||
Byte[] t = new Byte[ bytes.length ];
|
||||
|
||||
for (int i=0;i<t.length;i++)
|
||||
t[i] = bytes[i];
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -8,7 +8,31 @@ public enum HandShake {
|
|||
this.value = v;
|
||||
}
|
||||
|
||||
public static HandShake fromLetter(String letter){
|
||||
if (letter.equals("N"))
|
||||
return NONE;
|
||||
if (letter.equals("SW"))
|
||||
return XONXOFF;
|
||||
if (letter.equals("HW"))
|
||||
return RTSCTS;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public int getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public String getLetter(){
|
||||
switch (this.value){
|
||||
case 0:
|
||||
return "N";
|
||||
case 1:
|
||||
return "SW";
|
||||
case 2:
|
||||
return "HW";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,10 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hwo.StringHelper;
|
||||
import org.hwo.io.NativeSerialPort;
|
||||
import org.hwo.io.SerialPortExeption;
|
||||
import org.hwo.io.SerialPortWINDOWS;
|
||||
|
@ -18,6 +21,9 @@ public class NewSerialPort {
|
|||
public static long nsp_RET_OK = 0; // Kein Fehler
|
||||
public static long nsp_RET_NOTFOUND = -1; // Port konnte nicht geöffnet werden
|
||||
public static long nsp_RET_PARAM = -2; // Parameter ungültig
|
||||
public static long nsp_RET_SHORTREAD = -3;
|
||||
public static long nsp_RET_TIMEOUT = -4;
|
||||
public static long nsp_RET_NOTOPEN = -5;
|
||||
|
||||
public static long nsp_RET_OTHER = -99; // Unbekannter Fehler
|
||||
|
||||
|
@ -34,6 +40,7 @@ public class NewSerialPort {
|
|||
long nsp;
|
||||
|
||||
boolean wasOpened;
|
||||
boolean autoOpen;
|
||||
|
||||
private NewSerialPortInputStream inputStream;
|
||||
private NewSerialPortOutputStream outputStream;
|
||||
|
@ -71,6 +78,13 @@ public class NewSerialPort {
|
|||
return outputStream;
|
||||
}
|
||||
|
||||
public boolean isAutoOpen(){
|
||||
return autoOpen;
|
||||
}
|
||||
public void setAutoOpen(boolean autoOpen){
|
||||
this.autoOpen = autoOpen;
|
||||
}
|
||||
|
||||
public String getPortName() {
|
||||
return portName;
|
||||
}
|
||||
|
@ -150,6 +164,55 @@ public class NewSerialPort {
|
|||
return wasOpened;
|
||||
}
|
||||
|
||||
/*
|
||||
* getSettings(...)
|
||||
*
|
||||
* Erstelle eine Text-Repräsentation der aktuellen Port Einstellungen
|
||||
*
|
||||
*/
|
||||
public String getSettings(boolean includePortName){
|
||||
List<String> tokens = new LinkedList<String>();
|
||||
|
||||
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();
|
||||
|
@ -177,8 +240,13 @@ public class NewSerialPort {
|
|||
{
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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 "";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
String platformLibName = calculateLibraryName(libName);
|
||||
|
||||
try
|
||||
{
|
||||
System.loadLibrary("./"+platformLibName);
|
||||
return;
|
||||
} catch (UnsatisfiedLinkError e)
|
||||
System.load(libName);
|
||||
return true;
|
||||
} catch (UnsatisfiedLinkError ee)
|
||||
{
|
||||
System.err.println("Native Library " + platformLibName + " failed to load.");
|
||||
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")));
|
||||
//}
|
||||
|
||||
|
||||
if (tryLoad(libName))
|
||||
return;
|
||||
|
||||
String platformLibName = calculateLibraryName(libName);
|
||||
if (tryLoad(platformLibName))
|
||||
return;
|
||||
|
||||
if (tryLoad(new File("").getAbsolutePath() + File.separator + platformLibName))
|
||||
return;
|
||||
|
||||
String resourcePath;
|
||||
|
||||
|
@ -104,6 +124,7 @@ public class NativeLoader {
|
|||
System.load(tempLibFile.getAbsolutePath());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue