diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 8747942..bbbbcaf 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -9,18 +9,18 @@ int main() { // 创建一个共享的 io 对象 boost::asio::io_service io; + auto service = new TcpService(io, "/dev/ttyACM0", 115200); //机械臂串口 // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([&io] { - TcpClient client("eth0"); + std::thread client_thread([&io,&service] { + TcpClient client("eth0", service); 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); + std::thread service_thread([&io,&service] { + service->start(8888); io.run(); }); diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 8747942..bbbbcaf 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -9,18 +9,18 @@ int main() { // 创建一个共享的 io 对象 boost::asio::io_service io; + auto service = new TcpService(io, "/dev/ttyACM0", 115200); //机械臂串口 // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([&io] { - TcpClient client("eth0"); + std::thread client_thread([&io,&service] { + TcpClient client("eth0", service); 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); + std::thread service_thread([&io,&service] { + service->start(8888); io.run(); }); diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index ac6f3c5..584655c 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -9,7 +9,8 @@ #include #include -TcpClient::TcpClient(const char *networkInterface): _slam(networkInterface) { +TcpClient::TcpClient(const char *networkInterface, TcpService *tcp_service): _slam(networkInterface), + _tcp_service(tcp_service) { std::cout << "TcpClient::init success" << std::endl; } diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 8747942..bbbbcaf 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -9,18 +9,18 @@ int main() { // 创建一个共享的 io 对象 boost::asio::io_service io; + auto service = new TcpService(io, "/dev/ttyACM0", 115200); //机械臂串口 // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([&io] { - TcpClient client("eth0"); + std::thread client_thread([&io,&service] { + TcpClient client("eth0", service); 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); + std::thread service_thread([&io,&service] { + service->start(8888); io.run(); }); diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index ac6f3c5..584655c 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -9,7 +9,8 @@ #include #include -TcpClient::TcpClient(const char *networkInterface): _slam(networkInterface) { +TcpClient::TcpClient(const char *networkInterface, TcpService *tcp_service): _slam(networkInterface), + _tcp_service(tcp_service) { std::cout << "TcpClient::init success" << std::endl; } diff --git a/src/tcp_client.hpp b/src/tcp_client.hpp index 92b47e9..8f5bdb8 100644 --- a/src/tcp_client.hpp +++ b/src/tcp_client.hpp @@ -11,10 +11,11 @@ #include #include #include "slam_wrapper.hpp" +#include "tcp_service.hpp" class TcpClient { public: - explicit TcpClient(const char *networkInterface); + explicit TcpClient(const char *networkInterface, TcpService *tcp_service); ~TcpClient() { if (_client_socket != -1) { @@ -33,6 +34,7 @@ int _max_retries = 12; int _retry_interval = 5000; //重连间隔 SlamWrapper _slam; + TcpService *_tcp_service; void receive_data(); diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 8747942..bbbbcaf 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -9,18 +9,18 @@ int main() { // 创建一个共享的 io 对象 boost::asio::io_service io; + auto service = new TcpService(io, "/dev/ttyACM0", 115200); //机械臂串口 // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([&io] { - TcpClient client("eth0"); + std::thread client_thread([&io,&service] { + TcpClient client("eth0", service); 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); + std::thread service_thread([&io,&service] { + service->start(8888); io.run(); }); diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index ac6f3c5..584655c 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -9,7 +9,8 @@ #include #include -TcpClient::TcpClient(const char *networkInterface): _slam(networkInterface) { +TcpClient::TcpClient(const char *networkInterface, TcpService *tcp_service): _slam(networkInterface), + _tcp_service(tcp_service) { std::cout << "TcpClient::init success" << std::endl; } diff --git a/src/tcp_client.hpp b/src/tcp_client.hpp index 92b47e9..8f5bdb8 100644 --- a/src/tcp_client.hpp +++ b/src/tcp_client.hpp @@ -11,10 +11,11 @@ #include #include #include "slam_wrapper.hpp" +#include "tcp_service.hpp" class TcpClient { public: - explicit TcpClient(const char *networkInterface); + explicit TcpClient(const char *networkInterface, TcpService *tcp_service); ~TcpClient() { if (_client_socket != -1) { @@ -33,6 +34,7 @@ int _max_retries = 12; int _retry_interval = 5000; //重连间隔 SlamWrapper _slam; + TcpService *_tcp_service; void receive_data(); diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index c7d2876..b874a23 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -14,6 +14,10 @@ #include #include +void TcpService::send_command(const std::vector &command) { + boost::asio::write(_port, boost::asio::buffer(command)); +} + void TcpService::init_serial_port(const std::string &port_name, const int baud_rate) { _port.open(port_name); if (_port.is_open()) { diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 8747942..bbbbcaf 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -9,18 +9,18 @@ int main() { // 创建一个共享的 io 对象 boost::asio::io_service io; + auto service = new TcpService(io, "/dev/ttyACM0", 115200); //机械臂串口 // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 - std::thread client_thread([&io] { - TcpClient client("eth0"); + std::thread client_thread([&io,&service] { + TcpClient client("eth0", service); 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); + std::thread service_thread([&io,&service] { + service->start(8888); io.run(); }); diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index ac6f3c5..584655c 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -9,7 +9,8 @@ #include #include -TcpClient::TcpClient(const char *networkInterface): _slam(networkInterface) { +TcpClient::TcpClient(const char *networkInterface, TcpService *tcp_service): _slam(networkInterface), + _tcp_service(tcp_service) { std::cout << "TcpClient::init success" << std::endl; } diff --git a/src/tcp_client.hpp b/src/tcp_client.hpp index 92b47e9..8f5bdb8 100644 --- a/src/tcp_client.hpp +++ b/src/tcp_client.hpp @@ -11,10 +11,11 @@ #include #include #include "slam_wrapper.hpp" +#include "tcp_service.hpp" class TcpClient { public: - explicit TcpClient(const char *networkInterface); + explicit TcpClient(const char *networkInterface, TcpService *tcp_service); ~TcpClient() { if (_client_socket != -1) { @@ -33,6 +34,7 @@ int _max_retries = 12; int _retry_interval = 5000; //重连间隔 SlamWrapper _slam; + TcpService *_tcp_service; void receive_data(); diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index c7d2876..b874a23 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -14,6 +14,10 @@ #include #include +void TcpService::send_command(const std::vector &command) { + boost::asio::write(_port, boost::asio::buffer(command)); +} + void TcpService::init_serial_port(const std::string &port_name, const int baud_rate) { _port.open(port_name); if (_port.is_open()) { diff --git a/src/tcp_service.hpp b/src/tcp_service.hpp index 999a2f7..ec5037e 100644 --- a/src/tcp_service.hpp +++ b/src/tcp_service.hpp @@ -18,6 +18,8 @@ void init_serial_port(const std::string &port_name, int baud_rate); + void send_command(const std::vector &command); + private: int _socket_fd; boost::asio::io_context &_context;