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;
|
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(){
|
public int getValue(){
|
||||||
return this.value;
|
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.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
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.NativeSerialPort;
|
||||||
import org.hwo.io.SerialPortExeption;
|
import org.hwo.io.SerialPortExeption;
|
||||||
import org.hwo.io.SerialPortWINDOWS;
|
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_OK = 0; // Kein Fehler
|
||||||
public static long nsp_RET_NOTFOUND = -1; // Port konnte nicht geöffnet werden
|
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_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
|
public static long nsp_RET_OTHER = -99; // Unbekannter Fehler
|
||||||
|
|
||||||
|
@ -34,6 +40,7 @@ public class NewSerialPort {
|
||||||
long nsp;
|
long nsp;
|
||||||
|
|
||||||
boolean wasOpened;
|
boolean wasOpened;
|
||||||
|
boolean autoOpen;
|
||||||
|
|
||||||
private NewSerialPortInputStream inputStream;
|
private NewSerialPortInputStream inputStream;
|
||||||
private NewSerialPortOutputStream outputStream;
|
private NewSerialPortOutputStream outputStream;
|
||||||
|
@ -71,6 +78,13 @@ public class NewSerialPort {
|
||||||
return outputStream;
|
return outputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAutoOpen(){
|
||||||
|
return autoOpen;
|
||||||
|
}
|
||||||
|
public void setAutoOpen(boolean autoOpen){
|
||||||
|
this.autoOpen = autoOpen;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPortName() {
|
public String getPortName() {
|
||||||
return portName;
|
return portName;
|
||||||
}
|
}
|
||||||
|
@ -150,6 +164,55 @@ public class NewSerialPort {
|
||||||
return wasOpened;
|
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();
|
private static native long nsp_alloc();
|
||||||
|
@ -176,9 +239,14 @@ public class NewSerialPort {
|
||||||
public class NewSerialPortInputStream extends InputStream
|
public class NewSerialPortInputStream extends InputStream
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
|
if (autoOpen && !isOpen())
|
||||||
|
open();
|
||||||
|
|
||||||
int ch = nsp_read(nsp);
|
int ch = nsp_read(nsp);
|
||||||
if (ch < 0){
|
if (ch < 0){
|
||||||
|
if (ch == nsp_RET_TIMEOUT)
|
||||||
|
return -1;
|
||||||
NewSerialPort.this.close();
|
NewSerialPort.this.close();
|
||||||
throw new IOException(String.format("nsp_read()=%d", ch));
|
throw new IOException(String.format("nsp_read()=%d", ch));
|
||||||
};
|
};
|
||||||
|
@ -190,6 +258,9 @@ public class NewSerialPort {
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void write(int arg0){
|
public void write(int arg0){
|
||||||
|
if (autoOpen && !isOpen())
|
||||||
|
open();
|
||||||
|
|
||||||
if (nsp_write(nsp, arg0)<0)
|
if (nsp_write(nsp, arg0)<0)
|
||||||
NewSerialPort.this.close();
|
NewSerialPort.this.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,29 @@ public enum Parity {
|
||||||
this.value = v;
|
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(){
|
public int getValue(){
|
||||||
return this.value;
|
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)
|
public static boolean tryLoad(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")));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
System.loadLibrary(libName);
|
System.loadLibrary(libName);
|
||||||
return;
|
return true;
|
||||||
} catch (UnsatisfiedLinkError e)
|
} 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);
|
|
||||||
|
if (tryLoad(libName))
|
||||||
try
|
return;
|
||||||
{
|
|
||||||
System.loadLibrary("./"+platformLibName);
|
String platformLibName = calculateLibraryName(libName);
|
||||||
|
if (tryLoad(platformLibName))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (tryLoad(new File("").getAbsolutePath() + File.separator + platformLibName))
|
||||||
return;
|
return;
|
||||||
} catch (UnsatisfiedLinkError e)
|
|
||||||
{
|
|
||||||
System.err.println("Native Library " + platformLibName + " failed to load.");
|
|
||||||
}
|
|
||||||
|
|
||||||
String resourcePath;
|
String resourcePath;
|
||||||
|
|
||||||
|
@ -104,6 +124,7 @@ public class NativeLoader {
|
||||||
System.load(tempLibFile.getAbsolutePath());
|
System.load(tempLibFile.getAbsolutePath());
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue