Discussion:
[asio-users] multiple serial ports/sessions?
martin naskovski
2014-07-16 08:51:59 UTC
Permalink
Hi - I'm a fairly experienced programmer however new to Boost's ASIO and
would like to ask for some direction regarding usage of multiple serial
ports with ASIO.

I dug up a "minicom" example which does exactly what I need it to - async
read and writes to the serial port. I also found similar logic in the posix
chat client (fully asynchronous) and a few other examples, however my
specific use case involves reading from multiple serial ports...

My app works great modeled after the minicom example, however now I have a
need to open up one more, completely different serial port and attach it to
the same io_service that the first serial port is running on. The first
serial port has read and write operations applied to it, whereas the second
serial port (console) will only have async_read_until('\n') and dump to a
log file...

I am unsure as to how to implement a two serial port with a single
io_service, where both ports would be constantly streaming (i.e. via
async_read_until()) and only one of them will be written to...

Again, I have my app working great using ASIO with a single serial port,
but I'm a little lost due to lack of clear example (or maybe I just have
been looking in the wrong place?) on how would I go about doing the same
with 2 (or more) serial ports.

Any direction/guidance on where to look and what to read up about this is
appreciated.

thank you in advance,

Martin
Yuri Timenkov
2014-07-16 09:24:53 UTC
Permalink
Hi Martin,


Not sure if serial port has some specifics, but ASIO is generally designed to work well with multiple descriptors bound to the same io_service object.


Think of io_service as of event loop which only dispatches handlers. So as long as you don’t mix up write operations or handlers to different io objects (ports) it should work fine.


So just create a special handler object/function which you will pass to async_read_until, and which re-schedules itself when read is complete.


What you should be aware of is to not occupy io_service with long synchronous operations which you may invoke from read handler (for example, if your log is on network share or system experiences disk shortage).


Regards,

Yuri





From: martin naskovski
Sent: ‎Wednesday‎, ‎July‎ ‎16‎, ‎2014 ‎11‎:‎19‎ ‎AM
To: asio-***@lists.sourceforge.net





Hi - I'm a fairly experienced programmer however new to Boost's ASIO and would like to ask for some direction regarding usage of multiple serial ports with ASIO.



I dug up a "minicom" example which does exactly what I need it to - async read and writes to the serial port. I also found similar logic in the posix chat client (fully asynchronous) and a few other examples, however my specific use case involves reading from multiple serial ports...




My app works great modeled after the minicom example, however now I have a need to open up one more, completely different serial port and attach it to the same io_service that the first serial port is running on. The first serial port has read and write operations applied to it, whereas the second serial port (console) will only have async_read_until('\n') and dump to a log file...




I am unsure as to how to implement a two serial port with a single io_service, where both ports would be constantly streaming (i.e. via async_read_until()) and only one of them will be written to...




Again, I have my app working great using ASIO with a single serial port, but I'm a little lost due to lack of clear example (or maybe I just have been looking in the wrong place?) on how would I go about doing the same with 2 (or more) serial ports.




Any direction/guidance on where to look and what to read up about this is appreciated.




thank you in advance,




Martin
martin naskovski
2014-07-16 22:34:05 UTC
Permalink
Yuri, I got it working, it was actually easier than I thought. I always
look at the source code for good examples so I was slightly paranoid about
not having one that matches my specific use case (or close enough to
it).... Works beautifully - the only thing I'm wondering is if
async_read_until() could have used buffers instead of streambuf.
Regardless, I'm very impressed with ASIO.




On Wed, Jul 16, 2014 at 2:24 AM, Yuri Timenkov <***@timenkov.ru> wrote:

