Skip to content

Commit

Permalink
[netty#957] Netty/Transport/RXTX: Add an optional wait time after ope…
Browse files Browse the repository at this point in the history
…ning the serial port but before configuration to allow the serial microcontroller to reset itself after opening.
  • Loading branch information
lw346 authored and Norman Maurer committed Jan 21, 2013
1 parent 4192222 commit 318328b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
private volatile Databits databits = Databits.DATABITS_8;
private volatile Paritybit paritybit = Paritybit.NONE;
private volatile int waitTime;

public DefaultRxtxChannelConfig(RxtxChannel channel) {
super(channel);
}

@Override
public Map<ChannelOption<?>, Object> getOptions() {
return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT);
return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT, WAIT_TIME);
}

@SuppressWarnings("unchecked")
Expand All @@ -65,6 +66,9 @@ public <T> T getOption(ChannelOption<T> option) {
if (option == PARITY_BIT) {
return (T) getParitybit();
}
if (option == WAIT_TIME) {
return (T) Integer.valueOf(getWaitTimeMillis());
}
return super.getOption(option);
}

Expand All @@ -84,6 +88,8 @@ public <T> boolean setOption(ChannelOption<T> option, T value) {
setDatabits((Databits) value);
} else if (option == PARITY_BIT) {
setParitybit((Paritybit) value);
} else if (option == WAIT_TIME) {
setWaitTimeMillis((Integer) value);
} else {
return super.setOption(option, value);
}
Expand Down Expand Up @@ -156,6 +162,20 @@ public RxtxChannelConfig setRts(final boolean rts) {
return this;
}

@Override
public int getWaitTimeMillis() {
return waitTime;
}

@Override
public RxtxChannelConfig setWaitTimeMillis(final int waitTimeMillis) {
if (waitTimeMillis < 0) {
throw new IllegalArgumentException("Wait time must be >= 0");
}
waitTime = waitTimeMillis;
return this;
}

@Override
public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (RxtxChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
*/
package io.netty.channel.rxtx;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import static io.netty.channel.rxtx.RxtxChannelOption.*;

import io.netty.channel.ChannelPromise;
import io.netty.channel.socket.oio.OioByteStreamChannel;

import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;

import static io.netty.channel.rxtx.RxtxChannelOption.*;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

/**
* A channel to a serial device using the RXTX library.
Expand Down Expand Up @@ -53,6 +56,11 @@ public boolean isOpen() {
return open;
}

@Override
protected AbstractUnsafe newUnsafe() {
return new RxtxUnsafe();
}

@Override
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
RxtxDeviceAddress remote = (RxtxDeviceAddress) remoteAddress;
Expand All @@ -62,11 +70,14 @@ protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress
deviceAddress = remote;

serialPort = (SerialPort) commPort;
}

protected void doInit() throws Exception {
serialPort.setSerialPortParams(
config().getOption(BAUD_RATE),
config().getOption(DATA_BITS).value(),
config().getOption(STOP_BITS).value(),
config().getOption(PARITY_BIT).value()
config().getOption(BAUD_RATE),
config().getOption(DATA_BITS).value(),
config().getOption(STOP_BITS).value(),
config().getOption(PARITY_BIT).value()
);
serialPort.setDTR(config().getOption(DTR));
serialPort.setRTS(config().getOption(RTS));
Expand Down Expand Up @@ -117,4 +128,57 @@ protected void doClose() throws Exception {
}
}
}

private final class RxtxUnsafe extends AbstractUnsafe {
@Override
public void connect(
final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) {
return;
}

try {
final boolean wasActive = isActive();
doConnect(remoteAddress, localAddress);

int waitTime = config().getOption(WAIT_TIME);
if (waitTime > 0) {
eventLoop().schedule(new Runnable() {
@Override
public void run() {
try {
doInit();
promise.setSuccess();
if (!wasActive && isActive()) {
pipeline().fireChannelActive();
}
} catch (Throwable t) {
promise.setFailure(t);
closeIfClosed();
}
}
}, waitTime, TimeUnit.MILLISECONDS);
} else {
doInit();
promise.setSuccess();
if (!wasActive && isActive()) {
pipeline().fireChannelActive();
}
}
} catch (Throwable t) {
promise.setFailure(t);
closeIfClosed();
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, promise);
}
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
* <td>{@link io.netty.channel.rxtx.RxtxChannelOption#DATA_BITS}</td><td>{@link #setDatabits(Databits)}</td>
* </tr><tr>
* <td>{@link io.netty.channel.rxtx.RxtxChannelOption#PARITY_BIT}</td><td>{@link #setParitybit(Paritybit)}</td>
* </tr><tr>
* <td>{@link io.netty.channel.rxtx.RxtxChannelOption#WAIT_TIME}</td><td>{@link #setWaitTimeMillis(int)}</td>
* </tr>
* </table>
*/
Expand Down Expand Up @@ -239,6 +241,23 @@ public static Paritybit valueOf(int value) {
*/
RxtxChannelConfig setRts(boolean rts);

/**
* @return The number of milliseconds to wait between opening the serial port and
* initialising.
*/
int getWaitTimeMillis();

/**
* Sets the time to wait after opening the serial port and before sending it any
* configuration information or data. A value of 0 indicates that no waiting should
* occur.
*
* @param waitTimeMillis The number of milliseconds to wait, defaulting to 0 (no
* wait) if unset
* @throws IllegalArgumentException if the supplied value is < 0
*/
RxtxChannelConfig setWaitTimeMillis(int waitTimeMillis);

@Override
RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public final class RxtxChannelOption<T> extends ChannelOption<T> {
public static final RxtxChannelOption<Paritybit> PARITY_BIT =
new RxtxChannelOption<Paritybit>("PARITY_BIT");

public static final RxtxChannelOption<Integer> WAIT_TIME =
new RxtxChannelOption<Integer>("WAIT_TIME");

private RxtxChannelOption(String name) {
super(name);
}
Expand Down

0 comments on commit 318328b

Please sign in to comment.