GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
serial_communication_thread.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_SERIAL_COMMUNICATION_THREAD_H_
9#define DESIGNLAB_SERIAL_COMMUNICATION_THREAD_H_
10
11#include <string>
12#include <vector>
13
14#include <boost/thread.hpp>
15
16#include <Windows.h> // シリアル通信を行うために必要.
17
18
19namespace designlab
20{
21
23{
24public:
27
28 void Loop();
29
33 void SetWriteData(const std::string& str);
34
38 std::string GetTopReadData() const;
39
43 std::vector<std::string> GetAllReadData() const;
44
49 std::vector<std::string> GetReadData(const int num) const;
50
52 void EndThread()
53 {
54 boost::mutex::scoped_lock lock(mutex_);
55 end_flag_ = true;
56 }
57
58 bool IsEnd() const
59 {
60 boost::mutex::scoped_lock lock(mutex_);
61 return end_flag_;
62 }
63
64private:
65 static constexpr float kThreadPeriod = 0.01f;
66 static constexpr int kBufferSize = 1024;
67 static constexpr int kComPortNumber = 3;
68
70 bool Initialize();
71
75 bool Read();
76
80 bool Write();
81
82 HANDLE serial_handle_{ INVALID_HANDLE_VALUE };
83
84 std::vector<std::string> read_data_;
85 std::string write_data_;
86
87 bool end_flag_{ false };
88
89 mutable boost::mutex mutex_;
90};
91
92} // namespace designlab
93
94
95#endif // DESIGNLAB_SERIAL_COMMUNICATION_THREAD_H_
void SetWriteData(const std::string &str)
指定した文字列をシリアル通信で送信する. この時,排他制御を行う.
void EndThread()
シリアル通信のスレッドを終了する.
std::string GetTopReadData() const
シリアル通信で受信した最新の文字列を取得する. この時,排他制御を行う.
std::vector< std::string > GetAllReadData() const
シリアル通信で受信した文字列を全て取得する. この時,排他制御を行う.
std::vector< std::string > GetReadData(const int num) const
シリアル通信で受信した文字列の中から指定した数だけ取得する. この時,排他制御を行う.