| 1 | #include <boost/asio.hpp>
|
|---|
| 2 | #include <iostream>
|
|---|
| 3 |
|
|---|
| 4 | void writeComplete(const boost::system::error_code & ec, size_t transferred)
|
|---|
| 5 | {
|
|---|
| 6 | if (ec)
|
|---|
| 7 | {
|
|---|
| 8 | // cancel was called before the io_service was entered. I would
|
|---|
| 9 | // expect an operation aborted error
|
|---|
| 10 | std::cout << "operation cancelled" << std::endl;
|
|---|
| 11 | }
|
|---|
| 12 | else
|
|---|
| 13 | {
|
|---|
| 14 | std::cout << transferred << " bytes transferred (not cancelled!)" << std::endl;
|
|---|
| 15 | }
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | int main(int argc, char * argv[])
|
|---|
| 19 | {
|
|---|
| 20 | boost::asio::io_service my_io_service;
|
|---|
| 21 | char msg[] = {'h', 'e', 'l', 'l', '0', 0};
|
|---|
| 22 |
|
|---|
| 23 | {
|
|---|
| 24 | boost::asio::serial_port port(my_io_service, "/dev/ttyS1");
|
|---|
| 25 |
|
|---|
| 26 | // serial port is opened and we initiate an async write to probe for a device
|
|---|
| 27 | // attached
|
|---|
| 28 | boost::asio::async_write(port, boost::asio::const_buffers_1(msg, sizeof(msg)), writeComplete);
|
|---|
| 29 |
|
|---|
| 30 | // assume elsewhere in the code an error occurred, and we cancel all processing
|
|---|
| 31 | port.cancel();
|
|---|
| 32 |
|
|---|
| 33 | // the cleanup of the error also causes the serial port to be destroyed.
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | // meanwhile the error cleanup has completed and we're ready to continue without using
|
|---|
| 37 | // the serial port
|
|---|
| 38 | std::cout << "entering io_service" << std::endl;
|
|---|
| 39 |
|
|---|
| 40 | my_io_service.run();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|