diff --git a/src/message_handler.cpp b/src/message_handler.cpp index e479c49..5a23a75 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -10,6 +10,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; + // client.connect("111.198.10.15", 11647); client.connect("192.168.123.18", 9001); io.run(); }); diff --git a/src/message_handler.cpp b/src/message_handler.cpp index e479c49..5a23a75 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -10,6 +10,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; + // client.connect("111.198.10.15", 11647); client.connect("192.168.123.18", 9001); io.run(); }); diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index a195f30..46e3469 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -153,6 +153,10 @@ std::cout << "TcpClient::connected to " << server_ip << ":" << server_port << ", waiting for command ..." << std::endl; + //连接成功后把自己的编号发送给Tcp Service + // const std::string dog_code = "B42D4000OCAD3N82"; + // send(_client_socket, dog_code.c_str(), dog_code.size(), 0); + // 启动接收数据的线程 std::thread receive_thread(&TcpClient::receive_data, this); diff --git a/src/message_handler.cpp b/src/message_handler.cpp index e479c49..5a23a75 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -10,6 +10,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; + // client.connect("111.198.10.15", 11647); client.connect("192.168.123.18", 9001); io.run(); }); diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index a195f30..46e3469 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -153,6 +153,10 @@ std::cout << "TcpClient::connected to " << server_ip << ":" << server_port << ", waiting for command ..." << std::endl; + //连接成功后把自己的编号发送给Tcp Service + // const std::string dog_code = "B42D4000OCAD3N82"; + // send(_client_socket, dog_code.c_str(), dog_code.size(), 0); + // 启动接收数据的线程 std::thread receive_thread(&TcpClient::receive_data, this); diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index 6578f68..3f33706 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -14,18 +14,23 @@ #include #include +/** + * @link SlamWrapper---> @link TcpService + * @param command + */ void TcpService::on_message_received(const std::vector &command) { boost::asio::write(_port, boost::asio::buffer(command)); } +/** + * 回消息给已连接的TCP客户端。需要拼接数据包。(机器狗当前位置node,经纬度,浓度) + * @param node + */ void TcpService::on_node_arrived(const int &node) { - //回消息给已连接的TCP客户端 const std::string message = std::to_string(node); std::cout << "send message to client: " << message << std::endl; for (const int client_socket: _client_sockets) { - if (send(client_socket, message.c_str(), message.size(), 0) == -1) { - std::cerr << "Failed to send message to client: " << strerror(errno) << std::endl; - } + send(client_socket, message.c_str(), message.size(), 0); } } @@ -62,22 +67,7 @@ //转为std::vector并发给机械臂串口 std::vector command(data.begin(), data.end()); - const size_t bytes_to_send = command.size(); - if (bytes_to_send == 0) { - std::cout << "Empty command, nothing to send." << std::endl; - return; - } - - try { - const size_t bytes_sent = boost::asio::write(_port, boost::asio::buffer(command)); - if (bytes_sent == bytes_to_send) { - std::cout << "sent successfully" << std::endl; - } else { - std::cerr << "only " << bytes_sent << " bytes sent out of " << bytes_to_send << std::endl; - } - } catch (const boost::system::system_error &e) { - std::cerr << "error sending data: " << e.what() << std::endl; - } + boost::asio::write(_port, boost::asio::buffer(command)); } void TcpService::handle_client(const int client_socket) {