Example SweeperΒΆ

  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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright [2017] Zurich Instruments AG

#include <stdio.h>
#include <stdlib.h>

#include "ziAPI.h"
#include "ziUtils.hpp"


void decodeChunk(ZIModuleEventPtr ev);

int main()
{
  // Define the device address of the device to run the example on.
  const char* deviceAddress = ziUtilsGetEnv("LABONE_DEVICE", "dev2006");
  printf("ENV LABONE_DEVICE=%s\n", deviceAddress);

  // Address of the data server.
  const char* dataServer = ziUtilsGetEnv("LABONE_SERVER", "localhost");

  // Port of the data server.
  const uint16_t port = 8004;

  // Over which interface the connection to the
  // device should be established. Can be either
  // - 1: "1GbE" - Ethernet
  // - 2: "USB" - USB
  // - 3: "PCIe" - PCIe
  const char* deviceInterface = "1GbE";

  ZIConnection conn = nullptr;

  if (isError(ziAPIInit(&conn)))
  {
    return 1;
  }

  ziAPISetDebugLevel(0);
  ziAPIWriteDebugLog(0, "Logging enabled.");

  try
  {
    checkError(
        ziAPIConnectEx(conn, dataServer, port, ZI_API_VERSION_6, nullptr));
    ziApiServerVersionCheck(conn);
    checkError(
        ziAPIConnectDevice(conn, deviceAddress, deviceInterface, nullptr));
    // Note, no device settings applied here; the device should be
    // configured appropriately for the sweep beforehand.
    ZIModuleHandle sweeper;
    checkError(ziAPIModCreate(conn, &sweeper, "sweep"));
    printf("module created, handle=%" PRIu64 "\n", sweeper);
    checkError(ziAPIModSetIntegerData(conn, sweeper, "xmapping", 1));
    checkError(ziAPIModSetString(conn, sweeper, "device", deviceAddress));
    checkError(ziAPIModSetDoubleData(conn, sweeper, "start", 1000.0000000));
    checkError(ziAPIModSetDoubleData(conn, sweeper, "stop", 1000000.0000000));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "scan", 0));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "samplecount", 100));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "loopcount", 1));
    checkError(ziAPIModSetString(conn, sweeper, "gridnode", "oscs/0/freq"));
    checkError(
        ziAPIModSetDoubleData(conn, sweeper, "settling/time", 0.0000000));
    checkError(
        ziAPIModSetDoubleData(conn, sweeper, "settling/inaccuracy", 0.0001000));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "averaging/sample", 10));
    checkError(ziAPIModSetDoubleData(conn, sweeper, "averaging/tc", 5.0000000));
    checkError(ziAPIModSetDoubleData(conn, sweeper, "bandwidth", 0.0000000));
    checkError(
        ziAPIModSetDoubleData(conn, sweeper, "maxbandwidth", 1250000.0000000));
    checkError(
        ziAPIModSetDoubleData(conn, sweeper, "omegasuppression", 40.0000000));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "bandwidthcontrol", 2));

    char savepath[] = "/tmp/";
    checkError(ziAPIModSetString(conn, sweeper, "save/directory", savepath));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "order", 4));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "phaseunwrap", 0));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "sincfilter", 0));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "historylength", 100));
    checkError(ziAPIModSetIntegerData(conn, sweeper, "endless", 0));

    printf("Finished writing sweep parameters.\n");

    char path[1024];
    snprintf(path, sizeof(path), "/%s/demods/0/sample", deviceAddress);
    checkError(ziAPIModSubscribe(conn, sweeper, path));
    printf("Executing\n");
    checkError(ziAPIModExecute(conn, sweeper));
    ZIIntegerData finished;
    printf("Checking if finished\n");
    checkError(ziAPIModFinished(conn, sweeper, &finished));
    ZIModuleEventPtr ev = NULL;

    while (finished == 0)
    {
      sleep(500);  // 500 [ms]

      // Read out intermediate results. Data for grid points that
      // have not been measured yet will be nan.
      checkError(ziAPIModRead(conn, sweeper, ""));
      ZIValueType_enum valueType;
      uint64_t chunks;
      ZIResult_enum res =
          ziAPIModNextNode(conn, sweeper, path, 1024, &valueType, &chunks);
      while (res == ZI_INFO_SUCCESS)
      {
        printf(
            "Reading intermediate results, got node: %s with %" PRIu64
            " chunks of type ",
            path,
            chunks);
        for (uint64_t chunk = 0; chunk < chunks; ++chunk)
        {
          checkError(ziAPIModGetChunk(conn, sweeper, chunk, &ev));
          printf(
              "Data of chunk %" PRIu64 ": type %d, header time %" PRId64 "\n",
              chunk,
              ev->value->valueType,
              ev->header->systemTime);
          decodeChunk(ev);
        }
        printf("\n");
        res = ziAPIModNextNode(conn, sweeper, path, 1024, &valueType, &chunks);
      }

      ZIDoubleData progress;
      checkError(ziAPIModProgress(conn, sweeper, &progress));
      printf("Progress: %.2f%%   \n", progress * 100);

      checkError(ziAPIModFinished(conn, sweeper, &finished));
    }
    printf("\n\n");
    checkError(ziAPIModFinish(conn, sweeper));
    checkError(ziAPIModUnSubscribe(conn, sweeper, "*"));

    // Note: The final data may have been read out above in an "intermediate"
    // after the sweeper had actually finished.
    checkError(ziAPIModRead(conn, sweeper, ""));
    ZIValueType_enum valueType;
    uint64_t chunks;
    ZIResult_enum res =
        ziAPIModNextNode(conn, sweeper, path, 1024, &valueType, &chunks);
    while (res == ZI_INFO_SUCCESS)
    {
      printf(
          "Reading finished result, got node: %s with %" PRIu64
          " chunks of type ",
          path,
          chunks);
      printf("%d\n", valueType);
      for (uint64_t chunk = 0; chunk < chunks; ++chunk)
      {
        checkError(ziAPIModGetChunk(conn, sweeper, chunk, &ev));
        printf(
            "Data of chunk %" PRIu64 ": type %d, header time %" PRId64 "\n",
            chunk,
            ev->value->valueType,
            ev->header->systemTime);
        decodeChunk(ev);
      }
      printf("\n");
      res = ziAPIModNextNode(conn, sweeper, path, 1024, &valueType, &chunks);
    }
    if (res != ZI_WARNING_NOTFOUND)
    {
      checkError(res);
    }
    checkError(ziAPIModEventDeallocate(conn, sweeper, ev));

    // Release module resources. Especially important if modules are created
    // inside a loop to prevent excessive resource consumption.
    checkError(ziAPIModClear(conn, sweeper));
    ziAPIDisconnect(conn);
  }
  catch (std::runtime_error& e)
  {
    char extErrorMessage[1024] = "";
    ziAPIGetLastError(conn, extErrorMessage, 1024);
    fprintf(stderr, "Error: %s\ndetails: %s\n", e.what(), extErrorMessage);
  }

  ziAPIDestroy(conn);
  return 0;
}

