diff --git a/src/message_handler.cpp b/src/message_handler.cpp index bf84d7f..1c59b4a 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -1,35 +1,12 @@ #include #include #include -#include #include "tcp_client.hpp" #include "tcp_service.hpp" #include "serial_port_wrapper.hpp" int main() { - // 启动甲烷数据查询串口线程,定时发送查询指令 - std::thread gas_serial_port_thread([] { - try { - boost::asio::io_service io; - //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - - // 从串口读取数据 - boost::asio::streambuf buffer; - wrapper.read_from_port(buffer); - - // TODO 暂时不考虑写数据到串口 - // steady_timer timer(io, std::chrono::seconds(3)); - // wrapper.start_periodic_send(); - - io.run(); - } catch (std::exception &e) { - std::cerr << e.what() << std::endl; - } - }); - gas_serial_port_thread.join(); - // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 std::thread client_thread([] { TcpClient client("eth0"); @@ -44,5 +21,23 @@ service.start(8888); }); service_thread.join(); + + // 启动甲烷数据查询串口线程,定时发送查询指令 + std::thread gas_serial_port_thread([] { + try { + boost::asio::io_service io; + //打开串口 + SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 + + // 从串口读取数据 + boost::asio::streambuf buffer; + wrapper.read_from_port(buffer); + + io.run(); + } catch (std::exception &e) { + std::cerr << e.what() << std::endl; + } + }); + gas_serial_port_thread.join(); return 0; } diff --git a/src/message_handler.cpp b/src/message_handler.cpp index bf84d7f..1c59b4a 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -1,35 +1,12 @@ #include #include #include -#include #include "tcp_client.hpp" #include "tcp_service.hpp" #include "serial_port_wrapper.hpp" int main() { - // 启动甲烷数据查询串口线程,定时发送查询指令 - std::thread gas_serial_port_thread([] { - try { - boost::asio::io_service io; - //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - - // 从串口读取数据 - boost::asio::streambuf buffer; - wrapper.read_from_port(buffer); - - // TODO 暂时不考虑写数据到串口 - // steady_timer timer(io, std::chrono::seconds(3)); - // wrapper.start_periodic_send(); - - io.run(); - } catch (std::exception &e) { - std::cerr << e.what() << std::endl; - } - }); - gas_serial_port_thread.join(); - // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 std::thread client_thread([] { TcpClient client("eth0"); @@ -44,5 +21,23 @@ service.start(8888); }); service_thread.join(); + + // 启动甲烷数据查询串口线程,定时发送查询指令 + std::thread gas_serial_port_thread([] { + try { + boost::asio::io_service io; + //打开串口 + SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 + + // 从串口读取数据 + boost::asio::streambuf buffer; + wrapper.read_from_port(buffer); + + io.run(); + } catch (std::exception &e) { + std::cerr << e.what() << std::endl; + } + }); + gas_serial_port_thread.join(); return 0; } diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp index 79d52e9..fbb54c5 100644 --- a/src/serial_port_wrapper.cpp +++ b/src/serial_port_wrapper.cpp @@ -6,6 +6,16 @@ #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, std::chrono::seconds(3)) { @@ -15,18 +25,10 @@ port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); } -void SerialPortWrapper::handle_data(const std::vector &buffer) { - const auto x = buffer[2] & 0xFF; - const auto y = buffer[3] & 0xFF; - const auto z = buffer[4] & 0xFF; - const auto result = x * 65536 + y * 256 + z; - std::cout << "methane concentration: " << result << " ppm" << std::endl; - //http上传数据 -} - +//CC 05 00 00 00 00 0D 77 void SerialPortWrapper::read_from_port(boost::asio::streambuf &buffer) { async_read_until( - port_, buffer, static_cast(0x96), //结束位,一定要注意匹配 + port_, buffer, 0x77, //结束位,一定要匹配 [this,&buffer](const boost::system::error_code &error, const size_t bytes_transferred) { if (!error) { // 获取接收到的数据 @@ -56,33 +58,4 @@ } } ); -} - -void SerialPortWrapper::write_to_port(std::vector command) { - 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; - } -} - -void SerialPortWrapper::start_periodic_send() { - timer_.async_wait([this](const boost::system::error_code &error) { - if (!error) { - write_to_port(query_methane_command); - timer_.expires_at(timer_.expiry() + std::chrono::seconds(3)); - start_periodic_send(); - } - }); -} +} \ No newline at end of file diff --git a/src/message_handler.cpp b/src/message_handler.cpp index bf84d7f..1c59b4a 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -1,35 +1,12 @@ #include #include #include -#include #include "tcp_client.hpp" #include "tcp_service.hpp" #include "serial_port_wrapper.hpp" int main() { - // 启动甲烷数据查询串口线程,定时发送查询指令 - std::thread gas_serial_port_thread([] { - try { - boost::asio::io_service io; - //打开串口 - SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 - - // 从串口读取数据 - boost::asio::streambuf buffer; - wrapper.read_from_port(buffer); - - // TODO 暂时不考虑写数据到串口 - // steady_timer timer(io, std::chrono::seconds(3)); - // wrapper.start_periodic_send(); - - io.run(); - } catch (std::exception &e) { - std::cerr << e.what() << std::endl; - } - }); - gas_serial_port_thread.join(); - // 启动TCP客户端,接收来自Web端的命令,控制机器狗运动 std::thread client_thread([] { TcpClient client("eth0"); @@ -44,5 +21,23 @@ service.start(8888); }); service_thread.join(); + + // 启动甲烷数据查询串口线程,定时发送查询指令 + std::thread gas_serial_port_thread([] { + try { + boost::asio::io_service io; + //打开串口 + SerialPortWrapper wrapper(io, "/dev/ttyUSB0", 115200); //甲烷串口 + + // 从串口读取数据 + boost::asio::streambuf buffer; + wrapper.read_from_port(buffer); + + io.run(); + } catch (std::exception &e) { + std::cerr << e.what() << std::endl; + } + }); + gas_serial_port_thread.join(); return 0; } diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp index 79d52e9..fbb54c5 100644 --- a/src/serial_port_wrapper.cpp +++ b/src/serial_port_wrapper.cpp @@ -6,6 +6,16 @@ #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, std::chrono::seconds(3)) { @@ -15,18 +25,10 @@ port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); } -void SerialPortWrapper::handle_data(const std::vector &buffer) { - const auto x = buffer[2] & 0xFF; - const auto y = buffer[3] & 0xFF; - const auto z = buffer[4] & 0xFF; - const auto result = x * 65536 + y * 256 + z; - std::cout << "methane concentration: " << result << " ppm" << std::endl; - //http上传数据 -} - +//CC 05 00 00 00 00 0D 77 void SerialPortWrapper::read_from_port(boost::asio::streambuf &buffer) { async_read_until( - port_, buffer, static_cast(0x96), //结束位,一定要注意匹配 + port_, buffer, 0x77, //结束位,一定要匹配 [this,&buffer](const boost::system::error_code &error, const size_t bytes_transferred) { if (!error) { // 获取接收到的数据 @@ -56,33 +58,4 @@ } } ); -} - -void SerialPortWrapper::write_to_port(std::vector command) { - 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; - } -} - -void SerialPortWrapper::start_periodic_send() { - timer_.async_wait([this](const boost::system::error_code &error) { - if (!error) { - write_to_port(query_methane_command); - timer_.expires_at(timer_.expiry() + std::chrono::seconds(3)); - start_periodic_send(); - } - }); -} +} \ No newline at end of file diff --git a/src/serial_port_wrapper.hpp b/src/serial_port_wrapper.hpp index db9dbef..04591db 100644 --- a/src/serial_port_wrapper.hpp +++ b/src/serial_port_wrapper.hpp @@ -19,18 +19,10 @@ void read_from_port(boost::asio::streambuf &buffer); - void write_to_port(std::vector command); - - void start_periodic_send(); - private: - // 查询甲烷的指令 - std::vector query_methane_command = {0xAA, 0x01, 0x00, 0x95, 0x00, 0x00, 0x96}; boost::asio::io_service &io_service_; boost::asio::serial_port port_; steady_timer timer_; - - void handle_data(const std::vector &buffer); }; #endif //SERIAL_PORT_WRAPPER_HPP