Skip to main content

PnSService

PROFINET IO Device Entry point. Start the device, write to Q and read from I, view the operating status, and run diagnostics. Crate name darra_pns, entry point type PnSService.

This product is designed SDK exclusively for this purpose PROFINET IO Deviceand does not provide interfaces for other fieldbus protocols.

Class Structure​

PnSService                                         // device instance
│
├── process_data() → ProcessData // write Q / read I
│ ├── inputs_mapping::<T>() / outputs_mapping::<T>() // map once
│ ├── read_struct / write_struct // snapshot, value does not change on its own
│ ├── in_i8(offset) … in_bit(offset, bit: u8) // read I (per-item scalar), read-only
│ └── out_i8(offset, v) … out_bit(offset, bit: u8, v) // write Q (per-item scalar), writable
├── events() → ServiceEvents // operating-state events
│ ├── on_connected_changed // cyclic exchange on/off
│ ├── on_state_changed // operating-state change
│ ├── on_error_occurred // runtime error
│ └── on_logged // each of the above also logs this
├── variables() → PnSVariables // variable association
├── diagnostics() → Diagnostics // diagnostics sub-object (snapshot / IOPS / alarms / records)
│ ├── snapshot() // diagnostics snapshot
│ ├── io_status() // IOPS / IOCS
│ ├── alarms() / alarm_history() // local alarms / history
│ └── records() // user-area records
│
├── connected() / state() / wait_reason() // cyclic exchange / operating state / wait reason
├── ever_connected() / error_code() // ever connected / runtime error
└── im_data() // I&M identification

Write Q (module output, device → controller), read I (module input, controller → device). The flat method diag_snapshot() / is alarms() still available, using the same diagnostics() data.

diagnostics() returns a Diagnostics that is not re-exported at the crate root: to write this type name use the full path darra_pns::service::Diagnostics (use darra_pns::Diagnostics; will not compile). PnSService / ServiceResult / ServiceState / ProcessData / BitIndex / Ioxs are re-exported, so use darra_pns::… just works.

Usage​

Start-Stop​

use darra_pns::PnSService;

let mut device = PnSService::new();
device.start()?;

while running {
if !device.connected() {
// device.wait_reason()
continue;
}
device.write_bool("Q0.0", true)?; // Write Q
let from_plc = device.read_bool("I0.0")?; // Read I
let _ = from_plc;
}

device.stop()?;

You don't need to check the status new: PnSService::is_service_ready() / service_connected() / service_wait_reason().

Process Data​

MethodVisitNote
Process Datadevice.process_data().inputs_mapping::<T>() / outputs_mapping::<T>()Map once
Process Datadevice.process_data()Snapshot read_struct / write_struct, Scalar by Item in_i8(offset) / out_i16(offset, v) Series

Identification​

let im = device.im_data()?;
let name = im.station_name;
let vendor = im.vendor_id;
let product = im.device_id;

Reading and Writing Process Data​

Word and double-word types are big-endian.

The "Golden Path" involves a single mapping: when the source changes, the field changes automatically (T required #[repr(C, packed)]).

#[repr(C, packed)]
struct FromPlc {
a: u8,
b: i16,
flag: u8,
}

// Map once
let inp = unsafe { device.process_data().inputs_mapping::<FromPlc>()? };
let outp = unsafe { device.process_data().outputs_mapping::<FromPlc>()? };
let present = inp.a; // It changes on its own after the bus is refreshed
outp.a = 1; // Lands directly in the Q process image
let _ = present;

device.process_data().out_u8(0, 0x11)?;
let v = device.process_data().in_u8(0)?;

Snapshot read_struct / write_struct(Structure implementation ProcessDataPacked: Required packed_size / unpack_from / pack_into Three, plus one more)With a default implementation(Note pack_to_vec: There's no need to duplicate the handwritten implementation) — The copied instance won't change on its own.

Diagnostics and Alarms​

Through diagnostic reading device.diagnostics() (read-only snapshots: links, frame count, watchdog, jitter, and alarm monitoring).

let snap = device.diagnostics().snapshot()?;
if !snap.ok {
println!("{}", snap.message);
}

for a in device.diagnostics().alarms()? {
println!("{}", a.message);
}