forked from LupusNobilis/java-org.hwo
NewSerialPort eingefügt
parent
64a52e82a6
commit
165630a9f2
|
@ -0,0 +1,34 @@
|
|||
package org.hwo.io.NewSerialPort;
|
||||
|
||||
public enum BaudRate {
|
||||
B0(0),
|
||||
B50(50),B75(75),B110(110),B134(134),B150(150),
|
||||
B200(200),B300(300),B600(600),B1200(1200),B1800(1800),
|
||||
B2400(2400),B4800(4800),B9600(9600),B19200(19200),
|
||||
B38400(38400),B57600(57600),B115200(115200),B230400(230400);
|
||||
|
||||
|
||||
private final int value;
|
||||
|
||||
private BaudRate(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static BaudRate fromInt(int baudRate){
|
||||
for (BaudRate rate: BaudRate.values()){
|
||||
if (rate.getValue()==baudRate)
|
||||
return rate;
|
||||
}
|
||||
return B0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d/s", getValue());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.hwo.io.NewSerialPort;
|
||||
|
||||
public enum HandShake {
|
||||
NONE(0),RTSCTS(1),XONXOFF(2);
|
||||
private final int value;
|
||||
|
||||
private HandShake(int v){
|
||||
this.value = v;
|
||||
}
|
||||
|
||||
public int getValue(){
|
||||
return this.value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,248 @@
|
|||
package org.hwo.io.NewSerialPort;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.hwo.io.NativeSerialPort;
|
||||
import org.hwo.io.SerialPortExeption;
|
||||
import org.hwo.io.SerialPortWINDOWS;
|
||||
import org.hwo.nativeloader.NativeLoader;
|
||||
import org.hwo.os.OsDetect;
|
||||
|
||||
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_OTHER = -99; // Unbekannter Fehler
|
||||
|
||||
|
||||
String portName;
|
||||
int baudRate;
|
||||
int databits;
|
||||
boolean stopbit2;
|
||||
HandShake handShake;
|
||||
Parity parity;
|
||||
|
||||
int timeout;
|
||||
|
||||
long nsp;
|
||||
|
||||
boolean wasOpened;
|
||||
|
||||
private NewSerialPortInputStream inputStream;
|
||||
private NewSerialPortOutputStream outputStream;
|
||||
|
||||
public NewSerialPort(String portName) {
|
||||
|
||||
this.nsp = nsp_alloc();
|
||||
|
||||
setPortName(portName);
|
||||
|
||||
setBaudRate(9600);
|
||||
setDatabits(8);
|
||||
setStopBit2(false);
|
||||
|
||||
setHandShake(HandShake.NONE);
|
||||
setParity(Parity.NONE);
|
||||
|
||||
setTimeOut(0);
|
||||
|
||||
this.inputStream = new NewSerialPortInputStream();
|
||||
this.outputStream = new NewSerialPortOutputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
nsp_free(this.nsp);
|
||||
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
public NewSerialPortInputStream getInputStream() {
|
||||
return inputStream;
|
||||
}
|
||||
public NewSerialPortOutputStream getOutputStream() {
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
public String getPortName() {
|
||||
return portName;
|
||||
}
|
||||
public void setPortName(String portName) {
|
||||
this.portName = portName;
|
||||
nsp_set_portname(nsp, portName);
|
||||
}
|
||||
|
||||
public int getBaudRate() {
|
||||
return baudRate;
|
||||
}
|
||||
public void setBaudRate(int baudRate) {
|
||||
this.baudRate = baudRate;
|
||||
nsp_setup_baudrate(nsp, baudRate);
|
||||
}
|
||||
public void setBaudRate(BaudRate baudRate) {
|
||||
setBaudRate(baudRate.getValue());
|
||||
}
|
||||
public BaudRate getBaudRate2(){
|
||||
return BaudRate.fromInt(this.baudRate);
|
||||
}
|
||||
|
||||
public int getDatabits() {
|
||||
return databits;
|
||||
}
|
||||
public void setDatabits(int databits) {
|
||||
this.databits = databits;
|
||||
nsp_setup_bits(nsp, databits, this.stopbit2 ? 2 : 1);
|
||||
}
|
||||
|
||||
public HandShake getHandShake() {
|
||||
return handShake;
|
||||
}
|
||||
public void setHandShake(HandShake handShake) {
|
||||
this.handShake = handShake;
|
||||
nsp_setup_handshake(nsp, handShake.getValue());
|
||||
}
|
||||
|
||||
public Parity getParity() {
|
||||
return parity;
|
||||
}
|
||||
public void setParity(Parity parity) {
|
||||
this.parity = parity;
|
||||
nsp_setup_parity(nsp, parity.getValue());
|
||||
}
|
||||
|
||||
public boolean getStopBit2(){
|
||||
return this.stopbit2;
|
||||
}
|
||||
public void setStopBit2(boolean sb2){
|
||||
this.stopbit2 = sb2;
|
||||
nsp_setup_bits(nsp, databits, this.stopbit2 ? 2 : 1);
|
||||
}
|
||||
|
||||
public int getTimeOut(){
|
||||
return this.timeout;
|
||||
}
|
||||
public void setTimeOut(int timeout){
|
||||
this.timeout = timeout;
|
||||
nsp_setup_timeout(nsp, timeout);
|
||||
}
|
||||
|
||||
public boolean open(){
|
||||
int r = nsp_open(nsp);
|
||||
wasOpened = (r == 0);
|
||||
|
||||
System.err.println(String.format("nsp_open(): %d",r));
|
||||
|
||||
return wasOpened;
|
||||
}
|
||||
public void close(){
|
||||
nsp_close(nsp);
|
||||
wasOpened = false;
|
||||
}
|
||||
|
||||
public boolean isOpen(){
|
||||
return wasOpened;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static native long nsp_alloc();
|
||||
private static native int nsp_free(long msp);
|
||||
|
||||
private static native int nsp_set_portname(long nsp,String portName);
|
||||
private static native int nsp_setup_baudrate(long nsp,int baudRate);
|
||||
private static native int nsp_setup_bits(long nsp,int dataBits,int stopBits);
|
||||
private static native int nsp_setup_parity(long nsp,int parity);
|
||||
private static native int nsp_setup_handshake(long nsp,int handshake);
|
||||
private static native int nsp_setup_timeout(long nsp,int timeout);
|
||||
|
||||
private static native int nsp_open(long nsp);
|
||||
private static native int nsp_close(long nsp);
|
||||
|
||||
private static native int nsp_read(long nsp);
|
||||
private static native int nsp_write(long nsp,int ch);
|
||||
|
||||
static {
|
||||
NativeLoader.loadLibrary("nsp");
|
||||
}
|
||||
|
||||
|
||||
public class NewSerialPortInputStream extends InputStream
|
||||
{
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
int ch = nsp_read(nsp);
|
||||
if (ch < 0){
|
||||
NewSerialPort.this.close();
|
||||
throw new IOException(String.format("nsp_read()=%d", ch));
|
||||
};
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
|
||||
public class NewSerialPortOutputStream extends OutputStream
|
||||
{
|
||||
@Override
|
||||
public void write(int arg0){
|
||||
if (nsp_write(nsp, arg0)<0)
|
||||
NewSerialPort.this.close();
|
||||
}
|
||||
}
|
||||
|
||||
static public String[] getPortNames(){
|
||||
switch (OsDetect.getOperatingSystem()){
|
||||
case LINUX:
|
||||
return getPortNamesLIN();
|
||||
case WINDOWS:
|
||||
return getPortNamesWIN();
|
||||
default:
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
static public String[] getPortNamesWIN()
|
||||
{
|
||||
ArrayList<String> portNames = new ArrayList<String>();
|
||||
|
||||
NewSerialPort sp = new NewSerialPort("");
|
||||
|
||||
for (int i = 1; i < 32; i++)
|
||||
{
|
||||
sp.setPortName(String.format("COM%d:",i));
|
||||
if (sp.open())
|
||||
{
|
||||
portNames.add(String.format("COM%d:",i));
|
||||
sp.close();
|
||||
}
|
||||
}
|
||||
|
||||
return portNames.toArray(new String[0]);
|
||||
}
|
||||
static public String[] getPortNamesLIN()
|
||||
{
|
||||
ArrayList<String> portNames = new ArrayList<String>();
|
||||
|
||||
File devDir = new File("/dev");
|
||||
File[] list = devDir.listFiles(new FilenameFilter() {
|
||||
|
||||
@Override
|
||||
public boolean accept(File arg0, String arg1) {
|
||||
if (arg1.startsWith("ttyS") || arg1.startsWith("ttyACM"))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
for (File file:list)
|
||||
portNames.add("/dev/" + file.getName());
|
||||
|
||||
return portNames.toArray(new String[0]);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.hwo.io.NewSerialPort;
|
||||
|
||||
public enum Parity {
|
||||
NONE(0),EVEN(1),ODD(2);
|
||||
private final int value;
|
||||
|
||||
private Parity(int v){
|
||||
this.value = v;
|
||||
}
|
||||
|
||||
public int getValue(){
|
||||
return this.value;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue