Skip to main content

Attributes and State Machines

C has no attributes. Status and error codes use free functions. Check status with the static pns_*; no need to call Connect. Write Q / read I: PnSServiceConnectRealtime → PnSServiceStart. Whether the IO controller has completed a cycle exchange: see PnSServiceIsConnected.

Properties​

Static items can be queried without a session. If the service is not up, write pns_last_error. The sticky message is not cleared on the success path.

CategoryPropertyTypeAccessDescription
Staticpns_is_service_readyboolread-onlyIs the local service ready?
pns_last_errorconst char *read-onlyThe most recent failure message for the entire app (sticky; not cleared upon success)
pns_last_error_codepns_result_tread-onlyThe fault code from the most recent failed call for the entire application. If no failures have occurred, this is set to PNS_OK
pns_service_connectedboolread-onlyWhether the IO controller is performing cyclic exchange. If the service is not started, this is false.
pns_service_statepns_device_state_tread-onlyOperational ST. If the service is not RUN = UNKNOWN
pns_service_wait_reasonconst char *read-onlyReason for the wait. Service not started = 'Service not ready'
pns_service_status_messageconst char *read-onlyST message. If the service is not RUN = 'Service not ready'
Connections and SessionsPnSServiceIsConnectedboolread-onlyWhether the IO controller has established cyclic data exchange. Only true means cyclic data is being exchanged.
PnSServiceIsAttachedboolread-onlyWhether this SDK session is connected to the local service. 'Stop' does not change this value
PnSServiceIsStartedboolread-onlyWhether the IO device is enabled (after Start, before Stop)
Operational STPnSServiceGetStatepns_device_state_tread-onlyOperational ST. If the actual ST cannot be read, it is UNKNOWN; do not treat this as STOP
PnSServiceGetStatuspns_result_tread-onlyRetrieves ST / connected / error_code in a single call
PnSServiceGetWaitReasonconst char *read-onlyReason for this session's wait. Empty when the IO controller is connected
PnSServiceGetEverConnectedboolread-onlyWhether a link to the IO controller has ever been established. true and Connected is false = disconnected
fault CodesPnSServiceGetErrorCodeintread-onlyThe raw value of the most recent runtime fault (0 = no fault; see pns_runtime_error_code_t for the value range; declared as an int in C).Unrecognized values are still passed through by this function
PnSServiceGetErrorMessageconst char *read-onlyRun-time fault message in Chinese
pns_runtime_error_messageconst char *read-onlyRuntime fault original value → Chinese. Unrecognized value with original value
PnSServiceLastErrorconst char *read-onlyThe string of the most recent failure in this session (sticky; not cleared upon success)
AlertsPnSServiceGetAlarmCountintread-onlyTotal number of active alarms. Same GetAlarms as
PnSServiceGetCriticalAlarmCountintread-onlyNumber of critical alarms
PnSServiceGetAlarmspns_result_tread-onlyActive alarms.
PnSServiceGetAlarmHistorypns_result_tread-onlyHistorical alarms (last 500 entries).
Diagnostic and Process DataPnSServiceOnConnectedChangedpns_result_tWrite-onlyRegister a callback for the local RUN ST.
PnSServiceGetDiagpns_result_tread-onlyRetrieves link, frame count, watchdog, and jitter in a single call.Returns 0 if the data cannot be read
PnSServiceGetIoStatuspns_result_tread-onlyIOPS / IOCS Snapshot.0x80 = GOOD,0x00 = BAD. If the link is lost, all values are BAD
PnSProcessDataInputsMapping / PnSProcessDataOutputsMappingpns_result_tRead/WriteMap once to get the I / Q views + length (I is read-only, Q is writable; use PnSProcessDataInputsMappingRO for the read-only view of I). Modifying a field in the Q view is a write, sent out in the next cycle.
PnSProcessDataWriteStruct / PnSProcessDataReadStructpns_result_tRead/WriteSnapshot: Write Q / Read I.
if (!pns_is_service_ready())
printf("%s (%d)\n", pns_last_error(), (int)pns_last_error_code());

pns_result_t​

Return codes. All functions SDK return values from this enumeration; use pre-conditions such as PNS_OK: 0 = success, negative value = failure.

typedef enum pns_result
{
PNS_OK = 0, /* Success */
PNS_ERR_INVALID_PARAM = -1, /* Invalid parameter (null pointer / out-of-bounds / invalid length) */
PNS_ERR_NO_STACK = -2, /* The service has not been started or is not connected (please start it first) */
PNS_ERR_NO_CONNECTION = -3, /* No link AR established (controller not connected) */
PNS_ERR_TIMEOUT = -4, /* Operation timed out */
PNS_ERR_NOT_SUPPORTED = -5, /* Function not supported (e.g., IRT / RTC3) */
PNS_ERR_BUSY = -6, /* Busy with service (e.g., waiting for an alert ACK); if the real-time link is already in use by another SDK session, it also returns this code. */
PNS_ERR_NATIVE = -7, /* Failed to run. For details, see PnSGetLastError() */
PNS_ERR_DLL_LOAD = -8, /* Failed to load the runtime library */
PNS_ERR_ENTRY_MISSING = -9, /* Entry symbol not found */
PNS_ERR_CONFIG = -10, /* Invalid configuration */
PNS_ERR_MEMORY = -11, /* Memory allocation failed */
PNS_ERR_NOT_FOUND = -12 /* The target does not exist. */
} pns_result_t;

