You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Boost.Asio在OTClientV8中读取COM端口的技术问题

串口读取功能集成问题(OTClientV8 + Boost.Asio)

我有一台设备每秒向COM5端口发送0-100的整数信号(数值最大变化±3),需要在Windows平台下为开源C++游戏客户端OTClientV8添加基于Boost.Asio的COM端口读取功能,先实现终端每秒显示该数值(后续用于玩家技能展示及服务器传输)。

作为无串口开发经验的Python开发者,刚接触C++,参考项目模块及工具编写了如下代码,客户端编译通过但无相关输出(已用Python验证COM5有数据,且g_logger可正常输出内容)。

新增文件 com.cpp

#include <boost/asio/read.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include "framework/global.h"
#include "com.h"

COM g_com;

COM::COM() : port(io_service, "COM5") {
    // 配置串口参数
    port.set_option(boost::asio::serial_port_base::baud_rate(9600));
    port.set_option(boost::asio::serial_port_base::character_size(8));
    port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
    port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
    port.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
}

void COM::startAsyncRead() {
    // 异步读取数据到缓冲区
    boost::asio::async_read_until(port, buffer, '\n',
        [this](boost::system::error_code error, std::size_t bytes_transferred) {
            if (!error) {
                // 处理接收的数据
                std::istream is(&buffer);
                std::string line;
                std::getline(is, line);
                g_logger.info(stdext::format("This is my output from com: %s", line));

                // 继续异步读取
                startAsyncRead();
            } else {
                // 错误处理
                std::cerr << "Error reading from serial port: " << error.message() << std::endl;
            }
        });
}

void COM::init() {
    // 在独立线程中启动异步读取
    asyncThread = std::thread([this] {
        io_service.run();
    });

    // 启动首次异步读取
    startAsyncRead();
}

void COM::terminate() {
    // 停止io_service并等待线程结束
    io_service.stop();
    asyncThread.join();
    g_logger.info("Bye!");
}

新增文件 com.h

#ifndef COM_H
#define COM_H

#include <boost/asio.hpp>
#include <thread>

class COM {
public:
    COM(); // 构造函数
    void init();
    void terminate();

private:
    void startAsyncRead();

private:
    boost::asio::io_service io_service;
    boost::asio::serial_port port;
    boost::asio::streambuf buffer;
    std::thread asyncThread;
};

extern COM g_com;

#endif

修改文件 client.cpp

Client g_client;

void Client::init(std::vector<std::string>& args)
{
    // 注册所需Lua函数
    registerLuaFunctions();

    g_map.init();
    g_minimap.init();
    g_game.init();
    g_shaders.init();
    g_things.init();
    g_healthBars.init();
    g_com.init(); // 新增代码

}

void Client::terminate()
{
    g_creatures.terminate();
    g_game.terminate();
    g_map.terminate();
    g_minimap.terminate();
    g_things.terminate();
    g_sprites.terminate();
    g_shaders.terminate();
    g_healthBars.terminate();
    g_com.terminate();  // 新增代码
}

问题

  • 该代码实现C++读取COM端口的方式是否合理?
  • 为何代码编译通过但终端无输出?

注:经调整后代码已可正常工作。


内容的提问来源于stack exchange,提问作者DoDr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 01:10:03