[#1390] Make sure RxtxChannel does not block forever when reading
This commit is contained in:
parent
bf5960e9eb
commit
b1b1a906f0
@ -36,6 +36,7 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
|
|||||||
private volatile Databits databits = Databits.DATABITS_8;
|
private volatile Databits databits = Databits.DATABITS_8;
|
||||||
private volatile Paritybit paritybit = Paritybit.NONE;
|
private volatile Paritybit paritybit = Paritybit.NONE;
|
||||||
private volatile int waitTime;
|
private volatile int waitTime;
|
||||||
|
private volatile int readTimeout = 1000;
|
||||||
|
|
||||||
public DefaultRxtxChannelConfig(RxtxChannel channel) {
|
public DefaultRxtxChannelConfig(RxtxChannel channel) {
|
||||||
super(channel);
|
super(channel);
|
||||||
@ -70,6 +71,9 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
|
|||||||
if (option == WAIT_TIME) {
|
if (option == WAIT_TIME) {
|
||||||
return (T) Integer.valueOf(getWaitTimeMillis());
|
return (T) Integer.valueOf(getWaitTimeMillis());
|
||||||
}
|
}
|
||||||
|
if (option == READ_TIMEOUT) {
|
||||||
|
return (T) Integer.valueOf(getReadTimeout());
|
||||||
|
}
|
||||||
return super.getOption(option);
|
return super.getOption(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,6 +95,8 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
|
|||||||
setParitybit((Paritybit) value);
|
setParitybit((Paritybit) value);
|
||||||
} else if (option == WAIT_TIME) {
|
} else if (option == WAIT_TIME) {
|
||||||
setWaitTimeMillis((Integer) value);
|
setWaitTimeMillis((Integer) value);
|
||||||
|
} else if (option == READ_TIMEOUT) {
|
||||||
|
setReadTimeout((Integer) value);
|
||||||
} else {
|
} else {
|
||||||
return super.setOption(option, value);
|
return super.setOption(option, value);
|
||||||
}
|
}
|
||||||
@ -177,6 +183,20 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
|
|||||||
return this;
|
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
|
@Override
|
||||||
public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
|
public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
|
||||||
return (RxtxChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
|
return (RxtxChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
|
||||||
|
@ -259,6 +259,16 @@ public interface RxtxChannelConfig extends ChannelConfig {
|
|||||||
*/
|
*/
|
||||||
RxtxChannelConfig setWaitTimeMillis(int waitTimeMillis);
|
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
|
@Override
|
||||||
RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
|
RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
|
||||||
|
|
||||||
|
@ -45,6 +45,9 @@ public final class RxtxChannelOption<T> extends ChannelOption<T> {
|
|||||||
public static final RxtxChannelOption<Integer> WAIT_TIME =
|
public static final RxtxChannelOption<Integer> WAIT_TIME =
|
||||||
new 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) {
|
private RxtxChannelOption(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user