boost实现串口通信(一):小试牛刀

 1 /************************************************************************/
 2 /* 功能:boost实现串口通信类                                            */
 3 /* 作者: kernelmain QQ:835609451                                        */
 4 /* 版本:v0.1 build at 2014.3.25                                        */
 5 /************************************************************************/
 6 
 7 #include <boost/asio.hpp>
 8 #include <boost/bind.hpp>
 9 #include <iostream>
10 
11 using namespace std;
12 using namespace boost::asio;
13 
14 typedef string anytype;
15 
16 class SuperTerminal
17 {
18 public:
19     SuperTerminal(const anytype &port_name);
20     ~SuperTerminal();
21 
22 private:
23     bool init_port(const anytype port, const unsigned int char_size);
24 
25 public:
26     void write_to_serial(const anytype data);
27     void read_from_serial();
28     void handle_read(char buf[], boost::system::error_code ec,std::size_t bytes_transferred);
29     void call_handle();
30 
31 private:
32     io_service m_ios;
33     serial_port *pSerialPort;
34     anytype m_port;
35     boost::system::error_code m_ec;
36 };
 1 /************************************************************************/
 2 /* 功能:boost实现串口通信类                                            */
 3 /* 作者: kernelmain QQ:835609451                                        */
 4 /* 版本:v0.1 build at 2014.3.25                                        */
 5 /************************************************************************/
 6 #include "SuperTerminal.h"
 7 
 8 SuperTerminal::SuperTerminal(const anytype & port_name):pSerialPort(NULL)
 9 {
10     pSerialPort = new serial_port(m_ios);
11     if (pSerialPort)
12     {
13         init_port(port_name,8);
14     }
15 }
16 
17 SuperTerminal::~SuperTerminal()
18 {
19     if (pSerialPort)
20     {
21         delete pSerialPort;
22     }
23 }
24 
25 bool SuperTerminal::init_port(const anytype port, const unsigned int char_size)
26 {
27     if (!pSerialPort)
28     {
29         return false;
30     }
31 
32     pSerialPort->open(port,m_ec);
33 
34     pSerialPort->set_option(serial_port::baud_rate(115200),m_ec);
35     pSerialPort->set_option(serial_port::flow_control(serial_port::flow_control::none),m_ec);
36     pSerialPort->set_option(serial_port::parity(serial_port::parity::none),m_ec);
37     pSerialPort->set_option(serial_port::stop_bits(serial_port::stop_bits::one),m_ec);
38     pSerialPort->set_option(serial_port::character_size(char_size),m_ec);
39 
40     return true;
41 }
42 
43 void SuperTerminal::write_to_serial(const anytype data)
44 {
45     if (!pSerialPort)
46     {
47         return;
48     }
49 
50     size_t len = write(*pSerialPort, buffer(data),m_ec);
51     cout << len << "\t" <<  data << endl;
52 }
53 
54 void SuperTerminal::handle_read(char buf[], boost::system::error_code ec,std::size_t bytes_transferred)
55 {
56     cout << "\nhandle_read: " ;
57     cout.write(buf, bytes_transferred);
58 }
59 
60 void SuperTerminal::read_from_serial()
61 {
62     char v[10];
63     async_read(*pSerialPort, buffer(v), boost::bind(&SuperTerminal::handle_read,this,v,_1, _2));
64 }
65 
66 void SuperTerminal::call_handle()
67 {
68     m_ios.run();
69 }

一直比较喜欢c++的简洁, 尤其用上boost后,代码显得更加简洁。

想把之前给同学用C#做的刷苹果设备软件用C++重写一下,一点一点来, 计划:

(1). 在控件台程序中调试熟悉boost串口通信

(2). 用C++ builder xe5画界面,调试。

 (3). 实现windows超级终端的所有功能。

 (4). 添加扩展功能。

 

下面是控件台程序测试代码:

 1 #include <vector>
 2 #include <string>
 3 
 4 #define BOOST_REGEX_NO_LIB
 5 #define BOOST_DATE_TIME_SOURCE
 6 #define BOOST_SYSTEM_NO_LIB
 7 #include "SuperTerminal.h"
 8 
 9 int main()
10 {
11     try
12     {
13         SuperTerminal sp("COM1");
14         sp.write_to_serial("serialPort");
15         sp.read_from_serial();
16         sp.call_handle();
17         getchar();
18         return 0;
19     }
20     catch (std::exception &e)
21     {
22         cout << e.what();
23         getchar();
24     }
25 }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个使用ROS和Boost实现串口通信的案例: 1. 在ROS工作空间中创建一个新的ROS包,例如“serial_communication”。 2. 在src目录下创建一个名为“serial_communication_node.cpp”的节点文件,并编写以下代码: ```c++ #include <ros/ros.h> #include <boost/asio.hpp> #include <boost/bind.hpp> #define SERIAL_PORT "/dev/ttyUSB0" // 串口号 #define BAUD_RATE 115200 // 波特率 class SerialCommunicationNode { public: SerialCommunicationNode() : io_service_(), serial_port_(io_service_) { // 打开串口 serial_port_.open(SERIAL_PORT); serial_port_.set_option(boost::asio::serial_port_base::baud_rate(BAUD_RATE)); // 创建定时器 timer_ = nh_.createTimer(ros::Duration(0.1), boost::bind(&SerialCommunicationNode::readSerialData, this)); } void readSerialData() { // 读取串口数据 boost::asio::async_read(serial_port_, boost::asio::buffer(buffer_), boost::asio::transfer_at_least(1), boost::bind(&SerialCommunicationNode::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void handleRead(const boost::system::error_code& error, size_t bytes_transferred) { if (!error) { // 将读取到的串口数据发布为ROS消息 std_msgs::String msg; msg.data = std::string(buffer_.begin(), buffer_.begin() + bytes_transferred); serial_data_pub_.publish(msg); } } private: ros::NodeHandle nh_; ros::Publisher serial_data_pub_ = nh_.advertise<std_msgs::String>("serial_data", 1); ros::Timer timer_; boost::asio::io_service io_service_; boost::asio::serial_port serial_port_; std::vector<uint8_t> buffer_; }; int main(int argc, char** argv) { ros::init(argc, argv, "serial_communication_node"); SerialCommunicationNode node; ros::spin(); return 0; } ``` 3. 在CMakeLists.txt中添加以下内容: ``` find_package(Boost REQUIRED COMPONENTS system thread) include_directories(${Boost_INCLUDE_DIRS}) add_executable(serial_communication_node src/serial_communication_node.cpp) target_link_libraries(serial_communication_node ${Boost_LIBRARIES}) ``` 4. 构建并运行ROS节点: ``` catkin_make rosrun serial_communication serial_communication_node ``` 5. 在另一个终端中,使用roscore启动ROS核心,并使用rostopic命令检查是否已发布“serial_data”主题: ``` roscore rostopic list ``` 6. 将串口设备连接到计算机上,并使用minicom或其他串口工具发送数据。这将导致ROS节点将读取的数据作为ROS消息发布到“serial_data”主题中。 ``` minicom -b 115200 -D /dev/ttyUSB0 ``` 7. 最后,您可以使用rostopic echo命令来查看从串口读取的数据: ``` rostopic echo serial_data ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值