diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp new file mode 100644 index 0000000..a55c912 --- /dev/null +++ b/src/methane_serial_port.cpp @@ -0,0 +1,71 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "methane_serial_port.hpp" +#include +#include + +void handle_data(const std::vector &buffer) { + const auto x = buffer[2] & 0xFFFF; + const auto y = buffer[3] & 0xFFF; + const auto z = buffer[4] & 0xFF; + const auto w = buffer[5]; + const auto result = x + y + z + w; + std::cout << "methane concentration: " << result << " ppm·m" << std::endl; + //http上传数据 +} + +MethaneSerialPort::MethaneSerialPort( + 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) { + 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)); + + start_timer(); +} + +void MethaneSerialPort::start_timer() { + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } + }); +} + +//CC 05 00 00 00 00 0D 77 +void MethaneSerialPort::read_from_port() { + async_read_until( + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { + if (!error) { + // 获取接收到的数据 + std::istream is(&buffer_); + std::vector received_data(static_cast(bytes_transferred)); + is.read( + reinterpret_cast(received_data.data()), static_cast(bytes_transferred) + ); + + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; + + handle_data(received_data); + + start_timer(); + } else { + std::cerr << "error: " << error.message() << std::endl; + start_timer(); + } + }); +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp new file mode 100644 index 0000000..a55c912 --- /dev/null +++ b/src/methane_serial_port.cpp @@ -0,0 +1,71 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "methane_serial_port.hpp" +#include +#include + +void handle_data(const std::vector &buffer) { + const auto x = buffer[2] & 0xFFFF; + const auto y = buffer[3] & 0xFFF; + const auto z = buffer[4] & 0xFF; + const auto w = buffer[5]; + const auto result = x + y + z + w; + std::cout << "methane concentration: " << result << " ppm·m" << std::endl; + //http上传数据 +} + +MethaneSerialPort::MethaneSerialPort( + 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) { + 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)); + + start_timer(); +} + +void MethaneSerialPort::start_timer() { + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } + }); +} + +//CC 05 00 00 00 00 0D 77 +void MethaneSerialPort::read_from_port() { + async_read_until( + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { + if (!error) { + // 获取接收到的数据 + std::istream is(&buffer_); + std::vector received_data(static_cast(bytes_transferred)); + is.read( + reinterpret_cast(received_data.data()), static_cast(bytes_transferred) + ); + + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; + + handle_data(received_data); + + start_timer(); + } else { + std::cerr << "error: " << error.message() << std::endl; + start_timer(); + } + }); +} \ No newline at end of file diff --git a/src/methane_serial_port.hpp b/src/methane_serial_port.hpp new file mode 100644 index 0000000..2d7a761 --- /dev/null +++ b/src/methane_serial_port.hpp @@ -0,0 +1,33 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef METHANE_SERIAL_PORT_HPP +#define METHANE_SERIAL_PORT_HPP + +#include +#include + +using boost::asio::serial_port_base; +using boost::asio::steady_timer; + +class MethaneSerialPort { +public: + explicit MethaneSerialPort(boost::asio::io_service &io_service, const std::string &port_name, int baud_rate); + + boost::asio::serial_port &get() { return port_; } + + void read_from_port(); + +private: + boost::asio::io_service &io_service_; + boost::asio::serial_port port_; + boost::asio::deadline_timer timer_; + boost::asio::streambuf buffer_; + + void start_timer(); +}; + + + +#endif //METHANE_SERIAL_PORT_HPP diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp new file mode 100644 index 0000000..a55c912 --- /dev/null +++ b/src/methane_serial_port.cpp @@ -0,0 +1,71 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "methane_serial_port.hpp" +#include +#include + +void handle_data(const std::vector &buffer) { + const auto x = buffer[2] & 0xFFFF; + const auto y = buffer[3] & 0xFFF; + const auto z = buffer[4] & 0xFF; + const auto w = buffer[5]; + const auto result = x + y + z + w; + std::cout << "methane concentration: " << result << " ppm·m" << std::endl; + //http上传数据 +} + +MethaneSerialPort::MethaneSerialPort( + 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) { + 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)); + + start_timer(); +} + +void MethaneSerialPort::start_timer() { + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } + }); +} + +//CC 05 00 00 00 00 0D 77 +void MethaneSerialPort::read_from_port() { + async_read_until( + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { + if (!error) { + // 获取接收到的数据 + std::istream is(&buffer_); + std::vector received_data(static_cast(bytes_transferred)); + is.read( + reinterpret_cast(received_data.data()), static_cast(bytes_transferred) + ); + + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; + + handle_data(received_data); + + start_timer(); + } else { + std::cerr << "error: " << error.message() << std::endl; + start_timer(); + } + }); +} \ No newline at end of file diff --git a/src/methane_serial_port.hpp b/src/methane_serial_port.hpp new file mode 100644 index 0000000..2d7a761 --- /dev/null +++ b/src/methane_serial_port.hpp @@ -0,0 +1,33 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef METHANE_SERIAL_PORT_HPP +#define METHANE_SERIAL_PORT_HPP + +#include +#include + +using boost::asio::serial_port_base; +using boost::asio::steady_timer; + +class MethaneSerialPort { +public: + explicit MethaneSerialPort(boost::asio::io_service &io_service, const std::string &port_name, int baud_rate); + + boost::asio::serial_port &get() { return port_; } + + void read_from_port(); + +private: + boost::asio::io_service &io_service_; + boost::asio::serial_port port_; + boost::asio::deadline_timer timer_; + boost::asio::streambuf buffer_; + + void start_timer(); +}; + + + +#endif //METHANE_SERIAL_PORT_HPP diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp new file mode 100644 index 0000000..ced7857 --- /dev/null +++ b/src/robotic_arm_serial_port.cpp @@ -0,0 +1,5 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "robotic_arm_serial_port.hpp" diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp new file mode 100644 index 0000000..a55c912 --- /dev/null +++ b/src/methane_serial_port.cpp @@ -0,0 +1,71 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "methane_serial_port.hpp" +#include +#include + +void handle_data(const std::vector &buffer) { + const auto x = buffer[2] & 0xFFFF; + const auto y = buffer[3] & 0xFFF; + const auto z = buffer[4] & 0xFF; + const auto w = buffer[5]; + const auto result = x + y + z + w; + std::cout << "methane concentration: " << result << " ppm·m" << std::endl; + //http上传数据 +} + +MethaneSerialPort::MethaneSerialPort( + 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) { + 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)); + + start_timer(); +} + +void MethaneSerialPort::start_timer() { + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } + }); +} + +//CC 05 00 00 00 00 0D 77 +void MethaneSerialPort::read_from_port() { + async_read_until( + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { + if (!error) { + // 获取接收到的数据 + std::istream is(&buffer_); + std::vector received_data(static_cast(bytes_transferred)); + is.read( + reinterpret_cast(received_data.data()), static_cast(bytes_transferred) + ); + + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; + + handle_data(received_data); + + start_timer(); + } else { + std::cerr << "error: " << error.message() << std::endl; + start_timer(); + } + }); +} \ No newline at end of file diff --git a/src/methane_serial_port.hpp b/src/methane_serial_port.hpp new file mode 100644 index 0000000..2d7a761 --- /dev/null +++ b/src/methane_serial_port.hpp @@ -0,0 +1,33 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef METHANE_SERIAL_PORT_HPP +#define METHANE_SERIAL_PORT_HPP + +#include +#include + +using boost::asio::serial_port_base; +using boost::asio::steady_timer; + +class MethaneSerialPort { +public: + explicit MethaneSerialPort(boost::asio::io_service &io_service, const std::string &port_name, int baud_rate); + + boost::asio::serial_port &get() { return port_; } + + void read_from_port(); + +private: + boost::asio::io_service &io_service_; + boost::asio::serial_port port_; + boost::asio::deadline_timer timer_; + boost::asio::streambuf buffer_; + + void start_timer(); +}; + + + +#endif //METHANE_SERIAL_PORT_HPP diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp new file mode 100644 index 0000000..ced7857 --- /dev/null +++ b/src/robotic_arm_serial_port.cpp @@ -0,0 +1,5 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "robotic_arm_serial_port.hpp" diff --git a/src/robotic_arm_serial_port.hpp b/src/robotic_arm_serial_port.hpp new file mode 100644 index 0000000..18532f8 --- /dev/null +++ b/src/robotic_arm_serial_port.hpp @@ -0,0 +1,13 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef ROBOTIC_ARM_SERIAL_PORT_HPP +#define ROBOTIC_ARM_SERIAL_PORT_HPP + +class RoboticArmSerialPort { + +}; + + +#endif //ROBOTIC_ARM_SERIAL_PORT_HPP diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp new file mode 100644 index 0000000..a55c912 --- /dev/null +++ b/src/methane_serial_port.cpp @@ -0,0 +1,71 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "methane_serial_port.hpp" +#include +#include + +void handle_data(const std::vector &buffer) { + const auto x = buffer[2] & 0xFFFF; + const auto y = buffer[3] & 0xFFF; + const auto z = buffer[4] & 0xFF; + const auto w = buffer[5]; + const auto result = x + y + z + w; + std::cout << "methane concentration: " << result << " ppm·m" << std::endl; + //http上传数据 +} + +MethaneSerialPort::MethaneSerialPort( + 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) { + 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)); + + start_timer(); +} + +void MethaneSerialPort::start_timer() { + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } + }); +} + +//CC 05 00 00 00 00 0D 77 +void MethaneSerialPort::read_from_port() { + async_read_until( + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { + if (!error) { + // 获取接收到的数据 + std::istream is(&buffer_); + std::vector received_data(static_cast(bytes_transferred)); + is.read( + reinterpret_cast(received_data.data()), static_cast(bytes_transferred) + ); + + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; + + handle_data(received_data); + + start_timer(); + } else { + std::cerr << "error: " << error.message() << std::endl; + start_timer(); + } + }); +} \ No newline at end of file diff --git a/src/methane_serial_port.hpp b/src/methane_serial_port.hpp new file mode 100644 index 0000000..2d7a761 --- /dev/null +++ b/src/methane_serial_port.hpp @@ -0,0 +1,33 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef METHANE_SERIAL_PORT_HPP +#define METHANE_SERIAL_PORT_HPP + +#include +#include + +using boost::asio::serial_port_base; +using boost::asio::steady_timer; + +class MethaneSerialPort { +public: + explicit MethaneSerialPort(boost::asio::io_service &io_service, const std::string &port_name, int baud_rate); + + boost::asio::serial_port &get() { return port_; } + + void read_from_port(); + +private: + boost::asio::io_service &io_service_; + boost::asio::serial_port port_; + boost::asio::deadline_timer timer_; + boost::asio::streambuf buffer_; + + void start_timer(); +}; + + + +#endif //METHANE_SERIAL_PORT_HPP diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp new file mode 100644 index 0000000..ced7857 --- /dev/null +++ b/src/robotic_arm_serial_port.cpp @@ -0,0 +1,5 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "robotic_arm_serial_port.hpp" diff --git a/src/robotic_arm_serial_port.hpp b/src/robotic_arm_serial_port.hpp new file mode 100644 index 0000000..18532f8 --- /dev/null +++ b/src/robotic_arm_serial_port.hpp @@ -0,0 +1,13 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef ROBOTIC_ARM_SERIAL_PORT_HPP +#define ROBOTIC_ARM_SERIAL_PORT_HPP + +class RoboticArmSerialPort { + +}; + + +#endif //ROBOTIC_ARM_SERIAL_PORT_HPP diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp deleted file mode 100644 index 2ce65d6..0000000 --- a/src/serial_port_wrapper.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// -// Created by casic on 25-2-25. -// - -#include "serial_port_wrapper.hpp" -#include -#include - -void handle_data(const std::vector &buffer) { - const auto x = buffer[2] & 0xFFFF; - const auto y = buffer[3] & 0xFFF; - const auto z = buffer[4] & 0xFF; - const auto w = buffer[5]; - const auto result = x + y + z + w; - std::cout << "methane concentration: " << result << " ppm·m" << std::endl; - //http上传数据 -} - -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) { - 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)); - - start_timer(); -} - -void SerialPortWrapper::start_timer() { - timer_.expires_from_now(boost::posix_time::seconds(3)); - timer_.async_wait([this](const boost::system::error_code &error) { - if (!error) { - read_from_port(); - } - }); -} - -//CC 05 00 00 00 00 0D 77 -void SerialPortWrapper::read_from_port() { - async_read_until( - port_, buffer_, 0x77, - [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { - if (!error) { - // 获取接收到的数据 - std::istream is(&buffer_); - std::vector received_data(static_cast(bytes_transferred)); - is.read( - reinterpret_cast(received_data.data()), static_cast(bytes_transferred) - ); - - // std::cout << "received " << bytes_transferred << " bytes: "; - // for (size_t i = 0; i < bytes_transferred; ++i) { - // std::cout << std::hex - // << std::uppercase - // << std::setw(2) - // << std::setfill('0') - // << static_cast(received_data[i]) - // << " "; - // } - // std::cout << std::endl; - - handle_data(received_data); - - start_timer(); - } else { - std::cerr << "error: " << error.message() << std::endl; - start_timer(); - } - }); -} diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ca6761..eb70a26 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ add_executable(message_handler src/message_handler.cpp - src/serial_port_wrapper.cpp src/slam_wrapper.cpp src/tcp_client.cpp src/tcp_service.cpp + src/methane_serial_port.cpp + src/robotic_arm_serial_port.cpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 5a23a75..e2f6f47 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -3,7 +3,7 @@ #include #include "tcp_client.hpp" -#include "serial_port_wrapper.hpp" +#include "methane_serial_port.hpp" int main() { // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 @@ -20,8 +20,8 @@ try { boost::asio::io_service io; //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - wrapper.read_from_port(); + MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp new file mode 100644 index 0000000..a55c912 --- /dev/null +++ b/src/methane_serial_port.cpp @@ -0,0 +1,71 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "methane_serial_port.hpp" +#include +#include + +void handle_data(const std::vector &buffer) { + const auto x = buffer[2] & 0xFFFF; + const auto y = buffer[3] & 0xFFF; + const auto z = buffer[4] & 0xFF; + const auto w = buffer[5]; + const auto result = x + y + z + w; + std::cout << "methane concentration: " << result << " ppm·m" << std::endl; + //http上传数据 +} + +MethaneSerialPort::MethaneSerialPort( + 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) { + 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)); + + start_timer(); +} + +void MethaneSerialPort::start_timer() { + timer_.expires_from_now(boost::posix_time::seconds(3)); + timer_.async_wait([this](const boost::system::error_code &error) { + if (!error) { + read_from_port(); + } + }); +} + +//CC 05 00 00 00 00 0D 77 +void MethaneSerialPort::read_from_port() { + async_read_until( + port_, buffer_, 0x77, + [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { + if (!error) { + // 获取接收到的数据 + std::istream is(&buffer_); + std::vector received_data(static_cast(bytes_transferred)); + is.read( + reinterpret_cast(received_data.data()), static_cast(bytes_transferred) + ); + + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; + + handle_data(received_data); + + start_timer(); + } else { + std::cerr << "error: " << error.message() << std::endl; + start_timer(); + } + }); +} \ No newline at end of file diff --git a/src/methane_serial_port.hpp b/src/methane_serial_port.hpp new file mode 100644 index 0000000..2d7a761 --- /dev/null +++ b/src/methane_serial_port.hpp @@ -0,0 +1,33 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef METHANE_SERIAL_PORT_HPP +#define METHANE_SERIAL_PORT_HPP + +#include +#include + +using boost::asio::serial_port_base; +using boost::asio::steady_timer; + +class MethaneSerialPort { +public: + explicit MethaneSerialPort(boost::asio::io_service &io_service, const std::string &port_name, int baud_rate); + + boost::asio::serial_port &get() { return port_; } + + void read_from_port(); + +private: + boost::asio::io_service &io_service_; + boost::asio::serial_port port_; + boost::asio::deadline_timer timer_; + boost::asio::streambuf buffer_; + + void start_timer(); +}; + + + +#endif //METHANE_SERIAL_PORT_HPP diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp new file mode 100644 index 0000000..ced7857 --- /dev/null +++ b/src/robotic_arm_serial_port.cpp @@ -0,0 +1,5 @@ +// +// Created by pengx on 2025/3/18. +// + +#include "robotic_arm_serial_port.hpp" diff --git a/src/robotic_arm_serial_port.hpp b/src/robotic_arm_serial_port.hpp new file mode 100644 index 0000000..18532f8 --- /dev/null +++ b/src/robotic_arm_serial_port.hpp @@ -0,0 +1,13 @@ +// +// Created by pengx on 2025/3/18. +// + +#ifndef ROBOTIC_ARM_SERIAL_PORT_HPP +#define ROBOTIC_ARM_SERIAL_PORT_HPP + +class RoboticArmSerialPort { + +}; + + +#endif //ROBOTIC_ARM_SERIAL_PORT_HPP diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp deleted file mode 100644 index 2ce65d6..0000000 --- a/src/serial_port_wrapper.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// -// Created by casic on 25-2-25. -// - -#include "serial_port_wrapper.hpp" -#include -#include - -void handle_data(const std::vector &buffer) { - const auto x = buffer[2] & 0xFFFF; - const auto y = buffer[3] & 0xFFF; - const auto z = buffer[4] & 0xFF; - const auto w = buffer[5]; - const auto result = x + y + z + w; - std::cout << "methane concentration: " << result << " ppm·m" << std::endl; - //http上传数据 -} - -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) { - 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)); - - start_timer(); -} - -void SerialPortWrapper::start_timer() { - timer_.expires_from_now(boost::posix_time::seconds(3)); - timer_.async_wait([this](const boost::system::error_code &error) { - if (!error) { - read_from_port(); - } - }); -} - -//CC 05 00 00 00 00 0D 77 -void SerialPortWrapper::read_from_port() { - async_read_until( - port_, buffer_, 0x77, - [this](const boost::system::error_code &error, const size_t bytes_transferred) mutable { - if (!error) { - // 获取接收到的数据 - std::istream is(&buffer_); - std::vector received_data(static_cast(bytes_transferred)); - is.read( - reinterpret_cast(received_data.data()), static_cast(bytes_transferred) - ); - - // std::cout << "received " << bytes_transferred << " bytes: "; - // for (size_t i = 0; i < bytes_transferred; ++i) { - // std::cout << std::hex - // << std::uppercase - // << std::setw(2) - // << std::setfill('0') - // << static_cast(received_data[i]) - // << " "; - // } - // std::cout << std::endl; - - handle_data(received_data); - - start_timer(); - } else { - std::cerr << "error: " << error.message() << std::endl; - start_timer(); - } - }); -} diff --git a/src/serial_port_wrapper.hpp b/src/serial_port_wrapper.hpp deleted file mode 100644 index cadcd45..0000000 --- a/src/serial_port_wrapper.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// Created by casic on 25-2-25. -// - -#ifndef SERIAL_PORT_WRAPPER_HPP -#define SERIAL_PORT_WRAPPER_HPP - -#include -#include - -using boost::asio::serial_port_base; -using boost::asio::steady_timer; - -class SerialPortWrapper { -public: - explicit SerialPortWrapper(boost::asio::io_service &io_service, const std::string &port_name, int baud_rate); - - boost::asio::serial_port &get() { return port_; } - - void read_from_port(); - -private: - boost::asio::io_service &io_service_; - boost::asio::serial_port port_; - boost::asio::deadline_timer timer_; - boost::asio::streambuf buffer_; - - void start_timer(); -}; - -#endif //SERIAL_PORT_WRAPPER_HPP