Skip to main content

C# SDK Overview

Develop the PROFINET IO Device application's .NET library. In the code using Darra.PnS;.

Installation​

Install from NuGet:

dotnet add package Darra.Profinet.Slave

You can also access it fromDownload Pageupload nupkg, or use the copy located in SDK\SDK_CSharp\ the installation directory.

Environment​

  • Windows 10 / 11 x64
  • .NET Standard 2.0 (.NET Framework 4.6.1+ / .NET 6+)
  • Darra Profinet Slave Installed

Quick Start​

After "Start," set I / Q Map onceCreate a structure, then use this variable to read and write fields throughout the process. The structure is mapped onto the process image; when the source changes, the fields change accordingly (the I side returns ref readonly T read-only—the controller overwrites the entire segment every cycle; writing to I returns; NotSupported the Q side returns writable ref T ). Read / Write This is a snapshot; the copied instance will not change on its own. Writing to it results in a throw In[i] InvalidOperationException.

Cycle switching occurs automatically; there is no need to write your own cycle loop, and any thread can use it simultaneously.

using System.Runtime.InteropServices;
using Darra.PnS;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct FromPlc // I: Controller → Device
{
public byte Present;
public short Recipe; // Online IO Controller Endpoint: Simply align it with the upper end; retrieve the value using `Read(ref rx)`
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ToPlc // Q: Equipment → Controller
{
public byte Ready;
public byte Busy;
}

using (var device = new DarraPnS())
{
PnSErrorCode rc = device.Start();
if (rc != PnSErrorCode.Success)
return;

// Map once
ref readonly FromPlc inp = ref device.ProcessData.InputsMapping<FromPlc>();
ref ToPlc outp = ref device.ProcessData.OutputsMapping<ToPlc>();

while (running)
{
if (!device.Connected)
continue;

byte present = inp.Present; // Read I: Changes on Its Own After Bus Refresh
outp.Ready = 1; // Q: Write the process image directly
}

device.Stop();
}

Multi-byte fields use PROFINET network big-endian (wire) byte order: reading them directly as host short / int reverses the bytes; for host-order values use Read / Write (big-endian encode/decode).