Attributes and State Machines
Properties
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Static | IsServiceReady | bool | read-only | Whether the local service is ready |
LastError | string | read-only | The most recent failure message for the entire application (sticky; not cleared upon success) | |
LastErrorCode | PnSErrorCode | Read-only | The fault code from the most recent failed call across the entire application. Never fails; returns 'Success'. | |
ServiceConnected | bool | read-only | Indicates whether the IO controller is performing cyclic exchange. If the service is not started, this is set to false. | |
ServiceState | PnSState | Read-only | Runtime ST. If the service is not RUN, the ST is 'Unknown' | |
ServiceWaitReason | string | read-only | Reason for the wait. Service not up = 'Service not ready' | |
ServiceStatusMessage | string | read-only | ST message. If the service hasn't started = 'Service not ready' | |
| Connections and Sessions | Connected | bool | read-only | Whether the IO controller has established cyclic data exchange. Only true means cyclic data is being exchanged. |
Attached | bool | read-only | Whether this SDK session is connected to the local service. Stop does not change this value | |
IsRunning | bool | read-only | Whether the IO device is enabled (after Start, before Stop) | |
| Operational ST | State | PnSState | read-only | Enumeration of runtime states. If the actual ST cannot be read, it is set to 'Unknown'; do not treat it as 'None' |
WaitReason | string | read-only | Reason for this session's wait. Empty when the IO controller is connected | |
EverConnected | bool | read-only | Whether a link has ever been established with the IO controller. true and Connected is false = disconnected | |
| fault Codes | ErrorCode | PnSRuntimeErrorCode | read-only | Last runtime fault. Unrecognized value: Unknown. ErrorCodeEnum has been deprecated; use this property instead |
ErrorCodeRaw | int | read-only | Raw value of the runtime fault (0 = no fault). Unrecognized values are still passed through this property | |
ErrorMessage | string | read-only | Runtime fault message in Chinese | |
LastServiceError | string | read-only | The most recent failed message for this session (sticky; not cleared upon success) | |
| Diagnostic Portal | Events | PnSEvents | Read-only | Runtime events. |
Diagnostics | PnSDiagnostics | Read-only | Diagnostic subobjects (Snapshot / IOPS / Alarms / Logs).Snapshot / IoStatus / Alarms share the same data as flat properties | |
DiagSnapshot | PnSDiagSnapshot | Read-only | Diagnostic snapshot. | |
IoStatus | PnSIoStatus | read-only | IOPS / IOCS. | |
ProcessData | PnSProcessData | Read/Write | Process data. | |
| Cycle | HighResolutionTimer | bool | read-write | High-resolution timer (held by the service process). Write = Notifies the service to power-on/off; Read = The value set by the service for this session |
| Local Session | ServiceHost | string | read-only | The service host connected to in this session |
ServicePort | int | read-only | The service port connected to in this session | |
IsServiceMode | bool | read-only | Whether this is a local service session |
PnSState Enumeration Values
The link and data exchange ST between the device and the IO controller.
public enum PnSState
{
None = 0, // Not Initialized / Stack Not Initialized
Idle = 1, // Started, waiting for the IO controller (no AR)
Connecting = 2, // The controller has initiated a link (STARTUP)
Parameterized = 3, // Parameterization complete (PRMEND), waiting for the application to be ready
ApplicationReady = 4, // Application Ready (APPLRDY), parameter exchange complete
DataExchange = 5, // Cyclic data exchange is normal (DATA)
Aborted = 6, // link lost (ABORT)
Resetting = 7, // Device is resetting (reserved ST name; this ST is not generated during a reset in the current version)
ConfigError = 8, // Configuration Error Downgrade ST: ST can be queried, but process data exchange has not been started
Unknown = 0x7FFFFFFF, // Unknown / Unable to read the actual ST
}
- Only
DataExchangeindicates that periodic data is being exchanged. Unknown"Unknown" / The actual ST cannot be read; do not treat it asNone.
PnSRuntimeErrorCode Enumeration Values
Enumeration of server-side runtime errors (as revealed by ST queries errorCode).
public enum PnSRuntimeErrorCode
{
Ok = 0, // No errors
ErrInit = -1, // Stack initialization failed
ErrCfg = -2, // Invalid configuration
ErrThread = -3, // Failed to create a periodic thread / set priority
ErrRunning = -4, // Invalid ST (repeated start/stop)
ErrPdi = -5, // Process Data Exchange Failed
ErrDap = -6, // Slot 0: Insertion Failed
ErrNull = -8, // The device has not started (not "Start" or "Stop")
ErrDiag = -9, // Invalid diagnostic parameter
Win32Error = 1, // System fault codes returned by the demoted ST (positive codes, such as 2048)
Unknown = -10000, // Unrecognized value
}
- Call Return Codes PnSErrorCode The meanings are different; they should not be used interchangeably.
- Unrecognized values are returned as-is
Unknown, while the original values are passed throughErrorCodeRawexactly as they are.
PnSErrorCode Enumeration Values
SDK Return values for calls (start/stop, read/write process data, diagnostics).
public enum PnSErrorCode
{
Success = 0, // Success
InvalidArgument = -1, // Invalid parameter (null handle / invalid slot / data area out-of-bounds, etc.)
InvalidHandle = -2, // Handle is invalid (not started or has been STOP)
NotConnected = -3, // Not connected (None AR / Not in data exchange)
NativeError = -4, // Stack / Remote fault
LengthMismatch = -5, // Data length mismatch
NotFound = -6, // Alert / Diagnostic entry does not exist or its ST is invalid
Busy = -7, // Busy: The previous operation is not complete (e.g., an alarm ACK), or the door being connected to in real-time is occupied by another session.
NotSupported = -8, // Not supported (e.g., writing to a read-only input address)
NativeDllNotFound = -100, // The built-in library was not loaded
PlatformNotSupported = -101, // Windows Only x64
ServiceUnreachable = -102, // Session Unreachable / Timeout
}
- Negative-value system:
0= success, negative value = failure. - Cross-language conversions are allowed
PnSErrorCodeMap.FromNativeCode(int), but direct translations are prohibited.
Start-Stop
Start()
public PnSErrorCode Start()
Enable the IO device. If it has already been Started and not yet Stopped, the function returns Success idempotently. This does not mean the IO controller has completed cyclic exchange; whether the IO controller is exchanging data is shown by Connected.
Return value:
PnSErrorCode—SuccessSuccess; Returned after releaseInvalidHandle; Process data not ready / Returned due to server startup failureNativeError; Service unreachable;ServiceUnreachable
A write failure results in a static exception LastError. An exception is thrown when another instance is in use; InvalidOperationException otherwise, the path IO does not throw an exception.
Example:
PnSErrorCode rc = device.Start();
if (rc != PnSErrorCode.Success)
Console.WriteLine(DarraPnS.LastError);
Stop()
public void Stop()
Deactivate the IO device. The instance still exists and can be started again with Start. If Start was never called or Stop has already been called, it returns idempotently. Failures write LastError; no exception is thrown.
Not Dispose(): Stop does not end the SDK session.
Example:
device.Stop();
HighResolutionTimer
public bool HighResolutionTimer { get; set; }
1ms The high-resolution timer is held by the service process. Write = Notify the local service process to power-on/off(POST /api/high-resolution-timer); Read = Values set by the service for this session, not the actual system value (the 1ms timer enabled in the server configuration will not be read by this property). It is disabled by substitution value in the installation package.
Example:
device.HighResolutionTimer = true;
After opening, wait for greater accuracy; the mouse may become sluggish. On non-Windows systems, this operation has no effect.
Operating State
State
Operational ST (Read-Only). Start / Stop Only enable / disable IO devices; this property is always read exactly as it appears on-site. If the actual ST cannot be read Unknown, do not interpret it as None.
public PnSState State { get; }
Example:
if (device.State == PnSState.DataExchange)
Console.WriteLine("周期数据交换正常");
WaitReason
public string WaitReason { get; }
Reason for this session's wait. Empty when the IO controller is connected. In the if (!device.Connected) loop, read this property or LastServiceError.
Example:
if (!device.Connected)
Console.WriteLine(device.WaitReason);
EverConnected
public bool EverConnected { get; }
Has a link ever been established with the IO controller? false = Never connected; true and Connected == false = Disconnected. This determination remains in effect even after the link is lost.
Example:
if (!device.Connected && device.EverConnected)
Console.WriteLine("掉线");
Error Code
ErrorCode
public PnSRuntimeErrorCode ErrorCode { get; }
Last runtime fault (enumeration). Unrecognized value Unknown. The original value is still passed ErrorCodeRaw through. Use for Chinese messages ErrorMessage.
Example:
PnSRuntimeErrorCode err = device.ErrorCode;
string text = device.ErrorMessage;
int raw = device.ErrorCodeRaw;
ErrorCodeRaw
public int ErrorCodeRaw { get; }
Original value of the runtime fault (0 = no fault). Unrecognized values are still passed through this property.
Example:
int raw = device.ErrorCodeRaw;
ErrorMessage
public string ErrorMessage { get; }
Chinese fault messages. Return a description with the original value for unrecognized codes; do not discard the information.
Example:
Console.WriteLine(device.ErrorMessage);
LastServiceError
public string LastServiceError { get; }
The most recent failed script for this session (sticky). The success path is not cleared. If this if (!Connected) property is empty during a loop, read it WaitReason.
Example:
Console.WriteLine(device.LastServiceError);
Diagnosis
Diagnostics
public PnSDiagnostics Diagnostics { get; }
Diagnostic subobjects (Snapshot / IOPS / Alert / Log). Snapshot / IoStatus / Alarms / AlarmHistory / Records Contains the same data as the flat attributes below.
Example:
var snap = device.Diagnostics.Snapshot;
foreach (var a in device.Diagnostics.Alarms)
Console.WriteLine(a.Message);
DiagSnapshot
public PnSDiagSnapshot DiagSnapshot { get; }
Retrieve the link, frame count, watchdog, and jitter in a single operation. If a value cannot be read Ok == false, read it again Message; this is not considered an exception.
Example:
PnSDiagSnapshot snap = device.DiagSnapshot;
if (!snap.Ok)
Console.WriteLine(snap.Message);
IoStatus
public PnSIoStatus IoStatus { get; }
IOPS / IOCS Snapshot. 0x80 = GOOD, 0x00 = BAD. If the link is lost, all values are set to BAD. If the data cannot be read, return PnSIoStatus.Empty.
Example:
if (!device.IoStatus.AllGood)
Console.WriteLine(device.IoStatus.ToString());
Complete Example
using Darra.PnS;
using (var device = new DarraPnS())
{
PnSErrorCode rc = device.Start();
if (rc != PnSErrorCode.Success)
return;
while (running)
{
if (!device.Connected)
{
Console.WriteLine(device.State);
Console.WriteLine(device.WaitReason);
Console.WriteLine(device.ErrorMessage);
continue;
}
device.ProcessData.Out[0].SetBit(0, true);
bool fromPlc = device.ProcessData.In[0].AsBool;
}
device.Stop();
}