C + + Boost serial application for Ros and slave communication

Source: Internet
Author: User

First, migrate the C + + Boost Library:

1. First go to the Boost website to download the latest boost version, I downloaded the boost_1_6_0 version, unzip.

2. Go to the Unzip directory: CD Boost_1_6_0, execute the following command:

$./bootstrap.sh--prefix=path/to/installation/prefix

The value of prefix is the path where you want to install boost, and the default is to install it under/usr/local if you do not turn on this parameter. I am installing in the/home/xzz/boost_1_56_0 directory:

$./bootstrap.sh--prefix=/home/cam/boost_1_6_0

Note: home directory do not use ~ to indicate that the compilation script does not recognize ~, will be currently a new directory named ' ~ '.

Then execute:

$./B2 Install

This command installs the Boost header file folder include/in the prefix defined directory, compiles all the boost modules, and places the compiled Library folder lib/in the prefix defined directory. All if compiled successfully, the prefix directory, the/HOME/XZZ/BOOST_1_56_0 directory, should contain include/and lib/two folders.

3. Test, in the src/directory of any feature pack in your Ros workspace, create the Boost_bode.cpp file with the following contents:

First, test the function module that relies only on the header file:

ASIO not only supports network communication, but also supports serial communication. For two devices to use serial communication, the key is to set the correct parameters, such as: baud rate, parity bit, stop bit, character size and flow control. Two serial devices only the same parameters are set to talk to each other.

ASIO provides the Boost::asio::serial_port class, which has a set_option (const settableserialportoption& option) method that is used to set the parameters enumerated above, The option can be:

    • Serial_port::baud_rate baud rate, construction parameter is unsigned int
    • Serial_port::p arity parity, construction parameter is serial_port::p arity::type,enum type, can be none, odd, even.
    • Serial_port::flow_control flow control, construction parameter is Serial_port::flow_control::type,enum type, can be none software hardware
    • Serial_port::stop_bits stop bit, construction parameter is Serial_port::stop_bits::type,enum type, can be one onepointfive two
    • serial_port::character_size character size, construction parameter is unsigned in
#include <iostream> #include <boost/asio.hpp> #include <boost/bind.hpp>using namespace std;using namespace Boost::asio;int Main (int argc, char* argv[]) {        io_service Iosev;        Node file        serial_port sp (Iosev, "/dev/ttyusb0");        Set the parameter        sp.set_option (Serial_port::baud_rate (115200));        Sp.set_option (Serial_port::flow_control (Serial_port::flow_control::none));        Sp.set_option (serial_port::p arity (serial_port::p arity::none));        Sp.set_option (Serial_port::stop_bits (Serial_port::stop_bits::one));        Sp.set_option (Serial_port::character_size (8));        Writes data to the serial port write        (sp, buffer ("Hello world");        Read data to the serial port        Char buf[1];        Read (SP, buffer (BUF)); printf ("buf[0] =%c\n", buf[0]);        Iosev.run ();        return 0;}

Add compilation options: Add the CMakeLists.txt file in your Feature pack:

Add_executable (Boost_node src/boost_node.cpp)
Target_link_libraries (Boost_node ${catkin_libraries})

Compile the Boost test program: In the top-level directory of your work space

Catkin_make

Terminate called after throwing an instance of ' Boost::exception_detail::clone_impl<boost::exception_detail::error _info_injector<boost::system::system_error> > '
What (): Open:permission denied
Aborted (core dumped)
Seems to be insufficient authority.

Connect your electricity and another computer through the serial port. Operations Root User Run

[Email protected]:/home/cam/catkin_ws/devel/lib/odom_tf_package#./boost_node buf[0] = 0

C + + Boost serial application for Ros and slave communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.