Skip to main content

Process Data

Use PnSProcessDataInputsMappingRO / PnSProcessDataOutputsMapping to map the entire process data to a structure, and use PnSProcessDataIn* / PnSProcessDataOut*andPnSServiceReadBool similar operators—such as offset-based or address-based read/write operations—to access individual values.​

Process data refers to the bytes automatically exchanged between the controller and the device every cycle: write Q (device → controller) and read I (controller → device).The most convenient approach is to map this sequence of bytes to a single structure #pragma pack(push, 1) ; thereafter, reading or writing fields directly reads or writes the current process data without the need for copying or flushing: what is read is always the data for the current cycle, and what is written is transmitted in the next cycle.

Zone I is read-only: Writing to it returns a response PNS_ERR_NOT_SUPPORTED; data loss is not silently ignored. To write process data, use Zone Q—changes take effect immediately and are automatically transmitted in the next cycle, No commit calls.

Q Value (Single Value for the Entire Station)

After entering "Q," the controller will read the value you entered. "Q" represents the device output, with the direction being device → controller. Changes take effect immediately and are automatically sent in the next cycle; no additional commit functions are required. The window length is determined *n by the returned value.

The entry point accessed directly by bytes pns_shm_i_payload / maps to the same data pns_shm_q_payload as the structure for reading and writing. All other entry points areCopy: Offset-based retrieval (PnSProcessDataInI16 a family), term-based retrieval (PnSProcessDataItemIn / Out + fixed-width read/write per term), and address-based retrieval (I0.0 / IW2 / QD4). The advantage of these methods is that they do not require creating a structure beforehand; the drawback is that each call involves copying that small segment of bytes and interpreting it in big-endian format.

Cycle-based updates occur automatically. The event stream reports operational status such as connection, state, link, and quality IO ; you can retrieve changes in process data at your own interval (PnSServiceRead / / PnSServiceGetIo mapping view). You determine the scheduling (loop, timer, or thread) yourself.

Structure Mapping Contract
  • Must #pragma pack(push, 1): Process data is an in-memory byte stream; it must not be aligned or padded.SDK Only provide a pointer and its length, Do not checkPackaging and Dimensions — Structures without a "pack" keyword appear misaligned but do not generate errors; use _Static_assert(sizeof(T) == 打包尺寸) to fix this at compile time.
  • If one of the three criteria is missing, the mapping is prohibited.: rc == PNS_OK && *p != NULL && *n >= sizeof(T). In case of failure *p = NULL, *n = 0 the old pointer is not retained.
  • Multibyte values are big-endian on the network.(PROFINET Network Sequence): The data mapped here consists of bytes from the network. If you treat the int16_t / int32_t / float fields as host values and use them directly, the order will be reversed—you must use the typed interface that accepts host values by offset (which decodes them in big-endian format for you) or parse the byte fields.
  • Direction: Zone Q (PnSProcessDataOutputsMapping) is writable; Zone I (PnSProcessDataInputsMappingRO) is read-only.
  • The window is based *n on the returned value.: Do not hardcode offsets yourself, and do not store the byte offsets you calculate in the mapping cache across calls—to ensure consistent offset positioning, use the "by offset" or "by address" ports.
  • The mapping starts at the 0th byte of the data: To map the middle section, place a placeholder array (uint8_t _skip[k];) at the beginning of the structure; or use byte-by-byte access PnSServiceReadArea based on an offset.
  • Life Cycle: The mapping remains valid for the duration of the session andPnSServiceClose expires afterward; Stop refreshing is paused but the mapping is not removed (so you can still retrieve the old values), but Stop afterwardNewAll mapping calls must be PNS_ERR_NO_STACK.
  • Q-zone mapping has a single owner: reads are concurrent, Note: The caller must perform serialization on their own.
Input/Output and I/Q are inverses of each other

The 20-byte Input in GSDML (device → controller) is the SDK's Q; the 32-byte Output in GSDML (controller → device) is the SDK's I.

The device is a fixed single module (Slot 1 / Sub-slot 1); the C API function signature has no slot / subslot parameters — the slot / subslot fields in pns_service_address_t are always 1, and the caller does not need to fill them in.

Use Cases​

Everyday Use​

① Structure mapping (ongoing use): Map once upon entering the data exchange; thereafter, when the variable is read or written.

#include "pns.h"

#pragma pack(push, 1)
typedef struct { uint8_t present; int16_t recipe; uint8_t flag; } from_plc_t; /* I: Controller → Device */
typedef struct { uint8_t ready; uint8_t busy; } to_plc_t; /* Q: Equipment → Controller */
#pragma pack(pop)

_Static_assert(sizeof(from_plc_t) == 4, "缺 #pragma pack(push, 1)");
_Static_assert(sizeof(to_plc_t) == 2, "缺 #pragma pack(push, 1)");

pns_service_handle_t session = NULL;
if (PnSServiceConnectRealtime(&session) != PNS_OK) return 1; /* Real-time Attachment (Occupies a Single Port) */
if (PnSServiceStart(session) != PNS_OK)
{ PnSServiceClose(session); return 1; } /* Enable IO Device */

const uint8_t *i = NULL; volatile uint8_t *q = NULL; size_t ni = 0, nq = 0;
if (PnSProcessDataInputsMappingRO(session, &i, &ni) != PNS_OK || i == NULL || ni < sizeof(from_plc_t))
{ PnSServiceClose(session); return 1; } /* I: Read-Only Mapping */
if (PnSProcessDataOutputsMapping(session, &q, &nq) != PNS_OK || q == NULL || nq < sizeof(to_plc_t))
{ PnSServiceClose(session); return 1; } /* Q: Writable mappings */

const from_plc_t *inp = (const from_plc_t *)i; /* Map it once, then use it as a variable */
to_plc_t *outp = (to_plc_t *)q;

outp->ready = 1; /* Save it to be sent in the next cycle; no need to submit a call. */
uint8_t present = inp->present; /* The value read is always from the current cycle */

If a field width is greater than 1 byte and you want the host value, do not read the struct field directly (since it’s in big-endian order); instead, use the typed interface based on offsets:

int16_t recipe = 0;
if (PnSProcessDataInI16(session, 1, &recipe) == PNS_OK) /* Offset 1: Decode into the host using big-endian format int16 */
printf("recipe=%d\n", recipe);

② Single-read (one-time retrieval): Do you need the entire byte sequence, or just one field?

uint8_t buf[64]; uint16_t len = sizeof(buf);
PnSServiceRead(session, buf, sizeof(buf), &len); /* I. Entire passage: Read in one go; self-consistent within a single beat */

int16_t recipe = 0;
PnSProcessDataInI16(session, 1, &recipe); /* I: Offset 1, int16big-endian */

pns_process_data_item_t item = PnSProcessDataItemIn(session, 0); /* See ④: Index (subscript) format */
uint16_t v = 0;
PnSProcessDataItemReadU16(&item, &v); /* Read 2 bytes from the top, big-endian */

String fields are represented aschar fixed-length byte arrays in process data and generally do not end with a 0:

typedef struct { char part_no[16]; } from_plc_text_t;   /* Place the definition inside #pragma pack(push, 1) */
const from_plc_text_t *txt = (const from_plc_text_t *)i; /* i Same as ①; the same mapping */

char part_no[17];
memcpy(part_no, txt->part_no, 16);
part_no[16] = '\0';

③ Read and write by address (write according to the addresses specified in the engineering software): Region letter + byte offset + bit.

bool done = false;
PnSServiceReadBool(session, "I0.0", &done); /* Read: Controller → Device */
int16_t recipe = 0;
PnSServiceReadInt16(session, "IW2", &recipe); /* Read word: big-endian */
int32_t counter = 0;
PnSServiceReadInt32(session, "ID4", &counter); /* Read double word: big-endian */

PnSServiceWriteBool(session, "Q0.0", true); /* Write a bit (read, modify, or write other bits in the same byte) */
PnSServiceWriteInt16(session, "QW2", 123); /* Write word: big-endian */
PnSServiceWriteInt32(session, "QD4", 10000); /* Write double word: big-endian */

④ Retrieving elements by subscript (item form): Term = session + offset + direction; a scalar of fixed width at the same coordinate. The term isCopy, not a view.

pns_process_data_item_t in_item  = PnSProcessDataItemIn(session, 1);   /* Read-Only Items in Zone I */
pns_process_data_item_t out_item = PnSProcessDataItemOut(session, 0); /* Q: Writable Items */

int16_t recipe = 0;
if (PnSProcessDataItemReadI16(&in_item, &recipe) == PNS_OK) /* Big-Endian Decoding */
printf("recipe=%d\n", recipe);

if (PnSProcessDataItemWriteU16(&out_item, 123) == PNS_OK) /* Takes effect immediately upon completion; will be sent out in the next cycle. */
printf("已写入 Q 偏移 0\n");