> Hi Martin,
>
> Not sure if serial port has some specifics, but ASIO is generally designed
> to work well with multiple descriptors bound to the same io_service object.
>
> Think of io_service as of event loop which only dispatches handlers. So as
> long as you don’t mix up write operations or handlers to different io
> objects (ports) it should work fine.
>
> So just create a special handler object/function which you will pass to
> async_read_until, and which re-schedules itself when read is complete.
>
> What you should be aware of is to not occupy io_service with long
> synchronous operations which you may invoke from read handler (for example,
> if your log is on network share or system experiences disk shortage).
>
> Regards,
> Yuri
>
> *From:* martin naskovski <***@naskovski.info>
> *Sent:* ‎Wednesday‎, ‎July‎ ‎16‎, ‎2014 ‎11‎:‎19‎ ‎AM
> *To:* asio-***@lists.sourceforge.net
>
> Hi - I'm a fairly experienced programmer however new to Boost's ASIO and
> would like to ask for some direction regarding usage of multiple serial
> ports with ASIO.
>
> I dug up a "minicom" example which does exactly what I need it to - async
> read and writes to the serial port. I also found similar logic in the posix
> chat client (fully asynchronous) and a few other examples, however my
> specific use case involves reading from multiple serial ports...
>
> My app works great modeled after the minicom example, however now I have a
> need to open up one more, completely different serial port and attach it to
> the same io_service that the first serial port is running on. The first
> serial port has read and write operations applied to it, whereas the second
> serial port (console) will only have async_read_until('\n') and dump to a
> log file...
>
> I am unsure as to how to implement a two serial port with a single
> io_service, where both ports would be constantly streaming (i.e. via
> async_read_until()) and only one of them will be written to...
>
> Again, I have my app working great using ASIO with a single serial port,
> but I'm a little lost due to lack of clear example (or maybe I just have
> been looking in the wrong place?) on how would I go about doing the same
> with 2 (or more) serial ports.
>
> Any direction/guidance on where to look and what to read up about this is
> appreciated.
>
> thank you in advance,
>
> Martin
>
>
> ------------------------------------------------------------------------------
> Want fast and easy access to all the code in your enterprise? Index and
> search up to 200,000 lines of code with a free copy of Black Duck
> Code Sight - the same software that powers the world's largest code
> search on Ohloh, the Black Duck Open Hub! Try it now.
> http://p.sf.net/sfu/bds
> _______________________________________________
> asio-users mailing list
> asio-***@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/asio-users
> _______________________________________________
> Using Asio? List your project at
> http://think-async.com/Asio/WhoIsUsingAsio
>
>
Yuri Timenkov
2014-07-19 08:56:47 UTC
Permalink
Hi Martin,

Glad that it worked :)

I think that you have to use streambuf because async_read_until is a complex operation and internally consists of multiple calls to async_read_some. Because one can't know in advance how much data arrives (otherwise async_read would be use,d right?) async_read_until has to grow buffer between reads which is not possible with generic asio::buffer wrapper (which is supposed to be as light as possible and is just an API adapter for a chunk of memory).

Another thing you should be aware of when using read_until is that it may leave some data in the buffer, so that the amount of data actually read from the socket is greater than the length received in Read Handler. IIRC successive calls to read_until may skip I/O operation if there is a match in the buffer (to handle cases like this).

Good luck on your async journeys :)
Yuri

Date: Wed, 16 Jul 2014 15:34:05 -0700
From: ***@naskovski.info
To: asio-***@lists.sourceforge.net
Subject: Re: [asio-users] multiple serial ports/sessions?

Yuri, I got it working, it was actually easier than I thought. I always look at the source code for good examples so I was slightly paranoid about not having one that matches my specific use case (or close enough to it).... Works beautifully - the only thing I'm wondering is if async_read_until() could have used buffers instead of streambuf. Regardless, I'm very impressed with ASIO.




On Wed, Jul 16, 2014 at 2:24 AM, Yuri Timenkov <***@timenkov.ru> wrote:







Hi Martin,

Not sure if serial port has some specifics, but ASIO is generally designed to work well with multiple descriptors bound to the same io_service object.
Think of io_service as of event loop which only dispatches handlers. So as long as you don’t mix up write operations or handlers to different io objects (ports) it should work fine.

So just create a special handler object/function which you will pass to async_read_until, and which re-schedules itself when read is complete.
What you should be aware of is to not occupy io_service with long synchronous operations which you may invoke from read handler (for example, if your log is on network share or system experiences disk shortage).

Regards,Yuri
From: martin naskovski

Sent: ýWednesdayý, ýJulyý ý16ý, ý2014 ý11ý:ý19ý ýAM
To: asio-***@lists.sourceforge.net

Hi - I'm a fairly experienced programmer however new to Boost's ASIO and would like to ask for some direction regarding usage of multiple serial ports with ASIO.
I dug up a "minicom" example which does exactly what I need it to - async read and writes to the serial port. I also found similar logic in the posix chat client (fully asynchronous) and a few other examples, however my specific use case involves reading from multiple serial ports...


My app works great modeled after the minicom example, however now I have a need to open up one more, completely different serial port and attach it to the same io_service that the first serial port is running on. The first serial port has read and write operations applied to it, whereas the second serial port (console) will only have async_read_until('\n') and dump to a log file...


I am unsure as to how to implement a two serial port with a single io_service, where both ports would be constantly streaming (i.e. via async_read_until()) and only one of them will be written to...


Again, I have my app working great using ASIO with a single serial port, but I'm a little lost due to lack of clear example (or maybe I just have been looking in the wrong place?) on how would I go about doing the same with 2 (or more) serial ports.


Any direction/guidance on where to look and what to read up about this is appreciated.
thank you in advance,
Martin




------------------------------------------------------------------------------

Want fast and easy access to all the code in your enterprise? Index and

search up to 200,000 lines of code with a free copy of Black Duck

Code Sight - the same software that powers the world's largest code

search on Ohloh, the Black Duck Open Hub! Try it now.

http://p.sf.net/sfu/bds
_______________________________________________

asio-users mailing list

asio-***@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/asio-users

_______________________________________________

Using Asio? List your project at

http://think-async.com/Asio/WhoIsUsingAsio





------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Loading...