【问题标题】:Communicating with the COM PORT in C++在 C++ 中与 COM PORT 通信
【发布时间】:2018-12-12 20:02:18
【问题描述】:

我必须编写一个程序,将消息发送到 C++ 中连接在 COM PORT 上的 UART 转换器。我实际上从一开始就被困住了。我从来没有做过这样的事情,也没有找到任何例子。 谁能帮帮我? 感谢任何帮助;)

【问题讨论】:

    标签: c++ port


    【解决方案1】:

    Boost 有一个不错的库和示例:https://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/overview/serial_ports.html

    boost 库的使用示例发布在这里:Clear input data from serial port in boost::asio

    #include <boost/asio.hpp>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    using namespace boost::asio;
    
    int main()
    {
        io_service io_service;
        serial_port port(io_service, "/dev/ttyACM0");
        port.set_option(serial_port_base::baud_rate(9600));
        vector<char> buf(1);
        read(port, buffer(buf));
        cout << (int) buf[0] << endl;
        return 0;
    }
    

    同样可以用 write 来完成:

    std::string s;
    write(port,buffer(s.c_str(),s.size()));
    

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 2019-10-30
      • 2012-04-18
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      相关资源
      最近更新 更多