[#1390] Make sure RxtxChannel does not block forever when reading

This commit is contained in:
Norman Maurer 2013-06-10 18:56:33 +02:00
parent bf5960e9eb
commit b1b1a906f0
3 changed files with 33 additions and 0 deletions

View File

@ -36,6 +36,7 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
private volatile Databits databits = Databits.DATABITS_8;
private volatile Paritybit paritybit = Paritybit.NONE;
private volatile int waitTime;
private volatile int readTimeout = 1000;
public DefaultRxtxChannelConfig(RxtxChannel channel) {
super(channel);
@ -70,6 +71,9 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
if (option == WAIT_TIME) {
return (T) Integer.valueOf(getWaitTimeMillis());
}
if (option == READ_TIMEOUT) {
return (T) Integer.valueOf(getReadTimeout());
}
return super.getOption(option);
}
@ -91,6 +95,8 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
setParitybit((Paritybit) value);
} else if (option == WAIT_TIME) {
setWaitTimeMillis((Integer) value);
} else if (option == READ_TIMEOUT) {
setReadTimeout((Integer) value);
} else {
return super.setOption(option, value);
}
@ -177,6 +183,20 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
return this;
}
@Override
public RxtxChannelConfig setReadTimeout(int readTimeout) {
if (readTimeout < 0) {
throw new IllegalArgumentException("readTime must be >= 0");
}
this.readTimeout = readTimeout;
return this;
}
@Override
public int getReadTimeout() {
return readTimeout;
}
@Override
public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (RxtxChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);

View File

@ -259,6 +259,16 @@ public interface RxtxChannelConfig extends ChannelConfig {
*/
RxtxChannelConfig setWaitTimeMillis(int waitTimeMillis);
/**
* Sets the maximal time (in ms) to block while try to read from the serial port. Default is 1000ms
*/
RxtxChannelConfig setReadTimeout(int readTimout);
/**
* Return the maximal time (in ms) to block and wait for something to be ready to read.
*/
int getReadTimeout();
@Override
RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);

View File

@ -45,6 +45,9 @@ public final class RxtxChannelOption<T> extends ChannelOption<T> {
public static final RxtxChannelOption<Integer> WAIT_TIME =
new RxtxChannelOption<Integer>("WAIT_TIME");
public static final RxtxChannelOption<Integer> READ_TIMEOUT =
new RxtxChannelOption<Integer>("READ_TIMEOUT");
private RxtxChannelOption(String name) {
super(name);
}