Skip to content

opc.tcp · UA Binary

OPC UA Server

Put an in-memory OPC UA server into your application. Register variables, expose them on secure endpoints and answer clients while you call poll.

What it does

  • opc.tcp and UA Binary with the Basic256Sha256, Aes128_Sha256_RsaOaep and Aes256_Sha256_RsaPss policies.
  • Browse, Read, Write and Call, plus Subscriptions and MonitoredItems.
  • Add variables and set or read their values.
  • Public variables, including for anonymous users, and variables for authenticated users only.
  • Build the server from C, C++ or .NET.

Example

Each binding shows code from a real example file in the examples folder of the PDComm SDK. The source file and line range appear above the code.

OPC UA C server

Source: examples/opcua/opcua_server.c:104-134
  bind.data = (const uint8_t *)argv[1];
  bind.size = strlen(argv[1]);
  status = pdcomm_opcua_server_create(bind, &options, &server);
  example_wipe(key, key_size); /* The SDK copied the identity. */
  key = NULL;
  if (status != PDCOMM_OPCUA_OK) { print_error("create", status); goto done; }
  status = add(server, 1001, "Value", 42, PDCOMM_OPCUA_VARIABLE_WRITABLE | PDCOMM_OPCUA_VARIABLE_PUBLIC);
  if (status == PDCOMM_OPCUA_OK) status = add(server, 1002, "Limit", 100, PDCOMM_OPCUA_VARIABLE_PUBLIC);
  if (status == PDCOMM_OPCUA_OK) status = add(server, 1003, "Protected", 7, PDCOMM_OPCUA_VARIABLE_WRITABLE);
  if (status != PDCOMM_OPCUA_OK) {
    print_error("add_variable", status);
    goto done;
  }
  status = pdcomm_opcua_server_local_address(server, &address);
  if (status == PDCOMM_OPCUA_OK)
    status = pdcomm_opcua_buffer_copy(address, text, sizeof(text) - 1, &size);
  if (status != PDCOMM_OPCUA_OK) { print_error("local_address", status); goto done; }
  text[size] = 0;
  /* Install the stop handlers before announcing readiness. */
  signal(SIGINT, on_signal);
  signal(SIGTERM, on_signal);
#ifdef SIGBREAK
  signal(SIGBREAK, on_signal); /* Windows Ctrl+Break, e.g. from a service manager */
#endif
  printf("Listening on %s\n", (const char *)text);
  started = time(NULL);
  while (!stop_requested && (!seconds || difftime(time(NULL), started) < (double)seconds)) {
    status = pdcomm_opcua_server_poll(server);
    if (status != PDCOMM_OPCUA_OK) { print_error("poll", status); goto done; }
    example_sleep_ms(1);
  }

From the C example: create a secure-only server from the server certificate, private key and trusted client certificate, add three Int32 variables, then serve requests by calling pdcomm_opcua_server_poll() in a loop.