if (PnSProcessDataItemWriteU16(&in_item, 1) == PNS_ERR_NOT_SUPPORTED) /* Item I cannot be filled in; please report the error as is. */
printf("I 区只读\n");

Cyclical Changes​

Just put the same mapping into your own loop and use it directly (it updates automatically every cycle, so whatever you read is the current shot).

while (running) {                                /* Your own recurring tasks—set your own schedule */
outp->ready = (uint8_t)(inp->present + 1); /* Read the current beat → Calculate → Write down the beat and play it */
Sleep(10); /* Bring your own while waiting between shots: Sleep from <windows.h>; this SDK header is not included. */
}

Retrieving the data once outside the loop is still the recommended approach: it eliminates the need to parse the data with every iteration, automatically updates the process data each cycle, and requires no additional calls. The costs must be made clear: Use the returned value as the *n basis for mapping the window; do not hard-code offsets, and do not cache the byte offsets you calculate yourself across mapping operations—if the offset is incorrect, what you read will beMisaligned Bytes, no exceptions are thrown; *n >= sizeof(T) even a barrier of this size can’t stop it. Therefore, self-checking the valid range of values remains your responsibility; to locate a stable byte, use the “by offset” or “by address” port. Stop Backward mapping is still readable (you can read back the old value), PnSServiceClose but backward mapping is disabled.

The offset, per-element, and per-address access modes retrieve the current value directly on every call, soMultiple reads of the same byte within a single cycle may occur in two different bus cycles.. To ensure consistency across multiple fields within a single frame (for example, "the present and recipe values for this frame must come from the same frame"), PnSServiceRead take a snapshot of the entire segment, or directly read the structure mapping from ① (the structure mapping always returns the current value upon each read, so there is no need to retrieve it again).

Structure Mapping​

PnSProcessDataInputsMappingRO()​

static inline pns_result_t PnSProcessDataInputsMappingRO(
pns_service_handle_t session,
const uint8_t ** p,
size_t * n);

The mapping in Zone I (Controller → Device) has an output parameter of const uint8_t ** — which effectively blocks the write port. This is the recommended mapping entry point for Zone I.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • PNS_ERR_INVALID_PARAM — p or n for NULL
  • PNS_ERR_NO_STACK — Session has not started or has been stopped. Message: 服务未 Start 或已 Stop,请先 PnSServiceStart
  • PNS_ERR_NATIVE — Process data is unavailable or empty
  • When a failure occurs *p = NULL, *n = 0 it is handled realistically; the old pointer from the previous call is not passed back to the caller.
  • *n = Effective Length = min(真长, 可用范围): True Length = True Length of Zone I (GET /api/io the field name is opposite to the I/Q direction outputLength; do not judge by the name alone); if unknown, use the available range, Do not extrapolate based on district capacity; If even one of the three criteria is missing, do not use it as a structure.
  • The window should be based *n on the returned value: do not store it as a fixed offset for reuse, and do not cache the byte offsets you calculate across multiple operations—to ensure consistent offset positioning, use the "by offset" or "by address" modes.
  • Mapping remains valid for the duration of the lifetime: PnSServiceClose Expires afterward; Stop only stops refreshing—the mapping is not removed—but afterward StopNewMapping Call Returns PNS_ERR_NO_STACK(Old Mappings Are Still Readable)
  • Maps online bytes to network big-endian; readable by any thread

Example:

const uint8_t *i = NULL;
size_t n = 0;
pns_result_t rc = PnSProcessDataInputsMappingRO(session, &i, &n);

if (rc == PNS_OK && i != NULL && n >= sizeof(from_plc_t)) {
const from_plc_t *inp = (const from_plc_t *)i; /* Map it once, then use it as a variable */
printf("present=%u\n", inp->present); /* The value read is always from the current cycle */
} else {
printf("映射未就绪: rc=%d (%s)\n", (int)rc, PnSServiceLastError(session));
}

PnSProcessDataOutputsMapping()​

pns_result_t PnSProcessDataOutputsMapping(
pns_service_handle_t session,
volatile uint8_t ** p,
size_t * n);

Mapping in Zone Q (Device → Controller): Modifying the byte being pointed *p to writes it to Q, and it is automatically transmitted in the next cycle without requiring any commit calls. This is the write direction for process data.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • PNS_ERR_INVALID_PARAM — p or n for NULL
  • PNS_ERR_NO_STACK — Session has not started or has been stopped. Message: 服务未 Start 或已 Stop,请先 PnSServiceStart
  • PNS_ERR_NATIVE — Process data is unavailable, or the mapping itself failed. Text: 过程数据输出映像不可用
  • When you fail *p = NULL, *n = 0
  • The usable range matches the write range of PnSProcessDataWriteStruct PnSServiceWriteArea / byte-for-byte; *n = effective length = min(真长, 可用范围)(true length = Q-area true length = GET /api/io inputLength, where the field names are in the opposite direction of I/Q—do not rely on the names; use the usable range when unknown), and do not extrapolate beyond the area capacity.
  • Concurrency Contract: Reads are concurrent; for writes, the caller must handle serialization.; Only one writable mapping is allowed at a time
  • float Before writing multi-byte fields to the host int16_t / int32_t / , you must manually convert them to big-endian (or use PnSProcessDataWriteStruct / the PnSProcessDataOut* family based on offsets).

Example:

volatile uint8_t *q = NULL;
size_t n = 0;
pns_result_t rc = PnSProcessDataOutputsMapping(session, &q, &n);

if (rc == PNS_OK && q != NULL && n >= sizeof(to_plc_t)) {
to_plc_t *outp = (to_plc_t *)q;
outp->ready = 1; /* Write Q; transmit in the next cycle */
outp->busy = 0;
} else {
printf("映射未就绪: rc=%d (%s)\n", (int)rc, PnSServiceLastError(session));
}

PnSProcessDataInputsMapping()​

pns_result_t PnSProcessDataInputsMapping(
pns_service_handle_t session,
volatile uint8_t ** p,
size_t * n);

With the same PnSProcessDataInputsMappingRO data, the same length, and the same failure semantics, the only difference is the output parameter type volatile uint8_t **(this is an existing signature and remains unchanged). For read-only operations (decoding / checking / forwarding), please use the version RO ; to obtain a pointer volatile tois not equal to Zone I is writable — writing to it returns PNS_ERR_NOT_SUPPORTED.

Return value:

  • pns_result_t — Same as “item PnSProcessDataInputsMappingRO by item”

Note:

  • PNS_ERR_INVALID_PARAM / PNS_ERR_NO_STACK / PNS_ERR_NATIVE The criteria are the same as in the copy for RO version ; if it fails *p = NULL, *n = 0 ,
  • The pointers in both versions point to the same offset within the same block of data, and the criteria are the same: rc == PNS_OK && *p != NULL && *n >= sizeof(T)

Example:

volatile uint8_t *raw = NULL;
size_t n = 0;
if (PnSProcessDataInputsMapping(session, &raw, &n) == PNS_OK && raw != NULL && n >= 2) {
uint16_t sw = (uint16_t)(((uint16_t)raw[0] << 8) | (uint16_t)raw[1]); /* Big-Endian Automatic Byte Decomposition */
printf("sw=0x%04X\n", sw);
}

pns_shm_i_payload() / pns_shm_q_payload()​

pns_result_t pns_shm_i_payload(pns_shm_map_t * map, volatile uint8_t ** p, size_t * n,
size_t payload_len);
pns_result_t pns_shm_q_payload(pns_shm_map_t * map, volatile uint8_t ** p, size_t * n,
size_t payload_len);

Data mapping on a direct-connect mapping handle has the same PnSProcessDataOutputsMapping semantics as PnSProcessDataInputsMappingRO / — reading and writing the same data segment — but does not go through a service session.The criterion is based solely on *n. The last parameter payload_len = the actual length in that direction (for Zone I, take GET /api/io from outputLength; for Zone Q, take inputLength; field names are opposite to those in the I/Q directions—do not rely on the names): *n = valid length = ; min(payload_len, 可用范围)0 = unknown. Use the available range; do not extrapolate beyond the zone capacity.

Return value:

  • pns_result_t — PNS_OK Success; Failure (unmapped / data is empty) *p = NULL``*n = 0, in which case mapping is prohibited

Note:

  • pns_shm_i_payload Data is supplied to Zone I (read-only mode; the controller overwrites the entire segment every cycle); pns_shm_q_payload data is supplied to Zone Q (write mode; data is automatically transmitted in the next cycle after writing, without requiring a commit call).
  • Q: The usable range on the side is the same as the write port: It aligns with pns_shm_write_to_plc the write start point of .
  • *n = Effective Length = min(payload_len, 可用范围)(payload_len = True length in that direction; 0 = Unknown, based on available range); same value *p_len as pns_shm_read_from_plc return
  • *p Valid until pns_shm_unmap
  • Please map the structure according to this view — the mapping is based on online bytes (PROFINET big-endian); multi-byte fields will be reversed when int short used with the host.

Example:

