ServiceLink, NetiveSerialPort: Aktualisierungen, Asynchrone Register

thobaben_serialize
Harald Wolff 2015-08-12 22:13:51 +02:00
parent aaa49a098b
commit 0c44470463
3 changed files with 38 additions and 36 deletions

View File

@ -32,21 +32,22 @@ public class NativeSerialPort extends SerialPort {
class SerialPortInputStream extends InputStream
{
@Override
public int read() throws SerialPortExeption {
if (lastThread != Thread.currentThread()){
lastThread = Thread.currentThread();
//System.err.println("NSP-R: Thread Change! " + lastThread.toString());
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;
}
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;
}
}
@ -55,16 +56,15 @@ public class NativeSerialPort extends SerialPort {
{
@Override
public void write(int arg0) throws SerialPortExeption {
if (lastThread != Thread.currentThread()){
lastThread = Thread.currentThread();
// System.err.println("NSP-R: Thread Change! " + lastThread.toString());
}
if (!isOpen())
throw new SerialPortExeption(-6);
native_putch(nativeHandle, getTimeout(), arg0);
//System.err.println(String.format("TX:0x%02x", arg0 ));
synchronized (NativeSerialPort.this) {
if (lastThread != Thread.currentThread()){
lastThread = Thread.currentThread();
}
if (!isOpen())
throw new SerialPortExeption(-6);
native_putch(nativeHandle, getTimeout(), arg0);
};
}
}
@ -90,12 +90,12 @@ public class NativeSerialPort extends SerialPort {
}
@Override
public boolean isOpen() {
public synchronized boolean isOpen() {
return (nativeHandle >= 0);
}
@Override
public boolean open() {
public synchronized boolean open() {
if (isOpen())
return false;
@ -113,6 +113,7 @@ public class NativeSerialPort extends SerialPort {
{
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;
}

View File

@ -57,7 +57,7 @@ public class AsynchronServiceLinkProvider {
for (Integer hash: serviceRegisterListeners.keySet()){
if (!getListeners(hash).isEmpty()){
System.err.println(String.format("AsyncUpdate for Hash: 0x%08x", hash));
//System.err.println(String.format("AsyncUpdate for Hash: 0x%08x", hash));
if ((hash & 0x40000000) != 0){
Float f = this.cache.getCachedFloat( axFromHash(hash),nodeFromHash(hash),regFromHash(hash));

View File

@ -14,6 +14,7 @@ import org.hwo.Smoother;
import org.hwo.bitfields.BitField;
import org.hwo.interactiveobjects.InteractiveObject;
import org.hwo.io.SerialPort;
import org.hwo.io.NewSerialPort.NewSerialPort;
/* ServiceLink
*
@ -176,7 +177,7 @@ public class ServiceLink {
}
private SerialPort serialPort;
private NewSerialPort serialPort;
int retries;
private ServiceRegisterCache
serviceRegisterCache;
@ -186,11 +187,11 @@ public class ServiceLink {
private AsynchronServiceLinkProvider
asynchronServiceLinkProvider;
public ServiceLink(SerialPort serialPort)
public ServiceLink(NewSerialPort serialPort)
{
this.retries = 3;
this.serialPort = serialPort;
this.serialPort.setTimeout(500);
this.serialPort.setTimeOut(250);
this.serviceRegisterCache = new ServiceRegisterCache(this);
this.asynchronServiceLinkProvider = new AsynchronServiceLinkProvider(serviceRegisterCache);
this.requestTime = new Smoother();
@ -211,14 +212,14 @@ public class ServiceLink {
return asynchronServiceLinkProvider;
}
public void open() throws ServiceLinkException
public synchronized void open() throws ServiceLinkException
{
if (serialPort != null)
serialPort.open();
throwNotOpen();
}
public void close()
public synchronized void close()
{
if (serialPort != null)
serialPort.close();
@ -315,24 +316,24 @@ public class ServiceLink {
return telegram;
}
public SerialPort getSerialPort() {
public synchronized NewSerialPort getSerialPort() {
return serialPort;
}
public void setSerialPort(SerialPort serialPort) {
public synchronized void setSerialPort(NewSerialPort serialPort) {
if (isOpen())
this.serialPort.close();
this.serialPort = serialPort;
}
private void throwNotOpen() throws ServiceLinkException
private synchronized void throwNotOpen() throws ServiceLinkException
{
if (!isOpen())
throw new ServiceLinkException("Port not Opened!");
}
public boolean isOpen()
public synchronized boolean isOpen()
{
return this.serialPort.isOpen();
}