@@ -8,22 +8,19 @@ namespace infinite_sense {
88UsbManager::UsbManager (std::string port, int baud_rate)
99 : port_(std::move(port)), started_(false ) {
1010 serial_ptr_ = std::make_unique<serial::Serial>();
11-
1211 try {
1312 serial_ptr_->setPort (port_);
1413 serial_ptr_->setBaudrate (baud_rate);
1514 serial::Timeout to = serial::Timeout::simpleTimeout (1000 );
1615 serial_ptr_->setTimeout (to);
1716 serial_ptr_->open ();
18-
1917 if (serial_ptr_->isOpen ()) {
2018 serial_ptr_->flush ();
2119 LOG (INFO) << " Serial port " << port_ << " opened successfully." ;
2220 } else {
2321 LOG (ERROR) << " Failed to open serial port: " << port_;
2422 return ;
2523 }
26-
2724 ptp_ = std::make_unique<Ptp>();
2825 ptp_->SetUsbPtr (serial_ptr_);
2926 } catch (const serial::IOException& e) {
@@ -43,55 +40,53 @@ void UsbManager::Start() {
4340 LOG (ERROR) << " Cannot start USB manager: serial port not open." ;
4441 return ;
4542 }
46-
4743 started_ = true ;
4844 rx_thread_ = std::thread (&UsbManager::Receive, this );
4945 tx_thread_ = std::thread (&UsbManager::TimeStampSynchronization, this );
50-
5146 LOG (INFO) << " USB manager started" ;
5247}
5348
5449void UsbManager::Stop () {
55- if (!started_) return ;
56-
50+ if (!started_) {
51+ return ;
52+ }
5753 started_ = false ;
58-
59- if (rx_thread_.joinable ()) rx_thread_.join ();
60- if (tx_thread_.joinable ()) tx_thread_.join ();
61-
54+ if (rx_thread_.joinable ()) {
55+ rx_thread_.join ();
56+ }
57+ if (tx_thread_.joinable ()) {
58+ tx_thread_.join ();
59+ }
6260 if (serial_ptr_ && serial_ptr_->isOpen ()) {
6361 serial_ptr_->close ();
6462 LOG (INFO) << " Serial port " << port_ << " closed." ;
6563 }
66-
6764 LOG (INFO) << " USB manager stopped" ;
6865}
6966
7067void UsbManager::Receive () const {
7168 while (started_) {
7269 try {
73- if (!serial_ptr_ || !serial_ptr_->isOpen ()) break ;
74-
70+ if (!serial_ptr_ || !serial_ptr_->isOpen ()) {
71+ break ;
72+ }
7573 if (serial_ptr_->available ()) {
7674 const std::string serial_recv = serial_ptr_->readline ();
77-
78- if (serial_recv. empty ()) continue ;
79-
75+ if (serial_recv. empty ()) {
76+ continue ;
77+ }
8078 auto json_data = nlohmann::json::parse (serial_recv, nullptr , false );
8179 if (json_data.is_discarded ()) {
8280 LOG (WARNING) << " Received malformed JSON: " << serial_recv;
8381 continue ;
8482 }
85-
8683 ptp_->ReceivePtpData (json_data);
8784 ProcessTriggerData (json_data);
8885 ProcessIMUData (json_data);
8986 ProcessGPSData (json_data);
9087 ProcessLOGData (json_data);
9188 }
92-
9389 std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
94-
9590 } catch (const std::exception& e) {
9691 LOG (ERROR) << " Receive thread exception: " << e.what ();
9792 }
0 commit comments