#pragma pack(push, 1)
typedef struct { uint8_t present; uint8_t flag; } from_plc_t;
typedef struct { uint8_t ready; uint8_t busy; } to_plc_t;
#pragma pack(pop)

volatile uint8_t *i = NULL, *q = NULL;
size_t ni = 0, nq = 0;
size_t image_i_len = 2, image_q_len = 2; /* I actual length =outputLength, Q actual length =inputLength (field name reversed from I/Q ), unknown: 0 */
if (pns_shm_i_payload(shm, &i, &ni, image_i_len) == PNS_OK && i != NULL && ni >= sizeof(from_plc_t)
&& pns_shm_q_payload(shm, &q, &nq, image_q_len) == PNS_OK && q != NULL && nq >= sizeof(to_plc_t)) {
const from_plc_t *inp = (const from_plc_t *)i;
to_plc_t *outp = (to_plc_t *)q;
outp->ready = inp->present ? 1 : 0;
}

Direct Access (Works Even Without Mapping)​

You can retrieve a single value without creating a mapping. The examples in this section... Each call copies a block of bytes. ..not a view—to achieve "automatic field conversion at the source," only the full mapping shown above is available.

The PnSProcessDataIn*() family (read I by offset)​

static inline pns_result_t PnSProcessDataInI16(
pns_service_handle_t session,
uint16_t offset,
int16_t * p_value);

In* Class: Reads a fixed-width scalar at byte offset offset in Zone I (controller → device); all multi-byte values areWeb PlatformDecoding. The offset refers to the byte offset, starting from the 0th byte of the data (0 = the 0th byte of the mapped view, with no +1 adjustment).

Return value:

  • pns_result_t — PNS_OK "Success" should be entered here *p_value; see below for other values

Note:

  • PNS_ERR_INVALID_PARAM — p_value For ; NULL out-of-bound (offset + 宽度 extends beyond Zone I, copy 区域越界: 偏移 %u + 长度 %u 超出过程映像区 %u 字节); session is NULL
  • PNS_ERR_NO_STACK — The session has not started or has been stopped
  • PNS_ERR_NATIVE — Process data unavailable (channel not ready)
  • When You Fail *p_valueDo not rewrite, it won't leave any partially solved values
  • No mandatory 2/4 alignment: offset This refers to byte offsets; an offset of 3 is valid I16 (at the cost of crossing two byte boundaries).
  • This isCopy: Each call retrieves the current value; the return value is the value itself, not a pointer.

Example:

int16_t recipe = 0;
if (PnSProcessDataInI16(session, 1, &recipe) == PNS_OK)
printf("recipe=%d\n", recipe); /* Offset 1, starting at 2 bytes, big-endian */
else
printf("读失败: %s\n", PnSServiceLastError(session));

Related Structures:

/* Read I (Controller → Device): All static inline, taken verbatim from pns_process_data.h */
PnSProcessDataInI8 (session, offset, int8_t * v); /* 1 byte, signed */
PnSProcessDataInU8 (session, offset, uint8_t * v); /* 1 byte, unsigned */
PnSProcessDataInI16(session, offset, int16_t * v); /* 2 bytes, signed, big-endian */
PnSProcessDataInU16(session, offset, uint16_t * v); /* 2 bytes, unsigned, big-endian */
PnSProcessDataInI32(session, offset, int32_t * v); /* 4 bytes, signed, big-endian */
PnSProcessDataInU32(session, offset, uint32_t * v); /* 4 bytes, unsigned, big-endian */
PnSProcessDataInI64(session, offset, int64_t * v); /* 8 bytes, signed, big-endian */
PnSProcessDataInU64(session, offset, uint64_t * v); /* 8 bytes, unsigned, big-endian */
PnSProcessDataInF32(session, offset, float * v); /* 4 bytes IEEE754 big-endian */
PnSProcessDataInF64(session, offset, double * v); /* 8 bytes IEEE754 big-endian */
PnSProcessDataInBool(session, offset, bool * v); /* 1 byte; evaluates to true bit0 when:0x00=false, 0x01=true, 0x02/0x80=false */
PnSProcessDataInBit (session, offset, int bit, bool * v); /* The th bit of 1 byte, bits 0–7 (0 = LSB), out of bounds INVALID_PARAM */

The PnSProcessDataOut*() family (write Q by offset)​

static inline pns_result_t PnSProcessDataOutI16(
pns_service_handle_t session,
uint16_t offset,
int16_t value);

Out* A: Align values to a fixed widthbig-endianOnce compiled, write to offset the byte offset in Q-area (Device → Controller). A return indicates a successful write PNS_OK ; it will be automatically transmitted in the next cycle, No delayed submission, no submission calls.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • PNS_ERR_INVALID_PARAM — Out of bounds (offset + 宽度 beyond the usable range of Zone Q; PnSServiceLastError(session) use the specific text as is); session is NULL
  • PNS_ERR_NO_STACK — The session has not started or has been stopped
  • PNS_ERR_NATIVE — Process data is unavailable
  • Crossing the Line: A Brief Essay Without Silence: Not a single byte was written; report the actual result PNS_ERR_INVALID_PARAM—do not use "partial overwrite + report success."
  • PnSProcessDataOutBit Read-and-modify: First read back the same byte; modify only the target bit, and keep the remaining bits at their original values.
  • PnSProcessDataOutBool Write true = 0x01, false = 0x00(full bytes, and produce only standard values)

Example:

if (PnSProcessDataOutI16(session, 0, 123) == PNS_OK)   /* Q: 2 bytes starting at offset 0, big-endian */
printf("已提交\n");

PnSProcessDataOutBit(session, 2, 0, true); /* Q, bit 0 with an offset of 2: Change only this bit */

Related Structures:

/* Writing Q (device → controller): All static inline, taken verbatim from pns_process_data.h */
PnSProcessDataOutI8 (session, offset, int8_t v); /* 1 byte, signed */
PnSProcessDataOutU8 (session, offset, uint8_t v); /* 1 byte, unsigned */
PnSProcessDataOutI16(session, offset, int16_t v); /* 2 bytes, signed, big-endian */
PnSProcessDataOutU16(session, offset, uint16_t v); /* 2 bytes, unsigned, big-endian */
PnSProcessDataOutI32(session, offset, int32_t v); /* 4 bytes, signed, big-endian */
PnSProcessDataOutU32(session, offset, uint32_t v); /* 4 bytes, unsigned, big-endian */
PnSProcessDataOutI64(session, offset, int64_t v); /* 8 bytes, signed, big-endian */
PnSProcessDataOutU64(session, offset, uint64_t v); /* 8 bytes, unsigned, big-endian */
PnSProcessDataOutF32(session, offset, float v); /* 4 bytes IEEE754 big-endian */
PnSProcessDataOutF64(session, offset, double v); /* 8 bytes IEEE754 big-endian */
PnSProcessDataOutBool(session, offset, bool v); /* 1 byte Full-byte overwrite: true=0x01 / false=0x00 */
PnSProcessDataOutBit (session, offset, int bit, bool v); /* Reads and rewrites the bit (0–7) of the same byte; the remaining bits remain unchanged. */

PnSProcessDataCopyInputsTo() / PnSProcessDataCopyToOutputs()​

static inline pns_result_t PnSProcessDataCopyInputsTo(
pns_service_handle_t session,
uint8_t * dest,
uint16_t dest_len,
uint16_t * copied);
static inline pns_result_t PnSProcessDataCopyToOutputs(
pns_service_handle_t session,
const uint8_t * src,
uint16_t src_len,
uint16_t * written);

Entire Zone I → Your buffer (snapshot copy); Zone Q is written in short blocks starting at offset 0 (shorter than the entire zone is allowed). Both areSnapshot, not a mapping.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • CopyInputsTo: copied Set NULL → ; PNS_ERR_INVALID_PARAM``dest The function only checks NULL the length (*copied rewrites I with valid length = min(真长, 可用范围), returns PNS_OK); dest_len < 需要的长度 → PNS_ERR_INVALID_PARAM, *copied the required length has been rewritten, No half-frame copying
  • CopyToOutputs: written If NULL, src or NULL src_len == 0 → PNS_ERR_INVALID_PARAM(*written first cleared to 0); if successful, *written = src_len
  • The error messages for both lines are consistent: Session not started / Stopped → ; PNS_ERR_NO_STACK Channel not ready → PNS_ERR_NATIVE
  • The buffer copied out isYourMemory: Once copied, it does not change along with the bus, so it can be parsed gradually and shared across threads; this is the preferred source when "multiple fields must be consistent within the same snapshot."

Example:

uint8_t i_buf[64];
uint16_t copied = 0;
if (PnSProcessDataCopyInputsTo(session, i_buf, sizeof(i_buf), &copied) == PNS_OK)
printf("这一拍 %u 字节\n", copied);
else
printf("需要 %u 字节的缓冲\n", copied); /* When the buffer is too small, "copied" is the required length. */

