PnSService
The entry point for a PROFINET IO Device. Start the device, write Q / read I, and view the operating status and diagnostics. It implements AutoCloseable, so use try-with-resources.
This SDK only does PROFINET IO Device; it provides no entry points for other fieldbus protocols.
Class Structure
PnSService // Device instance (AutoCloseable)
│
├── getProcessData() → ProcessData // Write Q / Read I
│ ├── getInputsMapping(Class) / getOutputsMapping(Class) // Map once
│ ├── readStruct / writeStruct // Snapshot; values do not change on their own
│ ├── getIn() // Read I (module input), read-only
│ └── getOut() // Write Q (module output), writable
├── getEvents() → PnSEvents // Runtime events
│ ├── addConnectedChangedListener // Cycle exchange on/off
│ ├── addStateChangedListener // State changes
│ ├── addErrorOccurredListener // Runtime errors
│ └── addLoggedListener // Also logged for every event above
├── getDiagnostics() → PnSDiagnostics // Diagnostics subobject (snapshot / IOPS / alarms / records)
│ ├── getDiagSnapshot() // Diagnostics snapshot
│ ├── getIoStatus() // IOPS / IOCS
│ ├── getAlarms() / getAlarmHistory() // Local alarms / history
│ └── getRecords() // User-area records
│
├── isConnected() / getStatus() / getWaitReason() // Cycle exchange / state / wait reason
├── isEverConnected() / getErrorCodeEnum() // Ever connected / runtime errors
└── getImData() // I&M identification
Write Q (module output, device → controller), read I (module input, controller → device). The flat methods
getDiagSnapshot()/getAlarms()are still available and return the same data asgetDiagnostics().
PnSDiagnostics is in the com.darra.pns.service subpackage (the same package as the entry point PnSService). To write out this type name you use import com.darra.pns.service.PnSDiagnostics; — not com.darra.pns.PnSDiagnostics; that package only holds snapshot types such as PnSDiagSnapshot / PnSIoStatus.
Usage
Start-Stop
import com.darra.pns.service.PnSService;
try (PnSService device = new PnSService()) {
device.start();
while (running) {
if (!device.isConnected()) {
// device.getWaitReason()
continue;
}
device.writeBool("Q0.0", true); // Write Q
boolean fromPlc = device.readBool("I0.0"); // Read I
}
device.stop();
}
You don't need to check the status new: PnSService.isServiceReady() / isServiceConnected() / getServiceWaitReason().
Process Data
| Method | Visit | Note |
|---|---|---|
| Process Data | device.getProcessData().getInputsMapping(Class) / getOutputsMapping(Class) | Map once |
| Process Data | device.getProcessData() | Snapshot readStruct / writeStruct, getIn() / getOut() bytes |
Identification
PnSService.ImData im = device.getImData();
String name = im.getStationName();
int vendor = im.getVendorId();
int product = im.getDeviceId();
Reading and Writing Process Data
Word and double-word types are big-endian.
The gold path is a single mapping. Structure The field is a managed cache—read the latest value read(), and update it after making changes write().
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
@FieldOrder({"a", "b", "flag"})
class FromPlc extends Structure {
public byte a;
public short b;
public boolean flag;
public FromPlc() { super(ALIGN_NONE); }
}
// Map once
FromPlc inp = device.getProcessData().getInputsMapping(FromPlc.class);
FromPlc outp = device.getProcessData().getOutputsMapping(FromPlc.class);
inp.read(); // Process Image → Managed Cache
byte present = inp.a; // It changes on its own after the bus is refreshed
outp.a = 1;
outp.write(); // Cache Management → Process Image
device.getProcessData().getOut().get(0).setContent((byte) 0x11);
byte v = device.getProcessData().getIn().get(0).getContent();
Snapshot readStruct / writeStruct(Implementation ProcessData.Packed of packedSize / pack / unpack)— Copied instances do not change on their own.
Diagnostics and Alarms
Through diagnostic reading device.getDiagnostics() (read-only snapshots: links, frame count, watchdog, jitter, and alarm monitoring).
import com.darra.pns.PnSDiagSnapshot;
PnSDiagSnapshot snap = device.getDiagnostics().getDiagSnapshot();
if (!snap.isOk())
System.out.println(snap.getMessage());
for (PnSService.AlarmItem a : device.getDiagnostics().getAlarms())
System.out.println(a.getMessage());