NSP: read/write(byte[]...) funktionen hinzugefügt
parent
28bbe1bc5d
commit
85551c9000
|
@ -254,6 +254,11 @@ public class NewSerialPort {
|
|||
private static native int nsp_read(long nsp);
|
||||
private static native int nsp_write(long nsp,int ch);
|
||||
|
||||
private static native int nsp_read_bytes(long nsp,byte[] bytes,int offset,int len);
|
||||
private static native int nsp_write_bytes(long nsp,byte[] bytes,int offset,int len);
|
||||
|
||||
|
||||
|
||||
static {
|
||||
NativeLoader.loadLibrary("nsp");
|
||||
}
|
||||
|
@ -284,6 +289,27 @@ public class NewSerialPort {
|
|||
};
|
||||
return ch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
int nRead = nsp_read_bytes(nsp, b, off, len);
|
||||
if (nRead < 0){
|
||||
if (nRead == -11){ // -EAGAIN (timeout, keine zeichen verfügbar)
|
||||
return 0;
|
||||
};
|
||||
throw new IOException(String.format("nsp_read_bytes() returned %d", nRead));
|
||||
}
|
||||
return nRead;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException {
|
||||
int nRead = nsp_read_bytes(nsp, b, 0, b.length);
|
||||
if (nRead < 0){
|
||||
throw new IOException(String.format("nsp_read_bytes() returned %d", nRead));
|
||||
}
|
||||
return nRead;
|
||||
}
|
||||
}
|
||||
|
||||
public class NewSerialPortOutputStream extends OutputStream
|
||||
|
@ -321,6 +347,23 @@ public class NewSerialPort {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
int nWritten = nsp_write_bytes(nsp, b, off, len);
|
||||
if (nWritten != len){
|
||||
throw new IOException(String.format("nsp_write_bytes() returned %d", nWritten));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
int nWritten = nsp_write_bytes(nsp, b, 0, b.length);
|
||||
if (nWritten != b.length){
|
||||
throw new IOException(String.format("nsp_write_bytes() returned %d", nWritten));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static public String[] getPortNames(){
|
||||
|
|
Loading…
Reference in New Issue