pns_device_state_t​

Equipment Operating ST (pns_service_state / PnSServiceGetState() Back).

typedef enum pns_device_state
{
PNS_DEV_STATE_UNKNOWN = 0, /* Unknown / Unable to read the actual ST */
PNS_DEV_STATE_STOPPED = 1, /* STOP (Service is ready, but not enabled IO) */
PNS_DEV_STATE_IDLE = 2, /* RUN, waiting for controller link (not established AR) */
PNS_DEV_STATE_CONNECTING = 3, /* Connecting... */
PNS_DEV_STATE_DATA = 4, /* Data Exchange (Established AR; periodic data transmission and reception in progress) */
PNS_DEV_STATE_ABORTED = 5, /* link terminated */
PNS_DEV_STATE_APP_READY = 6, /* Application Ready (APPLRDY, parameter exchange complete, before periodic data exchange) */
PNS_DEV_STATE_CONFIG_ERROR = 7, /* Configuration error degradation states (e.g., missing network card, device unavailable) */
PNS_DEV_STATE_PARAMETERIZED = 8 /* Parameterization complete (PRMEND, waiting for the application to be ready) */
} pns_device_state_t;

Only PNS_DEV_STATE_DATA indicates that periodic data is being exchanged. PNS_DEV_STATE_UNKNOWN If the actual ST cannot be read (e.g., the service is not ready), do not interpret this as "not initialized."

pns_runtime_error_code_t​

Equipment operation fault codes (enumerated versions of the raw fault values returned by ST queries) should not pns_result_t be confused with .

typedef enum pns_runtime_error_code
{
PNS_RUNTIME_ERR_OK = 0, /* No errors */
PNS_RUNTIME_ERR_INIT = -1, /* Initialization Failed */
PNS_RUNTIME_ERR_CFG = -2, /* Invalid configuration */
PNS_RUNTIME_ERR_THREAD = -3, /* Cycle Exchange Startup Failed */
PNS_RUNTIME_ERR_RUNNING = -4, /* Invalid ST (Repeated Start/Stop) */
PNS_RUNTIME_ERR_PDI = -5, /* Process Data Exchange Failed */
PNS_RUNTIME_ERR_DAP = -6, /* DAP (Slot 0) Insertion failed */
PNS_RUNTIME_ERR_NULL = -8, /* Empty handle (IO Device not started) */
PNS_RUNTIME_ERR_DIAG = -9, /* Invalid diagnostic parameter */
PNS_RUNTIME_ERR_WIN32 = 1, /* Win32 System fault code (e.g., 2048) */
PNS_RUNTIME_ERR_UNKNOWN = -10000 /* Unrecognized value */
} pns_runtime_error_code_t;

Unrecognized values are returned as-is PNS_RUNTIME_ERR_UNKNOWN, while the original values are passed through PnSServiceGetErrorCode() exactly as they are.

Start-Stop​

PnSServiceStart()​

pns_result_t PnSServiceStart(pns_service_handle_t session);

Enable the IO device. If Start has already been called and Stop has not, returns idempotently with PNS_OK. This does not mean the IO controller is exchanging data on a cycle basis; whether the controller is currently exchanging data is shown by PnSServiceIsConnected.

Return value:

  • PNS_OK success; invalid handle returns PNS_ERR_INVALID_PARAM; start failure returns PNS_ERR_NATIVE; unreachable service returns PNS_ERR_NO_CONNECTION

On failure, write to PnSServiceLastError.

Example:

pns_result_t rc = PnSServiceStart(session);
if (rc != PNS_OK)
printf("%s\n", PnSServiceLastError(session));

PnSServiceStop()​

pns_result_t PnSServiceStop(pns_service_handle_t session);

Deactivate the IO device. The handle still exists and can be started again with Start. If Start has not been called or Stop has already been called, it returns idempotently. Failures write LastError.

Not PnSServiceClose(): Stop does not end the SDK session.

Example:

PnSServiceStop(session);

1ms High-resolution timer (Service Side)​

1ms High-resolution timer switch is in service Web API: POST /api/high-resolution-timer (body {"highResolutionTimer": true|false}).C SDK This switch is not provided. The installation package is not opened by substitution value.

note

After opening, wait for greater accuracy; the mouse may become sluggish. On non-Windows systems, this operation has no effect.

