-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlabview_integration_example.cpp
More file actions
242 lines (197 loc) · 9.59 KB
/
labview_integration_example.cpp
File metadata and controls
242 lines (197 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* LabVIEW Direct Variant/Cluster Integration Example
* Demonstrates direct conversion from LabVIEW data to CFG++ files
*/
#include "../cfgpp_parser.h"
#include <iostream>
#include <vector>
#include <cstring>
// Simulate LabVIEW data structures for demonstration
struct MockLabViewVariant {
LabViewDataHeader header;
uint8_t data[256];
};
struct MockLabViewCluster {
LabViewDataHeader header;
uint8_t data[1024];
};
// Helper function to create a mock LabVIEW string variant
MockLabViewVariant create_string_variant(const std::string& str) {
MockLabViewVariant variant = {};
variant.header.type_code = LABVIEW_TYPE_STRING;
variant.header.flags = 0;
variant.header.data_size = 4 + str.length(); // 4 bytes for length prefix
variant.header.dimensions = 0;
// Write string length (LabVIEW format)
*reinterpret_cast<uint32_t*>(variant.data) = str.length();
// Write string data
memcpy(variant.data + 4, str.c_str(), str.length());
return variant;
}
// Helper function to create a mock LabVIEW double variant
MockLabViewVariant create_double_variant(double value) {
MockLabViewVariant variant = {};
variant.header.type_code = LABVIEW_TYPE_DBL;
variant.header.flags = 0;
variant.header.data_size = sizeof(double);
variant.header.dimensions = 0;
*reinterpret_cast<double*>(variant.data) = value;
return variant;
}
// Helper function to create a mock LabVIEW integer variant
MockLabViewVariant create_integer_variant(int32_t value) {
MockLabViewVariant variant = {};
variant.header.type_code = LABVIEW_TYPE_I32;
variant.header.flags = 0;
variant.header.data_size = sizeof(int32_t);
variant.header.dimensions = 0;
*reinterpret_cast<int32_t*>(variant.data) = value;
return variant;
}
// Helper function to create a mock LabVIEW boolean variant
MockLabViewVariant create_boolean_variant(bool value) {
MockLabViewVariant variant = {};
variant.header.type_code = LABVIEW_TYPE_BOOLEAN;
variant.header.flags = 0;
variant.header.data_size = sizeof(uint8_t);
variant.header.dimensions = 0;
variant.data[0] = value ? 1 : 0;
return variant;
}
// Helper function to create a mock LabVIEW cluster
MockLabViewCluster create_measurement_cluster() {
MockLabViewCluster cluster = {};
cluster.header.type_code = LABVIEW_TYPE_CLUSTER;
cluster.header.flags = 0;
cluster.header.dimensions = 0;
size_t offset = 0;
// Field 1: Sample Rate (I32)
LabViewDataHeader* field1_header = reinterpret_cast<LabViewDataHeader*>(cluster.data + offset);
field1_header->type_code = LABVIEW_TYPE_I32;
field1_header->data_size = sizeof(int32_t);
offset += sizeof(LabViewDataHeader);
*reinterpret_cast<int32_t*>(cluster.data + offset) = 1000;
offset += sizeof(int32_t);
// Field 2: Voltage Threshold (DBL)
LabViewDataHeader* field2_header = reinterpret_cast<LabViewDataHeader*>(cluster.data + offset);
field2_header->type_code = LABVIEW_TYPE_DBL;
field2_header->data_size = sizeof(double);
offset += sizeof(LabViewDataHeader);
*reinterpret_cast<double*>(cluster.data + offset) = 3.14159;
offset += sizeof(double);
// Field 3: Enable Logging (BOOLEAN)
LabViewDataHeader* field3_header = reinterpret_cast<LabViewDataHeader*>(cluster.data + offset);
field3_header->type_code = LABVIEW_TYPE_BOOLEAN;
field3_header->data_size = sizeof(uint8_t);
offset += sizeof(LabViewDataHeader);
cluster.data[offset] = 1; // true
offset += sizeof(uint8_t);
// Field 4: Device Name (STRING)
LabViewDataHeader* field4_header = reinterpret_cast<LabViewDataHeader*>(cluster.data + offset);
field4_header->type_code = LABVIEW_TYPE_STRING;
std::string device_name = "NI-DAQmx Device";
field4_header->data_size = 4 + device_name.length();
offset += sizeof(LabViewDataHeader);
*reinterpret_cast<uint32_t*>(cluster.data + offset) = device_name.length();
offset += 4;
memcpy(cluster.data + offset, device_name.c_str(), device_name.length());
offset += device_name.length();
cluster.header.data_size = offset;
return cluster;
}
int main() {
std::cout << "CFG++ LabVIEW Direct Integration Example" << std::endl;
std::cout << "========================================" << std::endl << std::endl;
// Example 1: Direct variant to CFG++ file
std::cout << "Example 1: Convert LabVIEW String Variant to CFG++ File" << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
auto string_variant = create_string_variant("Hello from LabVIEW!");
size_t variant_size = sizeof(LabViewDataHeader) + string_variant.header.data_size;
CfgppResult result = cfgpp_variant_to_file(&string_variant, variant_size, "string_output.cfgpp");
if (result == CFGPP_SUCCESS) {
std::cout << "✓ Successfully wrote string variant to string_output.cfgpp" << std::endl;
} else {
std::cout << "✗ Failed to write string variant: " << result << std::endl;
}
// Example 2: Convert double variant to string
std::cout << std::endl << "Example 2: Convert LabVIEW Double Variant to CFG++ String" << std::endl;
std::cout << "-----------------------------------------------------------" << std::endl;
auto double_variant = create_double_variant(42.123456);
variant_size = sizeof(LabViewDataHeader) + double_variant.header.data_size;
char output_buffer[256];
size_t actual_size;
result = cfgpp_variant_to_string(&double_variant, variant_size, output_buffer, sizeof(output_buffer), &actual_size);
if (result == CFGPP_SUCCESS) {
std::cout << "✓ CFG++ representation: " << std::string(output_buffer, actual_size) << std::endl;
} else {
std::cout << "✗ Failed to convert double variant: " << result << std::endl;
}
// Example 3: Convert cluster to CFG++ file
std::cout << std::endl << "Example 3: Convert LabVIEW Measurement Cluster to CFG++ File" << std::endl;
std::cout << "-------------------------------------------------------------" << std::endl;
auto measurement_cluster = create_measurement_cluster();
size_t cluster_size = sizeof(LabViewDataHeader) + measurement_cluster.header.data_size;
// Define field names for the cluster
const char* field_names[] = {
"sample_rate",
"voltage_threshold",
"enable_logging",
"device_name"
};
size_t field_count = sizeof(field_names) / sizeof(field_names[0]);
result = cfgpp_cluster_to_file(&measurement_cluster, cluster_size, field_names, field_count,
"measurement_config.cfgpp");
if (result == CFGPP_SUCCESS) {
std::cout << "✓ Successfully wrote measurement cluster to measurement_config.cfgpp" << std::endl;
std::cout << " Fields: sample_rate, voltage_threshold, enable_logging, device_name" << std::endl;
} else {
std::cout << "✗ Failed to write measurement cluster: " << result << std::endl;
}
// Example 4: Multiple variant types demonstration
std::cout << std::endl << "Example 4: Multiple Data Types" << std::endl;
std::cout << "------------------------------" << std::endl;
struct {
const char* name;
MockLabViewVariant variant;
const char* filename;
} test_cases[] = {
{"Integer (1000)", create_integer_variant(1000), "integer_test.cfgpp"},
{"Boolean (true)", create_boolean_variant(true), "boolean_test.cfgpp"},
{"Double (π)", create_double_variant(3.14159265359), "pi_test.cfgpp"},
{"String (Config)", create_string_variant("Configuration Data"), "config_test.cfgpp"}
};
for (auto& test_case : test_cases) {
size_t size = sizeof(LabViewDataHeader) + test_case.variant.header.data_size;
result = cfgpp_variant_to_file(&test_case.variant, size, test_case.filename);
if (result == CFGPP_SUCCESS) {
std::cout << "✓ " << test_case.name << " → " << test_case.filename << std::endl;
} else {
std::cout << "✗ " << test_case.name << " failed (" << result << ")" << std::endl;
}
}
// Performance demonstration
std::cout << std::endl << "Performance Test: 1000 Variant Conversions" << std::endl;
std::cout << "===========================================";
auto start_time = std::chrono::high_resolution_clock::now();
int success_count = 0;
for (int i = 0; i < 1000; i++) {
auto test_variant = create_double_variant(i * 0.001);
size_t size = sizeof(LabViewDataHeader) + test_variant.header.data_size;
char buffer[128];
size_t buffer_size;
result = cfgpp_variant_to_string(&test_variant, size, buffer, sizeof(buffer), &buffer_size);
if (result == CFGPP_SUCCESS) {
success_count++;
}
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
std::cout << std::endl;
std::cout << "Results: " << success_count << "/1000 conversions successful" << std::endl;
std::cout << "Time: " << duration.count() << " microseconds" << std::endl;
std::cout << "Average: " << (duration.count() / 1000.0) << " µs per conversion" << std::endl;
std::cout << "Throughput: " << (1000000000.0 / duration.count()) << " conversions/second" << std::endl;
std::cout << std::endl << "LabVIEW Integration Example Complete!" << std::endl;
std::cout << "Check the generated .cfgpp files for results." << std::endl;
return 0;
}