Example SetGet#

  1// Copyright [2018] Zurich Instruments AG
  2
  3#include <algorithm>
  4#include <cstdlib>
  5#include <ctime>
  6#include <iostream>
  7#include <map>
  8#include <string>
  9
 10#include "ziAPI.h"
 11#include "ziUtils.hpp"
 12
 13void setDemodRate(
 14    ZIConnection conn,
 15    const char** deviceAddress,
 16    uint32_t index,
 17    ZIDoubleData rate)
 18{
 19  char nodePath[1024];
 20  snprintf(
 21      nodePath, sizeof(nodePath), "/%s/demods/%d/rate", *deviceAddress, index);
 22  checkError(ziAPISetValueD(conn, nodePath, rate));
 23}
 24
 25ZIDoubleData getDemodRate(
 26    ZIConnection conn, const char** deviceAddress, uint32_t index)
 27{
 28  ZIDoubleData rate;
 29  char nodePath[1024];
 30  snprintf(
 31      nodePath, sizeof(nodePath), "/%s/demods/%d/rate", *deviceAddress, index);
 32  checkError(ziAPIGetValueD(conn, nodePath, &rate));
 33  return rate;
 34}
 35
 36int main()
 37{
 38  // The device address of the device to run the example on.
 39  const char* deviceAddress = ziUtilsGetEnv("LABONE_DEVICE", "dev2006");
 40  printf("ENV LABONE_DEVICE=%s\n", deviceAddress);
 41
 42  // Address of the data server.
 43  const char* dataServer = ziUtilsGetEnv("LABONE_SERVER", "localhost");
 44
 45  // Port of the data server.
 46  const uint16_t port = 8004;
 47
 48  // Over which interface the connection to the
 49  // device should be established. Can be either
 50  // - 1: "1GbE" - Ethernet
 51  // - 2: "USB" - USB
 52  // - 3: "PCIe" - PCIe
 53  const char* deviceInterface = "1GbE";
 54
 55  ZIConnection conn = nullptr;
 56
 57  if (isError(ziAPIInit(&conn)))
 58  {
 59    return 1;
 60  }
 61
 62  ziAPISetDebugLevel(0);
 63  ziAPIWriteDebugLog(0, "Logging enabled.");
 64
 65  try
 66  {
 67    checkError(
 68        ziAPIConnectEx(conn, dataServer, port, ZI_API_VERSION_6, nullptr));
 69    ziApiServerVersionCheck(conn);
 70    checkError(
 71        ziAPIConnectDevice(conn, deviceAddress, deviceInterface, nullptr));
 72
 73    // Set a device configuration.
 74    uint32_t index = 0;
 75    ZIDoubleData rate = 10e3;
 76    setDemodRate(conn, &deviceAddress, index, rate);
 77
 78    // Read it back.
 79    rate = getDemodRate(conn, &deviceAddress, index);
 80    std::cout << "[INFO] "
 81              << "Device " << deviceAddress << " demod " << index
 82              << " has rate " << rate << ".\n";
 83
 84    ziAPIDisconnect(conn);
 85  }
 86  catch (std::runtime_error& e)
 87  {
 88    char extErrorMessage[1024] = "";
 89    ziAPIGetLastError(conn, extErrorMessage, 1024);
 90    fprintf(stderr, "[ERROR] %s\ndetails: `%s`\n.", e.what(), extErrorMessage);
 91    return 1;
 92  }
 93  catch (...)
 94  {
 95    // Ensure all exceptions are caught.
 96    fprintf(stderr, "[ERROR] Unexpected error\n.");
 97    return 1;
 98  }
 99
100  ziAPIDestroy(conn);
101  return 0;
102}