diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 1c59b4a..8747942 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -7,37 +7,38 @@ #include "serial_port_wrapper.hpp" int main() { + // 创建一个共享的 io 对象 + boost::asio::io_service io; + // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([] { + std::thread client_thread([&io] { TcpClient client("eth0"); client.connect("192.168.123.18", 9001); + io.run(); }); - client_thread.join(); // 启动TCP服务,接收AI结果并通过串口控制机械臂 - std::thread service_thread([] { - boost::asio::io_context _context; - TcpService service(_context, "/dev/ttyACM0", 115200); //机械臂串口 + std::thread service_thread([&io] { + TcpService service(io, "/dev/ttyACM0", 115200); //机械臂串口 service.start(8888); + io.run(); }); - service_thread.join(); // 启动甲烷数据查询串口线程,定时发送查询指令 - std::thread gas_serial_port_thread([] { + std::thread gas_serial_port_thread([&io] { try { - boost::asio::io_service io; //打开串口 SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - - // 从串口读取数据 - boost::asio::streambuf buffer; - wrapper.read_from_port(buffer); - + 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; } diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 1c59b4a..8747942 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -7,37 +7,38 @@ #include "serial_port_wrapper.hpp" int main() { + // 创建一个共享的 io 对象 + boost::asio::io_service io; + // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([] { + std::thread client_thread([&io] { TcpClient client("eth0"); client.connect("192.168.123.18", 9001); + io.run(); }); - client_thread.join(); // 启动TCP服务,接收AI结果并通过串口控制机械臂 - std::thread service_thread([] { - boost::asio::io_context _context; - TcpService service(_context, "/dev/ttyACM0", 115200); //机械臂串口 + std::thread service_thread([&io] { + TcpService service(io, "/dev/ttyACM0", 115200); //机械臂串口 service.start(8888); + io.run(); }); - service_thread.join(); // 启动甲烷数据查询串口线程,定时发送查询指令 - std::thread gas_serial_port_thread([] { + std::thread gas_serial_port_thread([&io] { try { - boost::asio::io_service io; //打开串口 SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - - // 从串口读取数据 - boost::asio::streambuf buffer; - wrapper.read_from_port(buffer); - + 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; } diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp index fbb54c5..cde8a36 100644 --- a/src/serial_port_wrapper.cpp +++ b/src/serial_port_wrapper.cpp @@ -18,21 +18,30 @@ SerialPortWrapper::SerialPortWrapper( boost::asio::io_service &io_service, const std::string &port_name, const int baud_rate -): io_service_(io_service), port_(io_service, port_name), timer_(io_service, std::chrono::seconds(3)) { +): io_service_(io_service), port_(io_service, port_name), timer_(io_service) { port_.set_option(serial_port_base::baud_rate(baud_rate)); port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } else { + std::cerr << "Timer error: " << error.message() << std::endl; + } + }); } //CC 05 00 00 00 00 0D 77 -void SerialPortWrapper::read_from_port(boost::asio::streambuf &buffer) { +void SerialPortWrapper::read_from_port() { async_read_until( - port_, buffer, 0x77, //结束位,一定要匹配 - [this,&buffer](const boost::system::error_code &error, const size_t bytes_transferred) { + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { if (!error) { // 获取接收到的数据 - std::istream is(&buffer); + std::istream is(&buffer_); std::vector received_data(static_cast(bytes_transferred)); is.read( reinterpret_cast(received_data.data()), static_cast(bytes_transferred) @@ -51,11 +60,10 @@ handle_data(received_data); - // 递归调用以继续读取数据 - read_from_port(buffer); + read_from_port(); } else { std::cerr << "error: " << error.message() << std::endl; + read_from_port(); } - } - ); -} \ No newline at end of file + }); +} diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 1c59b4a..8747942 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -7,37 +7,38 @@ #include "serial_port_wrapper.hpp" int main() { + // 创建一个共享的 io 对象 + boost::asio::io_service io; + // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([] { + std::thread client_thread([&io] { TcpClient client("eth0"); client.connect("192.168.123.18", 9001); + io.run(); }); - client_thread.join(); // 启动TCP服务,接收AI结果并通过串口控制机械臂 - std::thread service_thread([] { - boost::asio::io_context _context; - TcpService service(_context, "/dev/ttyACM0", 115200); //机械臂串口 + std::thread service_thread([&io] { + TcpService service(io, "/dev/ttyACM0", 115200); //机械臂串口 service.start(8888); + io.run(); }); - service_thread.join(); // 启动甲烷数据查询串口线程,定时发送查询指令 - std::thread gas_serial_port_thread([] { + std::thread gas_serial_port_thread([&io] { try { - boost::asio::io_service io; //打开串口 SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - - // 从串口读取数据 - boost::asio::streambuf buffer; - wrapper.read_from_port(buffer); - + 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; } diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp index fbb54c5..cde8a36 100644 --- a/src/serial_port_wrapper.cpp +++ b/src/serial_port_wrapper.cpp @@ -18,21 +18,30 @@ SerialPortWrapper::SerialPortWrapper( boost::asio::io_service &io_service, const std::string &port_name, const int baud_rate -): io_service_(io_service), port_(io_service, port_name), timer_(io_service, std::chrono::seconds(3)) { +): io_service_(io_service), port_(io_service, port_name), timer_(io_service) { port_.set_option(serial_port_base::baud_rate(baud_rate)); port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } else { + std::cerr << "Timer error: " << error.message() << std::endl; + } + }); } //CC 05 00 00 00 00 0D 77 -void SerialPortWrapper::read_from_port(boost::asio::streambuf &buffer) { +void SerialPortWrapper::read_from_port() { async_read_until( - port_, buffer, 0x77, //结束位,一定要匹配 - [this,&buffer](const boost::system::error_code &error, const size_t bytes_transferred) { + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { if (!error) { // 获取接收到的数据 - std::istream is(&buffer); + std::istream is(&buffer_); std::vector received_data(static_cast(bytes_transferred)); is.read( reinterpret_cast(received_data.data()), static_cast(bytes_transferred) @@ -51,11 +60,10 @@ handle_data(received_data); - // 递归调用以继续读取数据 - read_from_port(buffer); + read_from_port(); } else { std::cerr << "error: " << error.message() << std::endl; + read_from_port(); } - } - ); -} \ No newline at end of file + }); +} diff --git a/src/serial_port_wrapper.hpp b/src/serial_port_wrapper.hpp index 04591db..b4c90f4 100644 --- a/src/serial_port_wrapper.hpp +++ b/src/serial_port_wrapper.hpp @@ -17,12 +17,13 @@ boost::asio::serial_port &get() { return port_; } - void read_from_port(boost::asio::streambuf &buffer); + void read_from_port(); private: boost::asio::io_service &io_service_; boost::asio::serial_port port_; - steady_timer timer_; + boost::asio::deadline_timer timer_; + boost::asio::streambuf buffer_; }; #endif //SERIAL_PORT_WRAPPER_HPP