Operating State​

PnSServiceGetState()​

Operational ST (Read-Only). Start / Stop Only enable / disable IO devices; this function always reads the actual ST on-site. If the true ST cannot be read UNKNOWN, do not interpret it as STOPPED.

pns_device_state_t PnSServiceGetState(pns_service_handle_t session);

Example:

if (PnSServiceGetState(session) == PNS_DEV_STATE_DATA)
printf("周期数据交换正常\n");

PnSServiceGetStatus()​

pns_result_t PnSServiceGetStatus(
pns_service_handle_t session,
pns_device_state_t * p_state,
bool * p_connected,
int * p_error_code);

Retrieves the ST, whether cycle data has been exchanged, and the original value of the runtime fault in a single operation. p_connected / p_error_code Can be NULL.

pns_device_state_t state = PNS_DEV_STATE_UNKNOWN;
bool connected = false;
int err = 0;
PnSServiceGetStatus(session, &state, &connected, &err);
if (!connected)
printf("%s\n", PnSServiceGetWaitReason(session));

PnSServiceGetWaitReason()​

const char * PnSServiceGetWaitReason(pns_service_handle_t session);

Reason for this session's wait. Empty when the IO controller is connected. In the if (!PnSServiceIsConnected(session)) loop, read this function or PnSServiceLastError.

Example:

if (!PnSServiceIsConnected(session))
printf("%s\n", PnSServiceGetWaitReason(session));

PnSServiceGetEverConnected()​

bool PnSServiceGetEverConnected(pns_service_handle_t session);

Has a link ever been established with the IO controller? false = Never connected; true and not connected = Disconnected. This determination remains in effect even after the link is lost.

Example:

if (!PnSServiceIsConnected(session) && PnSServiceGetEverConnected(session))
printf("掉线\n");

Error Code​

PnSServiceGetErrorCode()​

int PnSServiceGetErrorCode(pns_service_handle_t session);

The original value from the most recent runtime fault (0 = no fault). Unrecognized values are still passed through by this function. Use for Chinese messages PnSServiceGetErrorMessage.

Example:

int raw = PnSServiceGetErrorCode(session);
const char * text = PnSServiceGetErrorMessage(session);

PnSServiceGetErrorMessage()​

const char * PnSServiceGetErrorMessage(pns_service_handle_t session);

Chinese fault messages. Return a description with the original value for unrecognized codes; do not discard the information.

Example:

printf("%s\n", PnSServiceGetErrorMessage(session));

PnSServiceLastError()​

const char * PnSServiceLastError(pns_service_handle_t session);

The most recent failure message for this session (sticky). Do not clear the success path. Fall back NULL when the handle is pns_last_error. If this if (!IsConnected) function is empty during loop , read GetWaitReason.

Example:

printf("%s\n", PnSServiceLastError(session));

Diagnosis​

PnSServiceGetDiag()​

pns_result_t PnSServiceGetDiag(
pns_service_handle_t session,
pns_service_diag_t * p_diag);

Retrieve the link, frame count, watchdog, and jitter in a single operation. If a value cannot be read ok == 0, read it again message; this is not considered an exception.

Example:

pns_service_diag_t snap;
if (PnSServiceGetDiag(session, &snap) == PNS_OK && !snap.ok)
printf("%s\n", snap.message);

PnSServiceGetIoStatus()​

pns_result_t PnSServiceGetIoStatus(
pns_service_handle_t session,
pns_service_io_status_t * p_status,
uint16_t * p_input_iops_len, uint8_t * p_input_iops,
uint16_t * p_input_iocs_len, uint8_t * p_input_iocs,
uint16_t * p_output_iops_len, uint8_t * p_output_iops,
uint16_t * p_output_iocs_len, uint8_t * p_output_iocs);

IOPS / IOCS Snapshot. 0x80 = GOOD, 0x00 = BAD. If the link drops, mark it all as BAD.

Example:

pns_service_io_status_t st;
if (PnSServiceGetIoStatus(session, &st,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == PNS_OK
&& !st.all_good)
printf("IOPS BAD %d\n", st.iops_bad_count);

Complete Example​

#include "pns.h"

int main(void)
{
pns_service_handle_t session = NULL;
bool from_plc = false;
int running = 1;

if (PnSServiceConnectRealtime(&session) != PNS_OK)
return 1;
if (PnSServiceStart(session) != PNS_OK)
return 1;

while (running)
{
if (!PnSServiceIsConnected(session))
{
printf("%d\n", (int)PnSServiceGetState(session));
printf("%s\n", PnSServiceGetWaitReason(session));
printf("%s\n", PnSServiceGetErrorMessage(session));
continue;
}

PnSServiceWriteBool(session, "Q0.0", true);
PnSServiceReadBool(session, "I0.0", &from_plc);
}

PnSServiceStop(session);
PnSServiceClose(session);
return 0;
}