uint8_t q_head[2] = { 0x01, 0x00 };
uint16_t written = 0;
PnSProcessDataCopyToOutputs(session, q_head, sizeof(q_head), &written); /* Short form: Q (first 2 bytes) */

PnSProcessDataReadStruct() / PnSProcessDataWriteStruct()​

static inline pns_result_t PnSProcessDataReadStruct(
pns_service_handle_t session,
void * dest,
uint16_t len);
static inline pns_result_t PnSProcessDataWriteStruct(
pns_service_handle_t session,
const void * src,
uint16_t len);

Copy the first len bytes of Zone I into a packing structure, or write a packing structure to the first len bytes of Zone Q. Snapshot copy, not a mirror — The "Struct" in the name refers to a "packaged struct," not a live view.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • dest / src for NULL, len == 0 → PNS_ERR_INVALID_PARAM
  • Codes for "Out of Bounds," "Not Started," and "Channel Not Ready" are grouped with the "Offset" category (区域越界: …, PNS_ERR_NO_STACK, PNS_ERR_NATIVE) along with their corresponding text descriptions.
  • What was copied out wasOnline Bytes: Multi-byte fields in the structure are still in big-endian format; to host the values, you must decode them yourself using big-endian decoding (or use the PnSProcessDataIn* family).
  • Write-side operations allow short writes that overwrite an entire segment: len the write can be shorter than the full length of the Q region, overwriting only the first len bytes.

Example:

#pragma pack(push, 1)
typedef struct { uint8_t present; int16_t recipe_be; uint8_t flag; } from_plc_t;
#pragma pack(pop)

from_plc_t snap;
if (PnSProcessDataReadStruct(session, &snap, (uint16_t)sizeof snap) == PNS_OK) {
const uint8_t *rb = (const uint8_t *)&snap.recipe_be;
int16_t recipe = (int16_t)(((uint16_t)rb[0] << 8) | (uint16_t)rb[1]); /* Decode in big-endian order on your own */
printf("present=%u recipe=%d\n", snap.present, recipe);
}

to_plc_t out = { 1, 0 };
PnSProcessDataWriteStruct(session, &out, (uint16_t)sizeof out); /* Overwrite the first 2 bytes of Q */

PnSProcessDataItemIn() / PnSProcessDataItemOut()​

static inline pns_process_data_item_t PnSProcessDataItemIn(pns_service_handle_t session, uint16_t offset);
static inline pns_process_data_item_t PnSProcessDataItemOut(pns_service_handle_t session, uint16_t offset);

Take an "item": bundle the three elements—(session, offset, direction)—into a single value. In An item is a read-only entry in Zone I (controller → device), Out and an item is a readable shadow + writable entry in Zone Q (device → controller). An item and PnSProcessDataInU16(session, offset, …) Same Coordinates: Byte 0 = byte 0 of the data; no +1 adjustment is made.

Return value:

  • pns_process_data_item_t — Items returned by value (session / offset / writable), No failure: The validity of parameters is not checked until actual read/write operations occur.

Note:

  • Retrieving an item does not perform any operations on the item itself IO, nor does it affect the session; session passing the item NULL allows it to be retrieved, and subsequent read and write operations return the item as-is. PNS_ERR_INVALID_PARAM
  • ItemNo holdingsData is buffered, but values are not cached: every read returns the current value at the "moment of the read," and every write takes effect immediately.
  • The item isCopyAn entry point is not a view (a persistent view consists only of the entire map).

Example:

pns_process_data_item_t item = PnSProcessDataItemIn(session, 1);   /* Zone I, offset 1, read-only */
uint16_t v = 0;
if (PnSProcessDataItemReadU16(&item, &v) == PNS_OK)
printf("v=%u\n", v);

Related Structures:

typedef struct pns_process_data_item
{
pns_service_handle_t session; /* Session handle (may be null NULL; reports errors accurately during read and write operations) */
uint16_t offset; /* Data byte offset: 0 = In[0] / Out[0] */
bool writable; /* false = In entry (read-only in Zone I) / true = Out entry (readable shadow in Zone Q + writable) */
} pns_process_data_item_t;

pns_process_data_item_read() / pns_process_data_item_write()​

static inline pns_result_t pns_process_data_item_read(const pns_process_data_item_t * item, uint16_t n, uint8_t * buf);
static inline pns_result_t pns_process_data_item_write(const pns_process_data_item_t * item, const uint8_t * buf, uint16_t n);

Read/write the entire segment on the item: Bytes n : In Read Item I, Out Read Item Q (the copy you recently wrote there, not the controller acknowledgment), writeOnly Out items. These are the only two ports in this family that operate in bytes (the rest are fixed-width scalars).

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • item / buf for NULL, n == 0 → PNS_ERR_INVALID_PARAM(evaluate the argument first, then check the session)
  • Right In ItemCall Write → PNS_ERR_NOT_SUPPORTED(Zone I is overwritten in its entirety by the controller every cycle; writing to it is invalid and does not return a silent success)
  • Out of bounds, Not Started, Channel Not Ready: Code and text share the same base zone (区域越界: … / PNS_ERR_NO_STACK / PNS_ERR_NATIVE)
  • Writes take effect immediately; a return value PNS_OK indicates a successful write, and the next cycle will automatically trigger the operation.
  • Simply read Q's shadow using the offset; when no entry is created, use pns_process_data_read_q(session, offset, n, buf)(which has the same pns_process_data_read_i form as , but with the region replaced by the output region; Out this is the rule for reading entries in reverse order).

Example:

uint8_t raw[2] = { 0 };
if (pns_process_data_item_read(&item, 2, raw) == PNS_OK)
printf("raw=0x%02X%02X\n", raw[0], raw[1]); /* Big-endian: The higher-order byte comes first */

if (pns_process_data_item_write(&out_item, raw, 2) == PNS_OK) /* "Out" Item: Submit on the spot */
printf("Q 偏移 0 起 2 字节已提交\n");
if (pns_process_data_item_write(&item, raw, 2) == PNS_ERR_NOT_SUPPORTED) /* In Item: I Zone Read-Only */
printf("In 项不可写\n");

The PnSProcessDataItemRead*() family​

static inline pns_result_t PnSProcessDataItemReadU16(
const pns_process_data_item_t * item,
uint16_t * p_value);

Read a scalar or multi-byte value based on the specified width. big-endianDecoding: In / Out Both can be read (Out the item reads the Q shadow; when read back immediately after writing, the value is the same).

Return value:

  • pns_result_t — PNS_OK "Success" should be entered here *p_value; see below for other values

Note:

  • item or p_value is NULL → PNS_ERR_INVALID_PARAM
  • Out of Bounds (offset + 宽度 Beyond the Directional Window) → PNS_ERR_INVALID_PARAM, Copy 区域越界: 偏移 %u + 长度 %u 超出过程映像区 %u 字节
  • Session not started / Stopped → ; PNS_ERR_NO_STACK Channel not ready → PNS_ERR_NATIVE
  • On failure, do not *p_value overwrite
  • ReadBoolBit0 Press bit0 True/False Evaluation: 0x00 → false, 0x01 → true, non-standard bytes 0x02 / 0x80 → false. To determine true or false based on the entire byte (0 / not 0), use PnSProcessDataItemReadU8 to retrieve the raw byte and compare it yourself.
  • ReadBit If bit is set to 0..7(0 = LSB), an out-of-bounds error will be returned before the session is initiated. PNS_ERR_INVALID_PARAM

Example:

int16_t recipe = 0;
if (PnSProcessDataItemReadI16(&item, &recipe) == PNS_OK)
printf("recipe=%d\n", recipe);

bool flag = false;
if (PnSProcessDataItemReadBoolBit0(&item, &flag) == PNS_OK)
printf("bit0=%s\n", flag ? "true" : "false"); /* 0x02 / is also 0x80 false */

bool b3 = false;
if (PnSProcessDataItemReadBit(&item, 8, &b3) == PNS_ERR_INVALID_PARAM)
printf("位号必须在 0..7\n");

Related Structures:

/* Read from Item (In Item Read I / Out Item Read Q Shadow): static inline, taken word for word from pns_process_data.h */
PnSProcessDataItemReadU8 (item, uint8_t * v); /* 1 byte, unsigned */
PnSProcessDataItemReadI8 (item, int8_t * v); /* 1 byte, signed */
PnSProcessDataItemReadU16(item, uint16_t * v); /* 2 bytes, unsigned, big-endian */
PnSProcessDataItemReadI16(item, int16_t * v); /* 2 bytes, signed, big-endian */
PnSProcessDataItemReadU32(item, uint32_t * v); /* 4 bytes, unsigned, big-endian */
PnSProcessDataItemReadI32(item, int32_t * v); /* 4 bytes, signed, big-endian */
PnSProcessDataItemReadU64(item, uint64_t * v); /* 8 bytes, unsigned, big-endian */
PnSProcessDataItemReadI64(item, int64_t * v); /* 8 bytes, signed, big-endian */
PnSProcessDataItemReadF32(item, float * v); /* 4 bytes IEEE754 big-endian */
PnSProcessDataItemReadF64(item, double * v); /* 8 bytes IEEE754 big-endian */
PnSProcessDataItemReadBoolBit0(item, bool * v); /* 1 byte bit0 Evaluates to true:0x00=false, 0x01=true, 0x02/0x80=false */
PnSProcessDataItemReadBit(item, int bit, bool * v); /* The th bit of 1 byte (0..7, where 0 = LSB) */

