回答編集履歴

1

例示のh/cpp追加

2022/03/11 12:29

投稿

matukeso
matukeso

スコア1677

test CHANGED
@@ -3,3 +3,98 @@
3
3
  urg_tについては、WrappingClassが持つにしても、Managed側に露出する必要がないと思いますよ。
4
4
  ref classは実体を持てないので、ポインタ(urg_t*)にして、WrappingClassのコンストラクタでnewなりmallocなりすればいいです。
5
5
 
6
+ つまりはこういう感じのコードになるんですよね。
7
+ ```C++
8
+ #pragma once
9
+ using OutAttribute = System::Runtime::InteropServices::OutAttribute;
10
+ namespace QRK
11
+ {
12
+ public enum class ConnectionType {
13
+ Serial,
14
+ Ethernet,
15
+ };
16
+ public enum class MeasurementType {
17
+ Distance,
18
+ Distance_intensity,
19
+ Multiecho,
20
+ Multiecho_intensity,
21
+ };
22
+ public ref class UrgDriver
23
+ {
24
+ public:
25
+
26
+ static const int DefaultBaudrate = 115200;
27
+ static const int DefaultPort = 10940;
28
+ static const int InfinityTimes = -1;
29
+
30
+ UrgDriver(void);
31
+ ~UrgDriver(void);
32
+
33
+ static array<System::String^>^ FindPorts(void);
34
+ static array<System::String^>^ FindPorts([Out] array<bool>^% is_urg_ports);
35
+ System::String^ What(void);
36
+
37
+ bool Open(System::String^ device_name) { return Open(device_name, Default_baudrate, ConnectionType::Serial); }
38
+ bool Open(System::String^ device_name, int baudrate) { return Open(device_name, baudrate, ConnectionType::Serial); }
39
+ bool Open(System::String^ device_name, int baudrate, ConnectionType type);
40
+ void Close(void);
41
+ ...略...
42
+ private:
43
+ void adjust_time_stamp(int% time_stamp)
44
+ {
45
+ time_stamp += time_stamp_offset_;
46
+ }
47
+ urg_t* urg_;
48
+ MeasurementType last_measure_type_;
49
+ int time_stamp_offset_;
50
+
51
+ System::String^ product_type_;
52
+ System::String^ firmware_version_;
53
+ System::String^ serial_id_;
54
+ };
55
+ ```
56
+
57
+ ```C++
58
+ #include "ticks.h"
59
+ extern "C" {
60
+ #include "urg_sensor.h"
61
+ #include "urg_utils.h"
62
+ #include "urg_serial_utils.h"
63
+ #include "urg_errno.h"
64
+ #include "urg_debug.h"
65
+ }
66
+ #include "UrgDriver.h"
67
+ #include <string>
68
+ #include <vector>
69
+ #include <msclr\marshal.h>
70
+ using namespace msclr::interop;
71
+ using namespace qrk;
72
+ namespace QRK
73
+ {
74
+ UrgDriver::UrgDriver(void)
75
+ {
76
+ urg_ = new urg_t;
77
+ urg_t_initialize(urg_);
78
+ }
79
+
80
+ array<System::String^>^ UrgDriver::FindPorts(void)
81
+ {
82
+ array<bool>^ dummy_is_urg_port;
83
+ return FindPorts(dummy_is_urg_port);
84
+ }
85
+
86
+
87
+ array<System::String^>^ UrgDriver::FindPorts(array<bool>^% is_urg_ports)
88
+ {
89
+ int n = urg_serial_find_port();
90
+ array<System::String^>^ found_ports = gcnew array<System::String^>(n);
91
+ is_urg_ports = gcnew array<bool>(n);
92
+
93
+ for (int i = 0; i < n; ++i) {
94
+ found_ports[i] = marshal_as<System::String^>(urg_serial_port_name(i));
95
+ is_urg_ports[i] = urg_serial_is_urg_port(i) ? true : false;
96
+ }
97
+ return found_ports;
98
+ }
99
+ ...略...
100
+ ```