forked from LupusNobilis/java-org.hwo
org.hwo.io angepasst
parent
74d50cd9f3
commit
fd9d976d84
|
@ -9,7 +9,7 @@ import java.util.ArrayList;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.hwo.fifo.FiFo;
|
import org.hwo.fifo.FiFo;
|
||||||
import org.hwo.nativeloader.NativeLoader;
|
import org.hwo.platform.natives.NativeLoader;
|
||||||
|
|
||||||
|
|
||||||
/* NATIVE RETURN VALUES:
|
/* NATIVE RETURN VALUES:
|
||||||
|
|
|
@ -13,8 +13,8 @@ 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;
|
||||||
import org.hwo.nativeloader.NativeLoader;
|
import org.hwo.platform.Platform;
|
||||||
import org.hwo.os.OsDetect;
|
import org.hwo.platform.natives.NativeLoader;
|
||||||
|
|
||||||
public class NewSerialPort {
|
public class NewSerialPort {
|
||||||
|
|
||||||
|
@ -45,10 +45,14 @@ public class NewSerialPort {
|
||||||
private NewSerialPortInputStream inputStream;
|
private NewSerialPortInputStream inputStream;
|
||||||
private NewSerialPortOutputStream outputStream;
|
private NewSerialPortOutputStream outputStream;
|
||||||
|
|
||||||
|
private List<NewSerialPortListener> serialPortListeners;
|
||||||
|
|
||||||
public NewSerialPort(String portName) {
|
public NewSerialPort(String portName) {
|
||||||
|
|
||||||
this.nsp = nsp_alloc();
|
this.nsp = nsp_alloc();
|
||||||
|
|
||||||
|
serialPortListeners = new LinkedList<NewSerialPortListener>();
|
||||||
|
|
||||||
setPortName(portName);
|
setPortName(portName);
|
||||||
|
|
||||||
setBaudRate(9600);
|
setBaudRate(9600);
|
||||||
|
@ -71,6 +75,19 @@ public class NewSerialPort {
|
||||||
super.finalize();
|
super.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addNewSerialPortListener(NewSerialPortListener listener){
|
||||||
|
serialPortListeners.add(listener);
|
||||||
|
}
|
||||||
|
public void removeNewSerialPortListener(NewSerialPortListener listener){
|
||||||
|
serialPortListeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fireConnectionStateChanged(){
|
||||||
|
for (NewSerialPortListener l: serialPortListeners)
|
||||||
|
l.connectionStateChanged(this, isOpen());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public NewSerialPortInputStream getInputStream() {
|
public NewSerialPortInputStream getInputStream() {
|
||||||
return inputStream;
|
return inputStream;
|
||||||
}
|
}
|
||||||
|
@ -153,11 +170,14 @@ public class NewSerialPort {
|
||||||
|
|
||||||
System.err.println(String.format("nsp_open(): %d",r));
|
System.err.println(String.format("nsp_open(): %d",r));
|
||||||
|
|
||||||
|
fireConnectionStateChanged();
|
||||||
|
|
||||||
return wasOpened;
|
return wasOpened;
|
||||||
}
|
}
|
||||||
public void close(){
|
public void close(){
|
||||||
nsp_close(nsp);
|
nsp_close(nsp);
|
||||||
wasOpened = false;
|
wasOpened = false;
|
||||||
|
fireConnectionStateChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOpen(){
|
public boolean isOpen(){
|
||||||
|
@ -215,21 +235,21 @@ public class NewSerialPort {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static native long nsp_alloc();
|
private static native synchronized long nsp_alloc();
|
||||||
private static native int nsp_free(long msp);
|
private static native synchronized int nsp_free(long msp);
|
||||||
|
|
||||||
private static native int nsp_set_portname(long nsp,String portName);
|
private static native synchronized int nsp_set_portname(long nsp,String portName);
|
||||||
private static native int nsp_setup_baudrate(long nsp,int baudRate);
|
private static native synchronized int nsp_setup_baudrate(long nsp,int baudRate);
|
||||||
private static native int nsp_setup_bits(long nsp,int dataBits,int stopBits);
|
private static native synchronized int nsp_setup_bits(long nsp,int dataBits,int stopBits);
|
||||||
private static native int nsp_setup_parity(long nsp,int parity);
|
private static native synchronized int nsp_setup_parity(long nsp,int parity);
|
||||||
private static native int nsp_setup_handshake(long nsp,int handshake);
|
private static native synchronized int nsp_setup_handshake(long nsp,int handshake);
|
||||||
private static native int nsp_setup_timeout(long nsp,int timeout);
|
private static native synchronized int nsp_setup_timeout(long nsp,int timeout);
|
||||||
|
|
||||||
private static native int nsp_open(long nsp);
|
private static native synchronized int nsp_open(long nsp);
|
||||||
private static native int nsp_close(long nsp);
|
private static native synchronized int nsp_close(long nsp);
|
||||||
|
|
||||||
private static native int nsp_read(long nsp);
|
private static native synchronized int nsp_read(long nsp);
|
||||||
private static native int nsp_write(long nsp,int ch);
|
private static native synchronized int nsp_write(long nsp,int ch);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
NativeLoader.loadLibrary("nsp");
|
NativeLoader.loadLibrary("nsp");
|
||||||
|
@ -272,7 +292,7 @@ public class NewSerialPort {
|
||||||
}
|
}
|
||||||
|
|
||||||
static public String[] getPortNames(){
|
static public String[] getPortNames(){
|
||||||
switch (OsDetect.getOperatingSystem()){
|
switch (Platform.getOperatingSystem()){
|
||||||
case LINUX:
|
case LINUX:
|
||||||
return getPortNamesLIN();
|
return getPortNamesLIN();
|
||||||
case WINDOWS:
|
case WINDOWS:
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.hwo.io.NewSerialPort;
|
||||||
|
|
||||||
|
public interface NewSerialPortListener {
|
||||||
|
|
||||||
|
void connectionStateChanged(NewSerialPort port,boolean connected);
|
||||||
|
|
||||||
|
}
|
|
@ -2,13 +2,13 @@ package org.hwo.io;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import org.hwo.os.OsDetect;
|
import org.hwo.platform.Platform;
|
||||||
|
|
||||||
public abstract class SerialPort {
|
public abstract class SerialPort {
|
||||||
|
|
||||||
static public String[] getPortNames()
|
static public String[] getPortNames()
|
||||||
{
|
{
|
||||||
switch (OsDetect.getOperatingSystem())
|
switch (Platform.getOperatingSystem())
|
||||||
{
|
{
|
||||||
case OSX:
|
case OSX:
|
||||||
return SerialPortOSX.getPortNames();
|
return SerialPortOSX.getPortNames();
|
||||||
|
@ -23,7 +23,7 @@ public abstract class SerialPort {
|
||||||
|
|
||||||
static public SerialPort newInstance()
|
static public SerialPort newInstance()
|
||||||
{
|
{
|
||||||
switch (OsDetect.getOperatingSystem())
|
switch (Platform.getOperatingSystem())
|
||||||
{
|
{
|
||||||
case OSX:
|
case OSX:
|
||||||
return new SerialPortOSX();
|
return new SerialPortOSX();
|
||||||
|
|
Loading…
Reference in New Issue