The PnSProcessDataItemWrite*() family​

static inline pns_result_t PnSProcessDataItemWriteU16(
const pns_process_data_item_t * item,
uint16_t value);

Write a scalar of a fixed width in the field; multi-bytebig-endianCoding, Only Out items: In The command always returns immediately PNS_ERR_NOT_SUPPORTED; it does not write a single byte. It takes effect as soon as it is executed and is automatically sent in the next cycle.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • In Item → PNS_ERR_NOT_SUPPORTED(Determine the direction before the collision; do not perform unnecessary reads)
  • item For NULL → PNS_ERR_INVALID_PARAM
  • Crossing the Line → PNS_ERR_INVALID_PARAM, use PnSServiceLastError(session) Read for the specific copy, Non-silent abbreviations
  • Session not started / Stopped → ; PNS_ERR_NO_STACK Channel not ready → PNS_ERR_NATIVE
  • WriteBoolBit0 It performs a full-byte overwrite and produces only the following standard values: true = 0x01, false = 0x00
  • WriteBit Read-and-rewrite: Only modify the target bit; the remaining bits in the same byte retain their original values; bit Out-of-bounds → PNS_ERR_INVALID_PARAM

Example:

if (PnSProcessDataItemWriteU16(&out_item, 123) == PNS_OK)
printf("已提交 Q 偏移 0\n");

uint16_t back = 0;
PnSProcessDataItemReadU16(&out_item, &back); /* Q Shadow Readback: Reading it aloud immediately after writing it down yields the same result */
printf("读回 %u\n", back);

if (PnSProcessDataItemWriteBit(&out_item, 3, true) == PNS_OK) /* Set only the 3rd bit; leave the rest unchanged. */
printf("已置第 3 位\n");

Related Structures:

/* Note (for "Out" items only; for "In" items → PNS_ERR_NOT_SUPPORTED): static inline, taken verbatim from pns_process_data.h */
PnSProcessDataItemWriteU8 (item, uint8_t v); /* 1 byte, unsigned */
PnSProcessDataItemWriteI8 (item, int8_t v); /* 1 byte, signed */
PnSProcessDataItemWriteU16(item, uint16_t v); /* 2 bytes, unsigned, big-endian */
PnSProcessDataItemWriteI16(item, int16_t v); /* 2 bytes, signed, big-endian */
PnSProcessDataItemWriteU32(item, uint32_t v); /* 4 bytes, unsigned, big-endian */
PnSProcessDataItemWriteI32(item, int32_t v); /* 4 bytes, signed, big-endian */
PnSProcessDataItemWriteU64(item, uint64_t v); /* 8 bytes, unsigned, big-endian */
PnSProcessDataItemWriteI64(item, int64_t v); /* 8 bytes, signed, big-endian */
PnSProcessDataItemWriteF32(item, float v); /* 4 bytes IEEE754 big-endian */
PnSProcessDataItemWriteF64(item, double v); /* 8 bytes IEEE754 big-endian */
PnSProcessDataItemWriteBoolBit0(item, bool v); /* 1 byte Full-byte overwrite: true=0x01 / false=0x00 */
PnSProcessDataItemWriteBit(item, int bit, bool v); /* Reads and rewrites the bit (0–7) of the same byte; the remaining bits remain unchanged. */

Read/Write by Address (Region + Byte + Bit)​

Use this group when you don't want to calculate the offset yourself and want to write the address exactly as it appears in the engineering software: Region Letter + Byte Offset + Bit.

