Skip to main content

C SDK Overview

A pure C interface for developing PROFINET IO Device applications. The header files are located in Download Page or the installation directory SDK\SDK_C\. In the code #include "pns.h".

Environment​

  • Windows 10 / 11 x64
  • Darra Profinet Slave Installed

Quick Start​

After "Start", map once I / Q into a struct, then keep reading and writing fields through that struct with Pack=1. The struct is mapped onto the process image, so when the source changes the fields change with it; ReadStruct / WriteStruct are snapshots, and the copied instance will not change on its own. Writing I is not supported.

Cycle switching occurs automatically; there is no need to write your own cycle loop. Reads are concurrent; for writes, the caller must serialize them.

#include "pns.h"

#pragma pack(push, 1)
typedef struct { /* I: Controller → Device */
uint8_t Present;
uint8_t Trigger;
} from_plc_t;

typedef struct { /* Q: Equipment → Controller */
uint8_t Ready;
uint8_t Busy;
uint8_t Ok;
uint8_t Ng;
} to_plc_t;
#pragma pack(pop)

int main(void)
{
pns_service_handle_t session = NULL;

if (PnSServiceConnectRealtime(&session) != PNS_OK)
return 1;
if (PnSServiceStart(session) != PNS_OK)
return 1;

/* Map once */
const uint8_t *i = NULL; /* I Read-Only: Using Views RO */
volatile uint8_t *q = NULL; /* Q: Writable */
size_t ni = 0, nq = 0;
if (PnSProcessDataInputsMappingRO(session, &i, &ni) != PNS_OK ||
PnSProcessDataOutputsMapping(session, &q, &nq) != PNS_OK ||
i == NULL || q == NULL || /* On failure, *p = NULL Never dereference */
ni < sizeof(from_plc_t) || nq < sizeof(to_plc_t))
{
PnSServiceStop(session);
PnSServiceClose(session);
return 1;
}

const from_plc_t *inp = (const from_plc_t *)i;
to_plc_t *outp = (to_plc_t *)q;

while (running)
{
if (!PnSServiceIsConnected(session))
continue;
uint8_t present = inp->Present; /* Read I: Changes on Its Own After Bus Refresh */
outp->Ready = 1; /* Q: Write the process image directly */
(void)present;
}

PnSServiceStop(session);
PnSServiceClose(session);
return 0;
}