void decodeChunk(ZIModuleEventPtr ev)
{
  printf(
      "  - Chunk header: created=%" PRId64 ", changed=%" PRId64 "\n",
      ev->header->createdTimeStamp,
      ev->header->changedTimeStamp);
  switch (ev->value->valueType)
  {
  case ZI_VALUE_TYPE_NONE:  // 0
    printf("  - Type 'None' can't be decoded and should not appear in the "
           "lookup!\n");
    break;
  case ZI_VALUE_TYPE_DOUBLE_DATA:  // 1
  {
    ZIEvent& e = *ev->value;
    printf("  - %d samples:\n", e.count);
    for (size_t i = 0; i < e.count; ++i)
    {
      printf("    - sample %" PRsize_t "d = %f\n", i, e.value.doubleData[i]);
    }
  }
  break;
  case ZI_VALUE_TYPE_INTEGER_DATA:  // 2
  {
    ZIEvent& e = *ev->value;
    printf("  - %d samples:\n", e.count);
    for (size_t i = 0; i < e.count; ++i)
    {
      printf(
          "    - sample %" PRsize_t "d = %" PRId64 "\n",
          i,
          e.value.integerData[i]);
    }
  }
  break;
  case ZI_VALUE_TYPE_DEMOD_SAMPLE:  // 3
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_SCOPE_WAVE_OLD:  // 4
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_AUXIN_SAMPLE:  // 5
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_DIO_SAMPLE:  // 6
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_BYTE_ARRAY:  // 7
  {
    ZIByteArray& v = *ev->value->value.byteArray;
    printf("  - length = %u\n", v.length);
    char* str = reinterpret_cast<char*>(malloc(v.length + 1));
    strncpy(str, reinterpret_cast<char*>(v.bytes), v.length);
    str[v.length] = '\0';
    printf("  - value = '%s'\n", str);
    free(str);
  }
  break;
  case ZI_VALUE_TYPE_PWA_WAVE:  // 8
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_TREE_CHANGE_DATA_OLD:  // 16
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_DOUBLE_DATA_TS:  // 32
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_INTEGER_DATA_TS:  // 33
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_SCOPE_WAVE:  // 35
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_SCOPE_WAVE_EX:  // 36
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_BYTE_ARRAY_TS:  // 38
  {
    ZIByteArrayTS& v = *ev->value->value.byteArrayTS;
    printf("  - timeStamp = %" PRIu64 "\n", v.timeStamp);
    printf("  - length = %u\n", v.length);
    char* str = reinterpret_cast<char*>(malloc(v.length + 1));
    strncpy(str, reinterpret_cast<char*>(v.bytes), v.length);
    str[v.length] = '\0';
    printf("  - value = '%s'\n", str);
    free(str);
  }
  break;
  case ZI_VALUE_TYPE_TREE_CHANGE_DATA:  // 48
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_ASYNC_REPLY:  // 50
    printf("  - Not implemented!\n");
    break;
  case ZI_VALUE_TYPE_SWEEPER_WAVE:  // 64
  {
    ZISweeperWave& v = *ev->value->value.sweeperWave;
    printf("  - timeStamp = %" PRIu64 "\n", v.timeStamp);
    printf("  - sampleCount = %" PRIu64 "\n", v.header.sampleCount);
    printf("  - flags = %d\n", v.header.flags);
    printf("  - sampleFormat = %d\n", v.header.sampleFormat);
    printf("  - sweepMode = %d\n", v.header.sweepMode);
    printf("  - bandwidthMode = %d\n", v.header.bandwidthMode);
    for (size_t i = 0; i < v.header.sampleCount; ++i)
    {
      printf("  - sample %" PRsize_t "d:\n", i);
      switch (v.header.sampleFormat)
      {
      case 0:
      {
        ZISweeperDoubleSample& s = v.data.dataDouble[i];
        printf("    - grid = %f\n", s.grid);
        printf("    - bandwidth = %f\n", s.bandwidth);
        printf("    - count = %" PRIu64 "\n", s.count);
        printf("    - v.avg = %f\n", s.value.avg);
        printf("    - v.stddev = %f\n", s.value.stddev);
        printf("    - v.pwr = %f\n", s.value.pwr);
      }
      break;
      case 1:
      {
        ZISweeperDemodSample& s = v.data.dataDemod[i];
        // printf("    - grid = %f\n", s.grid);
        // printf("    - bandwidth = %f\n", s.bandwidth);
        // printf("    - count = %" PRIu64 "\n", s.count);
        // printf("    - tc = %f\n", s.tc);
        // printf("    - tcMeas = %f\n", s.tcMeas);
        // printf("    - settling = %f\n", s.settling);
        // printf("    - setTimeStamp = %" PRIu64 "\n", s.setTimeStamp);
        // printf("    - nextTimeStamp = %" PRIu64 "\n", s.nextTimeStamp);
        // printf("    - x.avg = %f\n", s.x.avg);
        // printf("    - x.stddev = %f\n", s.x.stddev);
        // printf("    - x.pwr = %f\n", s.x.pwr);
        // printf("    - y.avg = %f\n", s.y.avg);
        // printf("    - y.stddev = %f\n", s.y.stddev);
        // printf("    - y.pwr = %f\n", s.y.pwr);
        printf("    - r.avg = %f\n", s.r.avg);
        // printf("    - r.stddev = %f\n", s.r.stddev);
        // printf("    - r.pwr = %f\n", s.r.pwr);
        // printf("    - phase.avg = %f\n", s.phase.avg);
        // printf("    - phase.stddev = %f\n", s.phase.stddev);
        // printf("    - phase.pwr = %f\n", s.phase.pwr);
        // printf("    - frequency.avg = %f\n", s.frequency.avg);
        // printf("    - frequency.stddev = %f\n", s.frequency.stddev);
        // printf("    - frequency.pwr = %f\n", s.frequency.pwr);
        // printf("    - auxin0.avg = %f\n", s.auxin0.avg);
        // printf("    - auxin0.stddev = %f\n", s.auxin0.stddev);
        // printf("    - auxin0.pwr = %f\n", s.auxin0.pwr);
        // printf("    - auxin1.avg = %f\n", s.auxin1.avg);
        // printf("    - auxin1.stddev = %f\n", s.auxin1.stddev);
        // printf("    - auxin1.pwr = %f\n", s.auxin1.pwr);
      }
      break;
      case 2:
      {
        ZISweeperImpedanceSample& s = v.data.dataImpedance[i];
        // printf("    - grid = %f\n", s.grid);
        // printf("    - bandwidth = %f\n", s.bandwidth);
        // printf("    - count = %" PRIu64 "\n", s.count);
        // printf("    - tc = %f\n", s.tc);
        // printf("    - tcMeas = %f\n", s.tcMeas);
        // printf("    - settling = %f\n", s.settling);
        // printf("    - setTimeStamp = %" PRIu64 "\n", s.setTimeStamp);
        // printf("    - nextTimeStamp = %" PRIu64 "\n", s.nextTimeStamp);
        printf("    - realz.avg = %f\n", s.realz.avg);
        // printf("    - realz.stddev = %f\n", s.realz.stddev);
        // printf("    - realz.pwr = %f\n", s.realz.pwr);
        // printf("    - imagz.avg = %f\n", s.imagz.avg);
        // printf("    - imagz.stddev = %f\n", s.imagz.stddev);
        // printf("    - imagz.pwr = %f\n", s.imagz.pwr);
        // printf("    - absz.avg = %f\n", s.absz.avg);
        // printf("    - absz.stddev = %f\n", s.absz.stddev);
        // printf("    - absz.pwr = %f\n", s.absz.pwr);
        // printf("    - phasez.avg = %f\n", s.phasez.avg);
        // printf("    - phasez.stddev = %f\n", s.phasez.stddev);
        // printf("    - phasez.pwr = %f\n", s.phasez.pwr);
        // printf("    - frequency.avg = %f\n", s.frequency.avg);
        // printf("    - frequency.stddev = %f\n", s.frequency.stddev);
        // printf("    - frequency.pwr = %f\n", s.frequency.pwr);
        // printf("    - param0.avg = %f\n", s.param0.avg);
        // printf("    - param0.stddev = %f\n", s.param0.stddev);
        // printf("    - param0.pwr = %f\n", s.param0.pwr);
        // printf("    - param1.avg = %f\n", s.param1.avg);
        // printf("    - param1.stddev = %f\n", s.param1.stddev);
        // printf("    - param1.pwr = %f\n", s.param1.pwr);
        // printf("    - drive.avg = %f\n", s.drive.avg);
        // printf("    - drive.stddev = %f\n", s.drive.stddev);
        // printf("    - drive.pwr = %f\n", s.drive.pwr);
        // printf("    - bias.avg = %f\n", s.bias.avg);
        // printf("    - bias.stddev = %f\n", s.bias.stddev);
        // printf("    - bias.pwr = %f\n", s.bias.pwr);
        // printf("    - auxin0.avg = %f\n", s.auxin0.avg);
        // printf("    - auxin0.stddev = %f\n", s.auxin0.stddev);
        // printf("    - auxin0.pwr = %f\n", s.auxin0.pwr);
        // printf("    - auxin1.avg = %f\n", s.auxin1.avg);
        // printf("    - auxin1.stddev = %f\n", s.auxin1.stddev);
        // printf("    - auxin1.pwr = %f\n", s.auxin1.pwr);
        printf("    - flags = %d\n", s.flags);
      }
      break;
      default:
        printf("  - Unknown sample format!\n");
      }
    }
  }
  break;
  case ZI_VALUE_TYPE_SPECTRUM_WAVE:  // 65
    printf("  - Not implemented!\n");
    break;
  default:
    printf("  - Unknown data type!\n");
  }
}