How to Write an AddressWhat is it?Read/Write Port
I0.0 / Q0.0Bit address: Bit 0 of byte 0; the bit number must be between 0 and 7.PnSServiceReadBool / PnSServiceWriteBool
IB0 / QB0Byte (the type indicator may be omitted after the region:I0 synonymous IB0 with )PnSServiceReadBytes / PnSServiceWriteBytes
IW2 / QW2Word: 2 bytes, big-endianPnSServiceReadInt16 / PnSServiceWriteInt16
ID4 / QD4Double word: 4 bytes, big-endianPnSServiceReadInt32 / PnSServiceWriteInt32
  • Do not enforce 2/4 alignment: IW3 / ID2 Both are valid; the offset refers to a byte offset, and the system won't pad single-word or double-word values for you.
  • The address corresponds only to this one byte at that offset.: Index 0..7(0 = LSB), if you need to access a location that spans multiple bytes, add the byte offset yourself.
  • Does not support the M zone / DB zone: M0.0 / DB1.DBW0 Always returns a resolution failure PNS_ERR_INVALID_PARAM; no mapping or guessing is performed.
  • Section I: Write-only, no support: When writing to the I side, return a value PNS_ERR_NOT_SUPPORTED; do not silently discard the data.
  • Case-insensitive (i0.0 / is equivalent q1.2 ); leading spaces and tabs in addresses are ignored.
  • The offset is a decimal number with a maximum value of ; 0xFFFF if it exceeds this value, parsing fails.
  • When You FailRead the specific copy PnSServiceLastError(session)(For example 地址越界: 偏移 4 + 长度 2 超出过程映像区 20 字节): Error codes only distinguish broad categories; details such as out-of-bounds, syntax, and direction are specified in the text.
  • The readback for Zone Q isLocal shadow values(The one you entered recently is not the controller's acknowledgment); the reading in Section I is the current value sent by the controller.

PnSServiceParseAddress()​

pns_result_t PnSServiceParseAddress(
const char * text,
pns_service_address_t * p_out);

Parses only address strings; does not read or write data: Parses I0.0 / IB0 / IW2 / ID4 / Q… into a structured format consisting of region + offset + bits + width. Use this if you want your code to validate addresses first, or if you need to convert addresses to byte offsets.

Return value:

  • pns_result_t — PNS_OK Success (p_out already filled in); PNS_ERR_INVALID_PARAM Invalid address

Note:

  • PNS_ERR_INVALID_PARAM Trigger conditions: text / p_out is ; NULL empty string; DB starts with ; the region character is not I / Q(M also not supported); the bit number is not ; 0..7 the offset is not in decimal or exceeds ; 0xFFFF the bit address is missing an offset (e.g., I.0)
  • On success slot / subslot Always 1; bit -1 indicates non-bit addressing; length is the width; bit address = 1 (spans 1 byte); B / no-type character = 1; W = 2; D = 4
  • A successful parse does not guarantee that the data can be read: an out-of-bounds error is reported only when the data is actually read or written.

Example:

pns_service_address_t addr;
if (PnSServiceParseAddress("IW3", &addr) == PNS_OK) {
printf("area=%d offset=%u length=%d bit=%d\n",
(int)addr.area, addr.byte_offset, addr.length, addr.bit); /* Does not enforce 2-byte alignment */
}

Addresses outside the I/Q range (M/DB) are resolved and returned directly PNS_ERR_INVALID_PARAMwithout being mapped—this feature SDK only supports the I/Q range.

Related Structures:

typedef enum pns_service_area
{
PNS_SERVICE_AREA_UNKNOWN = 0,
PNS_SERVICE_AREA_INPUT = 1, /* Zone I: Read-only (Controller → Device) */
PNS_SERVICE_AREA_OUTPUT = 2, /* Section Q: Write only (Device -> Controller) */
PNS_SERVICE_AREA_MEMORY = 3, /* Zone M: Not supported (resolution failed, no mapping) */
PNS_SERVICE_AREA_DB = 4, /* DB District: Not supported (resolution failed, no mapping) */
} pns_service_area_t;

typedef struct pns_service_address
{
pns_service_area_t area; /* Region */
uint16_t slot; /* Bay Number (DB Number; I/Q/M Zone Fixed 1) */
uint16_t subslot; /* Sub-slot Number (Fixed 1) */
uint16_t byte_offset; /* Byte Offset */
int bit; /* Bit number (0–7); -1 = non-bit addressing */
int length; /* Data width (bit = 1-byte span, byte = 1, word = 2, double word = 4) */
} pns_service_address_t;

PnSServiceReadBool() / PnSServiceWriteBool()​

pns_result_t PnSServiceReadBool(
pns_service_handle_t session,
const char * address,
bool * p_value);
pns_result_t PnSServiceWriteBool(
pns_service_handle_t session,
const char * address,
bool value);

Read/Write bit address (I0.0 / Q0.0). Reading zone I returns the current value sent by the controller, while reading zone Q returns the local shadow value you most recently wrote.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • PNS_ERR_INVALID_PARAM — p_value For NULL(read); invalid address (including M / DB, out-of-bounds bit number, or missing bit number —— for example, if there is IB0 no bit number, a read operation will report an invalid parameter); out-of-bounds address (text 地址越界: 偏移 %u + 长度 %d 超出过程映像区 %u 字节)
  • PNS_ERR_NOT_SUPPORTED — Write on Side I (I0.0 this type)
  • PNS_ERR_NO_STACK — The session has not started or has been stopped
  • PNS_ERR_NATIVE — Process data channel not ready
  • Write isRead and Rewrite: First read the byte, flip only the target bit, then write the entire byte back, leaving the other bits in the byte unchanged.
  • When a read fails, the value is first *p_value set to , and the old value is falsenot retained.

Example:

bool done = false;
if (PnSServiceReadBool(session, "I0.0", &done) == PNS_OK)
printf("done=%d\n", done);

if (PnSServiceWriteBool(session, "Q0.0", true) == PNS_OK)
printf("Q0.0 已置位\n");
if (PnSServiceWriteBool(session, "I0.0", true) == PNS_ERR_NOT_SUPPORTED)
printf("I 区只读\n");

PnSServiceReadInt16() / PnSServiceWriteInt16()​

pns_result_t PnSServiceReadInt16(
pns_service_handle_t session,
const char * address,
int16_t * p_value);
pns_result_t PnSServiceWriteInt16(
pns_service_handle_t session,
const char * address,
int16_t value);

Read/write a 2-byte integer at the word address (IW2 / QW2), big-endian.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • The address must be W Form-based, not bit-addressable: IB0(1 byte) or I0.0(bit address) PNS_ERR_INVALID_PARAMisIW3 always valid (2-byte alignment is not required)
  • Out of Bounds (offset + 2 Beyond This Direction) → PNS_ERR_INVALID_PARAM, Copy 地址越界: 偏移 %u + 长度 %d 超出过程映像区 %u 字节
  • Write to Side I → ; PNS_ERR_NOT_SUPPORTED Not Started / Stopped → PNS_ERR_NO_STACK; Channel Not Ready → PNS_ERR_NATIVE
  • On a read failure, it is *p_value initially set to 0.
  • Write: Overwrite exactly 2 bytes, encoded in big-endian format (do not read, modify, or write locally)

Example:

int16_t recipe = 0;
if (PnSServiceReadInt16(session, "IW2", &recipe) == PNS_OK)
printf("recipe=%d\n", recipe);

PnSServiceWriteInt16(session, "QW2", 123); /* big-endian lands at Q offset 2 */

PnSServiceReadInt32() / PnSServiceWriteInt32()​

pns_result_t PnSServiceReadInt32(
pns_service_handle_t session,
const char * address,
int32_t * p_value);
pns_result_t PnSServiceWriteInt32(
pns_service_handle_t session,
const char * address,
int32_t value);

Read or write a 4-byte integer using a double word address (ID4 / QD4), big-endian.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • The address must be D Form-based, not bit-addressable: IW2 / I0.0 Always PNS_ERR_INVALID_PARAM; ID2 Valid (4-byte alignment is not required)
  • The error code is the same as the text: PnSServiceReadInt16(地址越界: … / PNS_ERR_NOT_SUPPORTED / PNS_ERR_NO_STACK / PNS_ERR_NATIVE)
  • On a read failure, it is *p_value initially set to 0.

Example:

int32_t counter = 0;
if (PnSServiceReadInt32(session, "ID4", &counter) == PNS_OK)
printf("counter=%d\n", counter);

PnSServiceWriteInt32(session, "QD4", 10000);

PnSServiceReadBytes() / PnSServiceWriteBytes()​

pns_result_t PnSServiceReadBytes(
pns_service_handle_t session,
const pns_service_address_t * p_addr,
uint8_t * p_data);
pns_result_t PnSServiceWriteBytes(
pns_service_handle_t session,
const pns_service_address_t * p_addr,
const uint8_t * p_data);

Read and write raw bytes based on the parsed address structure—this reads and writes the same set of bytes as the address-based I/O methods described above. You can perform batch checks on the results PnSServiceParseAddress yourself, or if the addresses are constructed manually, you can use them directly.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • Read: session / p_addr / p_data for NULL → ; PNS_ERR_INVALID_PARAM the area is not I / Q → ; PNS_ERR_INVALID_PARAM``length not in 1..4 → ; PNS_ERR_INVALID_PARAM out of bounds → PNS_ERR_INVALID_PARAM, copy 地址越界: 偏移 %u + 长度 %d 超出过程映像区 %u 字节
  • Read: Session not started → PNS_ERR_NO_STACK(Copy 服务未 Start 或已 Stop: …); Channel not ready → PNS_ERR_NATIVE
  • Write: Region is I → ; PNS_ERR_NOT_SUPPORTED Region is not Q → ; PNS_ERR_INVALID_PARAM Not started → ; PNS_ERR_NO_STACK``length Not in 1..4 → PNS_ERR_INVALID_PARAM
  • Write "Out of Bounds" ButtonAvailable RangeVerify, return PNS_ERR_INVALID_PARAM; read PnSServiceLastError(session) the specific copy aloud, Do not perform a silent abbreviation and pretend it was successful
  • Neither of these will determine the address semantics for you: p_addr The li directly area determines which side to read from and which side to write to; don't pns_service_address_t manually piece together li and then use area = PNS_SERVICE_AREA_MEMORY it.

Example:

pns_service_address_t addr;
uint8_t word[2] = { 0 };

if (PnSServiceParseAddress("QW2", &addr) == PNS_OK
&& PnSServiceReadBytes(session, &addr, word) == PNS_OK) /* Q Shadow Replay */
printf("QW2 当前值 = 0x%02X%02X\n", word[0], word[1]);

word[0] = 0x00; word[1] = 0x7B; /* 123, Big-Endian */
PnSServiceWriteBytes(session, &addr, word);

entry point to the entire district (full section / abbreviated)​

PnSServiceRead()​

pns_result_t PnSServiceRead(
pns_service_handle_t session,
uint8_t * p_data,
uint16_t cap,
uint16_t * p_len);

Read the entire section in Section I (Controller → Device) into your buffer—one call at a time. Consistent Within the Same FrameOne byte.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • p_len For NULL → PNS_ERR_INVALID_PARAM
  • p_data Pass NULL / cap Pass 0 Check length only: *p_len Write back the valid length (I = min(真长, 可用范围)), then return PNS_OK
  • Buffer too small → PNS_ERR_INVALID_PARAM and the length required for the *p_len write operation, No half-frame copying
  • Session not started / Stopped → ; PNS_ERR_NO_STACK Channel not ready → PNS_ERR_NATIVE
  • Length = Effective Length = min(真长, 可用范围)(True Length = True Length of GET /api/io Zone I = outputLength, where the field name is opposite to the I/Q direction; do not rely on the name; 0 = Unknown, based on the available range), which is the same as the mapped value *n ; the length is determined by the value negotiated by the PLC (IO controller) and is automatically matched after link, Don't be too rigid 20 / 32

Example:

uint8_t i_buf[64];
uint16_t n = sizeof(i_buf);
if (PnSServiceRead(session, i_buf, sizeof(i_buf), &n) == PNS_OK)
printf("这一拍 %u 字节\n", n);
else
printf("需要 %u 字节的缓冲\n", n); /* The length has been written back */

uint16_t need = 0;
PnSServiceRead(session, NULL, 0, &need); /* Check length only */

PnSServiceWrite()​

pns_result_t PnSServiceWrite(
pns_service_handle_t session,
const uint8_t * p_data,
uint16_t length);

Short write to Q region (device → controller) starting at offset 0: length Allows a write shorter than the entire region, covering only the first length bytes. A return of PNS_OK indicates a successful write, and the next cycle is triggered automatically.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • p_data For NULL or length == 0 → PNS_ERR_INVALID_PARAM(Invalid empty data)
  • Out of Bounds (length beyond the usable range of Zone Q) → PNS_ERR_INVALID_PARAM; for the specific string, see PnSServiceLastError(session) Read
  • Session not started / Stopped → ; PNS_ERR_NO_STACK Channel not ready → PNS_ERR_NATIVE
  • The abbreviation covers only the prefix, The fixed-length bytes at the end retain their original values(This does not mean a complete reset of the entire district.)

Example:

uint8_t q[2] = { 0x01, 0x00 };
if (PnSServiceWrite(session, q, sizeof(q)) == PNS_OK)
printf("Q 前 2 字节已提交\n");

PnSServiceReadArea()​

pns_result_t PnSServiceReadArea(
pns_service_handle_t session,
pns_service_area_t area,
uint16_t offset,
uint16_t length,
uint16_t * p_len, uint8_t * p_data);

PressRegion + Offset + LengthRead the entire segment: PNS_SERVICE_AREA_INPUT Read I, PNS_SERVICE_AREA_OUTPUT Read Q shadow. Use this when you don't know how long the data is and just want a segment from the middle.

Return value:

  • pns_result_t — PNS_OK Success*p_len = actual number of bytes; see below for other possible values

Note:

  • session / p_len / p_data For NULL → PNS_ERR_INVALID_PARAM
  • area Not PNS_SERVICE_AREA_INPUT / PNS_SERVICE_AREA_OUTPUT(Includes M / DB) → PNS_ERR_INVALID_PARAM
  • length == 0 → *p_len = 0 and returns PNS_OK(a null read is considered successful)
  • Session not started / STOP → PNS_ERR_NO_STACK
  • Out of Bounds → PNS_ERR_INVALID_PARAM, Copy 区域越界: 偏移 %u + 长度 %u 超出过程映像区 %u 字节, *p_len Set to 0
  • Buffer too small → PNS_ERR_INVALID_PARAM, *p_len write back the required length (No half-frame copying)
  • Each time, the current value is read back.

Example:

uint8_t part[2]; uint16_t pn = sizeof(part);
if (PnSServiceReadArea(session, PNS_SERVICE_AREA_INPUT, 4, 2, &pn, part) == PNS_OK)
printf("I 偏移 4 起 2 字节:0x%02X%02X\n", part[0], part[1]);

PnSServiceWriteArea()​

pns_result_t PnSServiceWriteArea(
pns_service_handle_t session,
pns_service_area_t area,
uint16_t offset,
const uint8_t * p_data,
uint16_t length);

PressRegion + Offset + LengthPartially overwrite Q zone. This is the entry point for "merging a segment on my own and writing it in all at once." A return PNS_OK indicates a successful write, and the next cycle will be triggered automatically.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • Write to Zone I (PNS_SERVICE_AREA_INPUT) → PNS_ERR_NOT_SUPPORTED(Zone I read-only)
  • area Not PNS_SERVICE_AREA_INPUT / PNS_SERVICE_AREA_OUTPUT → PNS_ERR_INVALID_PARAM(M / DB Not supported)
  • session / p_data For NULL → PNS_ERR_INVALID_PARAM
  • length == 0 → PNS_ERR_INVALID_PARAM(No data)
  • Session not started / STOP → PNS_ERR_NO_STACK
  • Out of Bounds → PNS_ERR_INVALID_PARAM, use PnSServiceLastError(session) Read for the specific copy; Without Relying on Truncation or a Fallback Mechanism(Truncation + a success message might lead people to believe that the entire segment has been written.)
  • Write one at a timeA continuous segmentRegion; For non-adjacent changes, please merge them into a single commit or split them into multiple commits (each call counts as a separate commit).

Example:

uint8_t q[2] = { 0x00, 0x7B };
if (PnSServiceWriteArea(session, PNS_SERVICE_AREA_OUTPUT, 2, q, sizeof(q)) == PNS_OK)
printf("Q 偏移 2 起 2 字节已提交\n");

if (PnSServiceWriteArea(session, PNS_SERVICE_AREA_INPUT, 0, q, sizeof(q)) == PNS_ERR_NOT_SUPPORTED)
printf("I 区只读\n");

PnSServiceGetIo()​

pns_result_t PnSServiceGetIo(
pns_service_handle_t session,
uint16_t * p_input_len, uint8_t * p_input,
uint16_t * p_output_len, uint8_t * p_output);

Retrieves both directions into the local buffer at once. Use this if you don't want to make two separate calls; you can also just retrieve the length.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • Match the output parameter names with their corresponding regions.: Group 1 p_input_len / p_input ContainsServer Output Area = Addressed Q (Device → Controller); Group 3 p_output_len / p_output ContainsServer Input Area = Addressing I (Controller → Device). PnSServiceRead Read I uses the parameters from Group 3
  • If both output groups are passed, it means NULL "skip this group"; however, if only the buffer is provided without capacity (p_input_len == NULL && p_input != NULLor similarly for the output group) → PNS_ERR_INVALID_PARAM
  • p_*_len In = buffer capacity, Out = actual number of bytes; if capacity is 0, only the length is checked
  • A determinism buffer is too small → PNS_ERR_INVALID_PARAM, *p_*_len length required for write-back, string 过程映像 Q 缓冲过小: 需要 %u 字节, 容量 %u / 过程映像 I 缓冲过小: 需要 %u 字节, 容量 %u(No half-frame copying)
  • Session not started / Stopped → ; PNS_ERR_NO_STACK Channel not ready → PNS_ERR_NATIVE
  • Don't specify a fixed length: 20 / 32

Example:

uint8_t i[64], q[64]; uint16_t ilen = sizeof(i), qlen = sizeof(q);
if (PnSServiceGetIo(session, &qlen, q, &ilen, i) == PNS_OK) /* Group 1 = Q, Group 3 = I */
printf("Q %u 字节 / I %u 字节\n", qlen, ilen);

uint16_t q_need = 0, i_need = 0;
PnSServiceGetIo(session, &q_need, NULL, &i_need, NULL); /* Check only the lengths on both sides */

pns_shm_* Direct Connect Ports​

pns_shm_* A type that allows direct read/write access to process data without establishing a service session. The criteria (unmapped/unconnected) and failure semantics are consistent with those of service sessions, with the exception that it includes an additional write behavior called "allow short writes."

pns_shm_read_from_plc() / pns_shm_read_to_plc_shadow()​

pns_result_t pns_shm_read_from_plc(pns_shm_map_t * map,
uint8_t * buf, size_t buf_cap, size_t * p_len,
size_t payload_len);
pns_result_t pns_shm_read_to_plc_shadow(pns_shm_map_t * map,
uint8_t * buf, size_t buf_cap, size_t * p_len,
size_t payload_len);

Read the input area (I, controller → device) and the shadow of the output area (Q, the most recent entry you wrote there).

Return value:

  • pns_result_t — PNS_OK Success; PNS_ERR_INVALID_PARAM Insufficient buffer capacity (*p_len the required length has been written back); PNS_ERR_NO_STACK Unmapped

Note:

  • Reads the data itself; *p_len = Effective Length = min(payload_len, 可用范围): payload_len = True Length in this direction (take GET /api/io in Zone I outputLength, take in Zone Q inputLength; field names are opposite to the I/Q directions—do not rely on the names), 0 = Unknown; based on the available range, Do not extrapolate based on district capacity
  • pns_shm_read_to_plc_shadow Symmetric pns_shm_write_to_plc : What is written is what is read; *p_len = 0 returns when the output region is not yet ready PNS_OK
  • buf Call: Check NULL length only (returns PNS_OK, *p_len but still writes back the required length)
  • Reads in this family are snapshots; this may result in data inconsistencies due to concurrent updates during runtime; to ensure consistency, read all the data in a single operation.

Example:

uint8_t i_buf[64]; size_t n = 0;
size_t image_i_len = 2, image_q_len = 2; /* I actual length =outputLength, Q actual length =inputLength (field name reversed from I/Q ), unknown: 0 */
if (pns_shm_read_from_plc(shm, i_buf, sizeof(i_buf), &n, image_i_len) == PNS_OK)
printf("I %zu 字节\n", n);

size_t qn = 0;
pns_shm_read_to_plc_shadow(shm, NULL, 0, &qn, image_q_len); /* Check only the shadow length */

pns_shm_write_to_plc()​

pns_result_t pns_shm_write_to_plc(pns_shm_map_t * map,
uint32_t offset,
const uint8_t * data, size_t len,
size_t payload_len);

Writes to the Q area starting at data offset offset (device → controller). The write boundary = the valid length = min(payload_len, 可用范围) (payload_len = the true length of the Q area = inputLength of GET /api/io; the field names are opposite to the I / Q direction, do not take them by name; 0 = unknown, use the available range). Exceeding the valid range returns PNS_ERR_INVALID_PARAM.

Return value:

  • pns_result_t — PNS_OK Success (including short writes); PNS_ERR_INVALID_PARAM Invalid parameter / data is NULL / Off-bounds offset / Cannot write even a single byte; PNS_ERR_NO_STACK Unmapped

Note:

  • Abbreviations allowed: When you can't write the entire value, truncate it to the maximum allowed length; it will still return the value PNS_OK(you can use pns_shm_read_to_plc_shadow to verify the actual length written to disk). For ports that report an fault when the value exceeds the bounds, use the PnSServiceWriteArea / PnSProcessDataOut* family.
  • len == 0 Data is not backed up; only a single commit is made.
  • Mutual exclusion is not guaranteed for the same handle among callers, If writing in a multithreaded environment, please serialize the operations yourself.

Example:

uint8_t q[2] = { 0x01, 0x00 };
size_t image_q_len = 2; /* Q actual length = GET /api/io inputLength (field name reversed with I/Q ), pass 0 if unknown */
if (pns_shm_write_to_plc(shm, 0, q, sizeof(q), image_q_len) == PNS_OK) {
uint8_t back[2]; size_t n = 0;
pns_shm_read_to_plc_shadow(shm, back, sizeof(back), &n, image_q_len); /* Verify Actual Settlement */
}

pns_shm_connected() / pns_shm_is_mapped()​

int pns_shm_connected(pns_shm_map_t * map);
int pns_shm_is_mapped(const pns_shm_map_t * map);

Two local criteria: whether the mapping is still present, and whether the controller cycle data is normal. Taken from the runtime watchdog field; no false positives.

Return value:

  • int — pns_shm_connected: 1 = Controller periodic data is normal; 0 = Not connected / safe state / Not mapped
  • int — pns_shm_is_mapped: 1 = Mapped; 0 = NULL or Unmapped

Note:

  • These two functions do not produce any output IOand do not shielding; within the periodic loop, they first evaluate the condition and then pns_shm_connected decide whether to use the data or enter a safe state.
  • When not connected, the bytes read are the old values from before the refresh STOP; do not treat them as current readings.

Example:

while (running) {
if (!pns_shm_connected(shm)) { /* Not Connected / Safe State */
Sleep(10); /* Bring your own while waiting between shots: Sleep from <windows.h>; this SDK header is not included. */
continue;
}
pns_shm_read_from_plc(shm, i_buf, sizeof(i_buf), &n, image_i_len);
/* ... Let's see ... */
pns_shm_write_to_plc(shm, 0, q, sizeof(q), image_q_len);
Sleep(10); /* Waiting for the next shot (same as above) */
}

Record Index Space (Non-Periodic Data)​

Non-periodic data uses the record index, The index numbers are divided into segments.: Currently, the only sections SDK with access points are the user zone and the I&M sections; these two routes do not overlap.

Index SectionWhat is it?Where SDK Am I Going?
0x0000 – 0x7FFFUser/Manufacturer Section: Application parameter records written by the controller and stored on the device sidePnSServiceGetRecords
0x8000 StartStandard Index Section (standardized indexes such as expected/actual identifiers, diagnostics, substitute values, and port statistics)This does SDK not provide a general-purpose record read port.; Run the PnSServiceGetDiag / PnSServiceGetIoStatus diagnostic
0xAFF0 – 0xAFF4I&M Identification: I&M0(0xAFF0) and I&M1–4(0xAFF1–0xAFF4)PnSServiceGetIm
  • I&M is not in the user log: User Area Records: Receive Only 0x0000–0x7FFF For this section, I&M (0xAFF0 starting from) is handled automatically by the protocol stack, PnSServiceGetRecords The I&M index is never visible. If you need equipment identification, just go. PnSServiceGetIm.
  • This shielding was recorded byRead-Only Observation: Records written by the controller to the device are received and stored by the device,SDK which is responsible for reading them back to you; there is no mechanism for the device to " SDK write records back to the controller."
  • Records are independent of process data: Writing a record does not alter the cycle data, nor does reading a record.

PnSServiceGetRecords()​

pns_result_t PnSServiceGetRecords(
pns_service_handle_t session,
pns_service_user_record_t * p_items,
size_t items_cap,
size_t * p_count);

Retrieve the user area record table (index 0x0000—0x7FFF that section) using a two-rung call.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • session / p_count for NULL → PNS_ERR_INVALID_PARAM
  • Check quantity only: p_items "传" and "且 NULL " items_cap must be present; 0``p_items "非" and " NULL 而" items_cap == 0 are not allowed.
  • Actual number of entries exceeds capacity → The filled entries remain valid PNS_ERR_INVALID_PARAM; *p_count write back the actual number of entries, string 用户区记录缓冲过小: 实际 %u 条, 容量 %u
  • Service unreachable → ; PNS_ERR_NO_CONNECTION Server fault (not 200) → PNS_ERR_NATIVE. This is a service management request, Not a process data channel
  • Missing fields are treated as 0 or empty; data_hex no values are fabricated. Fields that are too long are truncated as-is (NUL at the end).

Example:

size_t count = 0;
PnSServiceGetRecords(session, NULL, 0, &count); /* Check the number of entries first */

pns_service_user_record_t recs[8];
if (PnSServiceGetRecords(session, recs, 8, &count) == PNS_OK) {
for (size_t k = 0; k < count; k++)
printf("idx=0x%04X slot=%u len=%u\n", recs[k].index, recs[k].slot, recs[k].length);
}

PnSServiceGetIm()​

pns_result_t PnSServiceGetIm(
pns_service_handle_t session,
pns_service_im_info_t * p_im);

Retrieve the device identifier (I&M0 effective value + I&M1–I&M3 field + project identifier field) —— This is the only entry point for the I&M index segment (0xAFF0–0xAFF4) SDK in this file.

Return value:

  • pns_result_t — PNS_OK Success; see below for other possible values

Note:

  • session / p_im For NULL → PNS_ERR_INVALID_PARAM
  • Service Unavailable → PNS_ERR_NO_CONNECTION; Server fault (not 200) → PNS_ERR_NATIVE
  • I&M1–I&M3 has been enabled and read by the native stack (stack is RUN im14_supported = 1; im_supported array of actual enabled sets):IO If the controller has never been written to, the factory default values are read (I&M1 function tags Darra, installation location Darra PnS 20I/32O, I&M2 date, I&M3 description), Not an empty string; I&M4 Not enabled; im4_signature_hex no value is guaranteed
  • These fields are empty strings when the stack is not RUN or when I&M1–4 is unreadable; the order numberim14_supported = 0, serial number, and software/hardware versions are fixed default values mirrored from the native stack—the reasons are all accurately documented in [...], with im_note no fictional elements.
  • Missing fields retain a zero value (empty string / 0) and are not formatted; string fields are truncated at the end NUL if they exceed the length limit.

Example:

pns_service_im_info_t im;
if (PnSServiceGetIm(session, &im) == PNS_OK) {
printf("vendor=0x%04X device=0x%04X station=%s\n",
im.vendor_id, im.device_id, im.station_name);
printf("I&M: %s (I&M1-4 supported=%d)\n", im.im_supported, im.im14_supported);
}

How do I get the number of bytes?​

Use the true length (true length GET /api/io for Zone I = outputLength, true length for Zone Q = inputLength; the field names are opposite to the I/Q directions—do not use the names as references), effective length = min(真长, 可用范围), true length unknown (0) based on the available range, Do not extrapolate based on district capacity. The length is determined by the value negotiated by the PLC (IO controller) and is automatically matched after link, Don't be too rigid 20 / 32.

What you wantWhere to get it
I Effective LengthPnSServiceRead(session, NULL, 0, &n) (If p_data is NULL, only the length is queried; no data is copied)
Q Effective LengthPnSServiceGetIo(session, &q_need, NULL, NULL, NULL) (The 1st group of output parameters is of type Q, and *n is of the same type as Q)
Take from both sidesPnSServiceGetIo(session, &q_len, q, &i_len, i) (both sides have valid lengths = min(actual length, available range)); any output parameter can be set to NULL to query only the length.
Mapped Data LengthMapped to parameter *n (effective length, same caliber as above)
The true length itselfThere is no separate query interface: The query pattern above uses an effective length equal to min(actual length, available range); under normal circumstances, this is the actual length. The input_area_length and output_area_length parameters in PnSServiceGetDiag represent the zone window lengths, not the actual data length.
Length of the direct link portpns_shm_read_from_plc / pns_shm_read_to_plc_shadow: *p_len (if buf is NULL, only the length is checked)

If one of the three criteria is missing, then... Don'tUsing a mapping as a struct: rc == PNS_OK && *p != NULL && *n >= sizeof(T) .

Behavior When the Channel Is Not Ready​

When the process data channel is not ready, all ports must report the fault accurately:

  • Session not started / Already STOP: PNS_ERR_NO_STACK , Copy 服务未 Start 或已 Stop,请先 PnSServiceStart
  • Data not ready: PNS_ERR_NATIVE Process data is unavailable
  • Read the specific string PnSServiceLastError(session)(process-level fallback pns_last_error())

IOPS / IOCS These quality bytes travel through the service plane /api/io, which is a separate path from the process data.

Boolean only in the least significant bit

PnSProcessDataInBool / PnSProcessDataItemReadBoolBit0 Press bit0 Judging Truth: Byte 0x00 is read as false, 0x01 and is read as , truewhile 0x02 Read with 0x80 Read with false — This is not "non-zero is true"; many people make this mistake the first time. To determine true or false based on the entire byte being 0 or not 0, use PnSProcessDataInU8 / PnSProcessDataItemReadU8 to retrieve the raw byte and compare it yourself; this function does not SDK provide a second Boolean operator for "non-zero is true."Write side-by-side: OutBool / produces only WriteBoolBit0 0x01 / 0x00.

Don't treat "Write I" as a usable feature.

"Write I" always returns PNS_ERR_NOT_SUPPORTED: PnSProcessDataInputsMappingRO it provides a pointer const ; PnSServiceWriteArea / PnSServiceWriteBool / PnSProcessDataItemWrite* It does not write to the I side and reports that it is not supported. To write process data, use the Q area.