java-org.hwo/src/org/hwo/io/NativeSerialPort.java

136 lines
3.1 KiB
Java

package org.hwo.io;
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 java.util.concurrent.TimeoutException;
import org.hwo.fifo.FiFo;
import org.hwo.platform.natives.NativeLoader;
/* NATIVE RETURN VALUES:
*
* 0 No error
* -1 Unknown IO Error
* -2 Timeout
* -3 Interface could not be opened
* -4 Parameter Error
* -5 More than one character returned
* -6 Port not opened
* -99 Java <-> C Interface Error
*
*/
public class NativeSerialPort extends SerialPort {
Thread lastThread = null;
class SerialPortInputStream extends InputStream
{
@Override
public int read() throws SerialPortExeption {
synchronized (NativeSerialPort.this) {
if (lastThread != Thread.currentThread()){
lastThread = Thread.currentThread();
//System.err.println("NSP-R: Thread Change! " + lastThread.toString());
}
if (!isOpen())
throw new SerialPortExeption(-6);
int ch = native_getch(nativeHandle,getTimeout());
//System.err.println(String.format("RX:0x%02x", ch ));
if (ch < 0)
throw new SerialPortExeption(ch);
return ch;
}
}
}
class SerialPortOutputStream extends OutputStream
{
@Override
public void write(int arg0) throws SerialPortExeption {
synchronized (NativeSerialPort.this) {
if (lastThread != Thread.currentThread()){
lastThread = Thread.currentThread();
}
if (!isOpen())
throw new SerialPortExeption(-6);
native_putch(nativeHandle, getTimeout(), arg0);
};
}
}
private static native int native_open(String portName);
private static native int native_close(int handle);
private static native int native_send(int handle,byte[] data);
private static native int native_recv(int handle,byte[] data);
private static native int native_getch(int handle,int timeout);
private static native int native_putch(int handle,int timeout,int ch);
private int nativeHandle;
private SerialPortInputStream inputStream;
private SerialPortOutputStream outputStream;
public NativeSerialPort()
{
nativeHandle = -1;
inputStream = new SerialPortInputStream();
outputStream = new SerialPortOutputStream();
}
@Override
public synchronized boolean isOpen() {
return (nativeHandle >= 0);
}
@Override
public synchronized boolean open() {
if (isOpen())
return false;
nativeHandle = native_open(getPortName());
System.err.println(String.format("NativeSerialPort: nativeHandle: %d for device %s",nativeHandle,getPortName()));
if (nativeHandle < 0)
return false;
return true;
}
@Override
public synchronized void close() {
if (nativeHandle >= 0)
{
System.err.println(String.format("NativeSerialPort: Closing nativeHandle %d",nativeHandle));
native_close(nativeHandle);
System.err.println(String.format("NativeSerialPort: Closed nativeHandle %d",nativeHandle));
}
nativeHandle = -1;
}
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public OutputStream getOutputStream() {
return outputStream;
}
static {
NativeLoader.loadLibrary("hwoio");
}
}