Newer
Older
casic_unitree_dog / src / message_handler.cpp
#include <iostream>
#include <thread>
#include <boost/asio.hpp>

#include "tcp_client.hpp"
#include "tcp_service.hpp"
#include "serial_port_wrapper.hpp"

int main() {
    // 创建一个共享的 io 对象
    boost::asio::io_service io;

    // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动
    std::thread client_thread([&io] {
        TcpClient client("eth0");
        client.connect("192.168.123.18", 9001);
        io.run();
    });

    // 启动TCP服务,接收AI结果并通过串口控制机械臂
    std::thread service_thread([&io] {
        TcpService service(io, "/dev/ttyACM0", 115200); //机械臂串口
        service.start(8888);
        io.run();
    });

    // 启动甲烷数据查询串口线程,定时发送查询指令
    std::thread gas_serial_port_thread([&io] {
        try {
            //打开串口
            SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口
            wrapper.read_from_port();
            io.run();
        } catch (std::exception &e) {
            std::cerr << e.what() << std::endl;
        }
    });

    // 等待所有线程完成
    client_thread.join();
    service_thread.join();
    gas_serial_port_thread.join();
    return 0;
}