Re: [asio-users] How to Enable/Disable Serial Port DTR?
Brought to you by:
chris_kohlhoff
|
From: Christopher K. <ch...@ko...> - 2009-08-31 14:08:45
|
Robert Dailey wrote:
> However, the problem I have is that I'm using async_write(). In other
> words, I have to enable the DTR before I call async_write(), and turn it
> off in the write completion callback. This is error prone because if
> writes are sent fast enough, DTR could be enabled several, consecutive
> times, and also disabled several consecutive times.
Do you mean you are starting new async_write operations before the
previous ones have finished? If so, don't, as async_write is a composed
operation (meaning it is made up of one or more calls to another async
operation, namely async_write_some). You may garble your data.
So in that case the approach you use should work fine. You could also
consider wrapping it in another async operation, e.g.:
template <typename Handler>
struct dtr_enabled_write_handler
{
serial_port& sp;
Handler h;
void operator()(error_code ec, size_t length)
{
serial_port::native_type handle = sp.native();
EscapeCommFunction(handle, CLRDTR);
h(ec, length);
}
};
template <typename MutableBufferSequence, typename Handler>
void dtr_enabled_async_write(serial_port& sp,
const MutableBufferSequence& b, Handler h)
{
serial_port::native_type handle = sp.native();
EscapeCommFunction(handle, SETDTR);
dtr_enabled_write_handler<Handler> dewh = { sp, h };
async_write(sp, b, dewh);
}
Cheers,
Chris
|