Skip to main content

Event

device.Events Subscribe to operational status events.​

Automatic Logs

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.

Feature Overview​

CategoryEventNoteAutomatic Logging
Connectdevice.Events.ConnectedChangedAlternate between on and off in sync with the IO controller's cycle✓
Operating Statedevice.Events.StateChangedChanges in Operating State✓
Runtime Errordevice.Events.ErrorOccurredRuntime Error✓
Process Data Qualitydevice.Events.IoStatusChangedChanges in the "non-GOOD" byte count✓
Linkdevice.Events.LinkChangedChanges in the Network Card's Physical Link✓
Logdevice.Events.LoggedEach event listed above will trigger this one again.—

Connect​

ConnectedChanged​

device.Events.ConnectedChanged += (sender, e) => { };
// EventHandler<PnSConnectedChangedEventArgs>

Triggered when the periodic data exchange connection is lost (Connected == false where EverConnected == true = disconnection; EverConnected == false = never connected).

Callback parameters:

public sealed class PnSConnectedChangedEventArgs : EventArgs
{
public bool Connected { get; } // Is the data currently being updated for a new cycle?
public bool EverConnected { get; } // Have you ever connected to it?
public PnSEventLevel Level { get; } // Connected = Info; Disconnected / Never Connected = Warn
}

Example:

device.Events.ConnectedChanged += (sender, e) =>
{
if (!e.Connected && e.EverConnected)
Console.WriteLine("掉线");
};

Operating State​

StateChanged​

device.Events.StateChanged += (sender, e) => { };
// EventHandler<PnSStateChangedEventArgs>

Triggered when the operating state changes (SDK observation only; does not actively switch states).

Callback parameters:

public sealed class PnSStateChangedEventArgs : EventArgs
{
public PnSState OldState { get; } // Before the change, see [PnSState](./api#pnsstate)
public PnSState NewState { get; } // After the change
public PnSEventLevel Level { get; } // Info
}

Example:

device.Events.StateChanged += (sender, e) =>
Console.WriteLine($"{e.OldState} → {e.NewState}");

ErrorOccurred​

device.Events.ErrorOccurred += (sender, e) => { };
// EventHandler<PnSErrorEventArgs>

Triggered when a runtime error occurs; e.ErrorCode it is an int primitive value; for enumeration types, use device.ErrorCode.

Callback parameters:

public sealed class PnSErrorEventArgs : EventArgs
{
public int ErrorCode { get; } // Original value of a runtime error (not an enumeration)
public string Message { get; } // News in Chinese
public PnSEventLevel Level { get; } // Warn
}

Example:

device.Events.ErrorOccurred += (sender, e) =>
Console.WriteLine($"{e.ErrorCode}: {e.Message}");

IoStatusChanged​

device.Events.IoStatusChanged += (sender, e) => { };
// EventHandler<PnSIoStatusChangedEventArgs>

The first time I read it, I IOPS only recorded the baseline—no callback. inputIopsHex If outputIopsHex both are missing, no fiction, no callback. A callback is triggered only when the byte count changes and is not a "GOOD" byte. The same rule applies across all six languages—a callback is not triggered on every read.

Callback parameters:

public sealed class PnSIoStatusChangedEventArgs : EventArgs
{
public int IopsBad { get; } // IOPS BAD Byte Count (0 = All GOOD)
public PnSEventLevel Level { get; } // BAD > 0 = Warn; otherwise, Info
}

Example:

device.Events.IoStatusChanged += (sender, e) =>
{
if (e.IopsBad > 0)
Console.WriteLine($"IOPS BAD: {e.IopsBad}");
};

LinkChanged​

device.Events.LinkChanged += (sender, e) => { };
// EventHandler<PnSLinkChangedEventArgs>

Triggered when the physical link of the network card changes (during status polling, determined by the "LinkUp" field in the process data channel header; on the first observation, only the baseline is recorded, and no trigger occurs).

Callback parameters:

public sealed class PnSLinkChangedEventArgs : EventArgs
{
public bool LinkUp { get; } // Is the link up?
public PnSEventLevel Level { get; } // Up = Info; Down = Warn
}

Example:

device.Events.LinkChanged += (sender, e) =>
Console.WriteLine(e.LinkUp ? "链路 Up" : "链路 Down");

Log​

Logged​

public event Action<PnSEventLevel, string> Logged;

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 Level
  • message (string) — Log body
PnSEventLevel Enumeration Values

Events and Log Levels.

public enum PnSEventLevel
{
Info = 0, // Information
Warn = 1, // Warning
Error = 2, // Error
}

Connection = Info, Disconnected / Run error = Warn.

Example:

device.Events.Logged += (PnSEventLevel level, string message) =>
Console.WriteLine($"[{level}] {message}");

Thread Safety​

Event callbacks are not triggered on the UI thread. Thread synchronization is required when updating the user interface:

// WinForms
device.Events.ConnectedChanged += (sender, e) =>
this.Invoke(() => labelStatus.Text = e.Connected ? "已连接" : "未连接");

// WPF
device.Events.ConnectedChanged += (sender, e) =>
Dispatcher.Invoke(() => StatusText = e.Connected ? "已连接" : "未连接");

Events are dispatched asynchronously through the thread pool and are not called back synchronously on the observing thread. Each subscriber gets its own try/catch: if one subscriber throws, only that one callback is dropped, and later subscribers for the same event are not skipped. The failure count is observable through device.Events.EventsDropped (long, read-only).

To unsubscribe, use the built-in C# event -=. If you subscribe to the same delegate N times, you will receive N notifications, and it takes -= N subscriptions to completely unsubscribe.