Event
device.Events() Subscribe to operational status events.
Each event is logged again when it is triggered Logged(with the severity level matching the source event), regardless of whether a corresponding event has been subscribed to. Subscribe Logged to receive messages in Chinese.
To receive ConnectedChanged / StateChanged / ErrorOccurred / LinkChanged, call it within a loop StatusEx(). On the first pass, only record the baseline; do not trigger anything.
IoStatusChanged When using the quality IO observation port: Adjust IoStatus(), or when accessing process data snapshots (Read / Write etc.), re-acquire 20 ms observations at the specified interval /api/io . Similarly, on the first run, only record the baseline; do not trigger the system.
Feature Overview
| Category | Event | Note | Automatic Logging |
|---|---|---|---|
| Connect | device.Events().ConnectedChanged | Alternate between on and off in sync with the IO controller's cycle | ✓ |
| Operating State | device.Events().StateChanged | Changes in Operating State | ✓ |
| Runtime Error | device.Events().ErrorOccurred | Runtime Error | ✓ |
| IO Quality | device.Events().IoStatusChanged | Changes in the "non-GOOD" byte count | ✓ |
| Link | device.Events().LinkChanged | Network Interface Card Link Changes | ✓ |
| Log | device.Events().Logged | Each event listed above will trigger this one again. | — |
Unsubscribe and Clear
std::function<void(bool, bool, PnSEventLevel)> onConn = ...;
device.Events().ConnectedChanged += onConn;
device.Events().ConnectedChanged -= onConn; // Remove the subscription that matches the first target type
device.Events().ConnectedChanged.Clear(); // Clear all subscriptions for this event
device.Events().ConnectedChanged.Size(); // Current Number of Subscribers
-= Matches based on the target std::function type: The same lambda expression or the same std::function object can be unsubscribed; different lambda expressions do not match each other, even if their signatures are identical. If the same subscription is added multiple times, only one is unsubscribed at a time.
Connect
ConnectedChanged
device.Events().ConnectedChanged +=
[](bool connected, bool ever_connected, PnSEventLevel level) { };
Triggered when the periodic data exchange connection is lost (connected == false where ever_connected == true = disconnection; ever_connected == false = never connected).
Callback parameters:
connected(bool) — Is the data currently being updated for a new cycle time?ever_connected(bool) — Have you ever connected?level(PnSEventLevel) — Connected = Info; Disconnected / Never Connected = Warn
Example:
device.Events().ConnectedChanged +=
[](bool connected, bool ever_connected, PnSEventLevel level) {
(void)level;
if (!connected && ever_connected)
printf("掉线\n");
};
Operating State
StateChanged
device.Events().StateChanged +=
[](PnSState old_state, PnSState new_state, PnSEventLevel level) { };
Triggered when the operating state changes (SDK observation only; does not actively switch states).
Callback parameters:
old_state(PnSState) — Before the changenew_state(PnSState) — After the changelevel(PnSEventLevel) — Info
Example:
device.Events().StateChanged +=
[](PnSState old_state, PnSState new_state, PnSEventLevel level) {
(void)level;
printf("%d → %d\n",
static_cast<int>(old_state), static_cast<int>(new_state));
};
ErrorOccurred
device.Events().ErrorOccurred +=
[](PnSRuntimeErrorCode code, const char* message, PnSEventLevel level) { };
Triggered when a runtime fault occurs; code this is a runtime fault enumeration—please use the original value device.ErrorCodeRaw().
Callback parameters:
code(PnSRuntimeErrorCode) — Runtime faultmessage(const char*) — News in Chineselevel(PnSEventLevel) — Warn
Example:
device.Events().ErrorOccurred +=
[](PnSRuntimeErrorCode code, const char* message, PnSEventLevel level) {
(void)level;
printf("%d: %s\n", static_cast<int>(code), message);
};
IoStatusChanged
device.Events().IoStatusChanged +=
[](bool iops_bad, PnSEventLevel level) { };
When reading for the first time, only record the baseline IOPS ; do not trigger a callback. If both IOPS the input and output fields are missing, do not generate data or trigger a callback; if any field contains invalid hex values, do not modify the baseline. Trigger a callback only when the byte count changes and the result is not GOOD(0x80); this applies consistently across all six languages—a callback is not triggered on every read. The criteria are the same as those in [... IoStatus().iops_bad_count ].
When triggered, automatically send another message Logged: BAD = Warn; when restored, GOOD = Info.
Callback parameters:
iops_bad(bool) — true = A byte that is IOPS not "GOOD"level(PnSEventLevel) — BAD = alarm; Restored GOOD = Info
Example:
device.Events().IoStatusChanged +=
[](bool iops_bad, PnSEventLevel level) {
(void)level;
if (iops_bad)
printf("IOPS BAD\n");
};
Link
LinkChanged
device.Events().LinkChanged +=
[](bool link_up, PnSEventLevel level) { };
Triggered when the network card link state changes. This is called within a loop, and StatusEx() a notification is received only if the snapshot includes a link field. It does not trigger if there is no link field. Do not treat Connected() as a link.
Now available to read DiagSnapshot().link_up Determine the link.
Callback parameters:
link_up(bool) — Is the link up?level(PnSEventLevel) — Up = Info; Down = Warn
Example:
device.Events().LinkChanged +=
[](bool link_up, PnSEventLevel level) {
(void)level;
printf(link_up ? "链路 Up\n" : "链路 Down\n");
};
Log
Logged
device.Events().Logged +=
[](PnSEventLevel level, const char* message) { };
This message is sent again each time an event is triggered, and its severity level follows that of the source event.
Callback parameters:
level(PnSEventLevel) — Log Levelmessage(const char*) — Log Body
PnSEventLevel
Event log level.
enum class PnSEventLevel {
Info = 0, // Information
Warn = 1, // Warning
Error = 2, // Error
};
Connection = Info, Disconnected / Run error = Warn.
Example:
device.Events().Logged +=
[](PnSEventLevel level, const char* message) {
printf("[%d] %s\n", static_cast<int>(level), message);
};
Thread
Event callbacks are triggered synchronously on the StatusEx() thread that called them. Raise A snapshot of the subscription table is taken before distribution: the += / -= / / being distributed Clear only affect the next event and do not invalidate the current iteration; each subscriber operates independently try/catch, so an exception thrown by one subscriber does not affect subsequent subscribers.Snapshots only ensure iteration safety; subscribing, unsubscribing, and dispatching should still be performed by the same thread.
Complete Example
device.Events().ConnectedChanged +=
[](bool connected, bool ever_connected, PnSEventLevel level) {
(void)level;
if (!connected && ever_connected)
printf("掉线\n");
};
device.Events().StateChanged +=
[](PnSState old_state, PnSState new_state, PnSEventLevel level) {
(void)level;
printf("%d → %d\n",
static_cast<int>(old_state), static_cast<int>(new_state));
};
device.Events().ErrorOccurred +=
[](PnSRuntimeErrorCode code, const char* message, PnSEventLevel level) {
(void)level;
printf("%d: %s\n", static_cast<int>(code), message);
};
device.Events().Logged +=
[](PnSEventLevel level, const char* message) {
printf("[%d] %s\n", static_cast<int>(level), message);
};