Attributes and State Machines
Properties
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Static | is_service_ready / PnSService.is_service_ready | bool | read-only | Whether the local service is ready |
last_error / PnSService.last_error | str | read-only | Message describing the most recent failure across the entire application (sticky; not cleared upon success) | |
last_error_code / PnSService.last_error_code | PnSErr | read-only | The fault code from the most recent failed call across the entire application. Never fails; returns OK | |
service_connected / PnSService.service_connected | bool | read-only | Whether the IO controller is performing cyclic exchange. If the service is not started, this is False. | |
service_state / PnSService.service_state | PnSState | Read-only | Operational ST. If the service is not RUN = UNKNOWN | |
service_wait_reason / PnSService.service_wait_reason | str | read-only | Reason for the wait. Service did not start = 'Service not ready' | |
service_status_message / PnSService.service_status_message | str | 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 | |
is_running / is_started | bool | read-only | Whether the IO device is enabled (after start, before stop) | |
| Operational ST | state | PnSState | read-only | Enumeration of operational states. If the actual ST cannot be read, it is UNKNOWN; do not treat it as NONE |
wait_reason | str | read-only | Reason for this session's wait. Empty when the IO controller is connected | |
ever_connected | bool | read-only | Whether a link has ever been established with the IO controller. True and connected is False = disconnected | |
| fault Codes | error_code | PnSRuntimeErrorCode | read-only | Last runtime fault. Unrecognized negative value UNKNOWN; positive values are mapped to WIN32_ERROR |
error_code_raw | int | read-only | Raw value of the runtime fault (0 = no fault). Unrecognized values are still passed through this property | |
error_message | str | read-only | Runtime fault message in Chinese | |
last_service_error | str | read-only | The most recent fault message for this session (sticky; not cleared upon success) | |
| Byte-Level Operations | input | bytes | read-only | Reads the entire block back; the name is reversed — it corresponds to the `process_data.Out` block; see the explanation below |
output | bytes | read-only | A snapshot of the entire block; the name is reversed — it corresponds to the `process_data.In` block and shares the same source as read() | |
output_committed | bool | read-only | Whether the written block has been sent out (written and delivered). Q never written / delivery failed = False |
input / output The name is reversedsvc.input and svc.output the nameThe exact opposite of that set process_data:
| Properties | Which part of the process data does this correspond to? |
|---|---|
svc.input | and process_data.Out / outputs_mapping(S) The same piece(Not that one In ) |
svc.output | and process_data.In / inputs_mapping(S) The same piece(Not that one Out ) |
Don't use these in everyday writing: svc.read() / svc.write() and process_data.In / have the same Out names and meanings as the characters in the table above, so use those instead. Use svc.input / only when you need to write a whole paragraph of raw bytes svc.output; be sure to check the table above before using it.
Module functions are equivalent to class properties (class properties do not use parentheses). If the service hasn't been implemented yet last_error, no exception is thrown.
from darra_pns import (
is_service_ready, service_connected, service_state,
service_wait_reason, last_error,
)
ready = is_service_ready()
ctrl = service_connected()
st = service_state()
wait = service_wait_reason()
err = last_error()
PnSState
The link and data exchange ST between the device and the IO controller (return values of state / service_state).
class PnSState(IntEnum):
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
APPLICATION_READY = 4 # Application Ready (APPLRDY), parameter exchange complete
DATA_EXCHANGE = 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)
CONFIG_ERROR = 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
DATA_EXCHANGEindicates that periodic data is being exchanged. UNKNOWN"Unknown" / The actual ST cannot be read; do not treat it asNONE.
PnSRuntimeErrorCode
Enumeration of server-side runtime errors (as revealed by ST queries errorCode) anderror_code their return values.
class PnSRuntimeErrorCode(IntEnum):
OK = 0 # No errors
ERR_INIT = -1 # Stack initialization failed
ERR_CFG = -2 # Invalid configuration
ERR_THREAD = -3 # Failed to create a periodic thread / set priority
ERR_RUNNING = -4 # Invalid ST (repeated start/stop)
ERR_PDI = -5 # Process Data Exchange Failed
ERR_DAP = -6 # Insertion into slot 0 (DAP) failed
ERR_NULL = -8 # The device has not started (not started or has STOP)
ERR_DIAG = -9 # Invalid diagnostic parameter
WIN32_ERROR = 1 # System fault codes returned by the demoted ST (positive codes, such as 2048)
UNKNOWN = -10000 # Unrecognized value
- Call Return Codes PnSErr The meanings are different; they should not be used interchangeably.
- Unrecognized negative values are assigned to
UNKNOWN, positive values to ;WIN32_ERRORthe original values are passed througherror_code_rawas-is via .
PnSErr Enumeration Values
SDK The return value of a call (start/stop, read/write process data, or diagnostics); 0 = success.
class PnSErr(IntEnum):
OK = 0 # Success
INVALID_PARAM = 1 # Invalid parameter
NOT_INITIALIZED = 2 # Device not initialized (not started)
ALREADY_INITIALIZED = 3 # The device has been initialized.
TIMEOUT = 4 # Operation timed out
NO_DEVICE = 5 # No available devices / Service unavailable
NOT_AVAILABLE = 6 # Feature Unavailable
IO_ERROR = 7 # IO Read/Write fault
ALARM_ERROR = 8 # fault Report
CONFIG_ERROR = 9 # Configuration Error
DLL_NOT_FOUND = 100 # Library not found
UNKNOWN = 999 # Unknown fault
- This enumeration does not have a "Busy" member: returns when
start()the real-time link port is occupied by another instanceNOT_AVAILABLE.
Start-Stop
start Once; do not call it every cycle.
start()
def start(self) -> PnSErr
Enable IO: start the local service, establish a process data channel, POST /api/start. If start has already been called and stop has not, returns idempotently with PnSErr.OK.
This does not mean that the IO controller has completed cyclic exchange; whether the IO controller is exchanging data is shown by connected. Only one real-time session is allowed at a time; a second one returns PnSErr.NOT_AVAILABLE.
Example:
rc = svc.start()
if rc != PnSErr.OK:
print(svc.last_service_error)
stop()
def stop(self) -> None
Deactivate IO (POST /api/stop). The process data channels and sessions are retained and can be started again with start. It is idempotent whether called before start or after stop. It is not close.
Example:
svc.stop()
close()
def close(self) -> None
Terminate the service session on this machine. Q is cleared (only the data area); I is retained for the next program. Idempotent; with exits the call stack and returns to this method. Once released, this instance can no longer be used and must be re-initialized PnSService(). The real-time main path also releases the real-time link gate.
Example:
svc.close()
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.
@property
def state(self) -> PnSState
Example:
if svc.state == PnSState.DATA_EXCHANGE:
print("周期数据交换正常")
wait_reason
@property
def wait_reason(self) -> str
Reason for this session's wait. Empty when the IO controller is connected. In the if not svc.connected loop, read this property or last_service_error.
Example:
if not svc.connected:
print(svc.wait_reason)
ever_connected
@property
def ever_connected(self) -> bool
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 not svc.connected and svc.ever_connected:
print("掉线")
Error Code
error_code
@property
def error_code(self) -> PnSRuntimeErrorCode
Last runtime fault (enumeration). Unrecognized negative value UNKNOWN; positive values are returned WIN32_ERROR. The original value is still passed error_code_raw through. Use for Chinese messages error_message.
Example:
err = svc.error_code
text = svc.error_message
raw = svc.error_code_raw
error_code_raw
@property
def error_code_raw(self) -> int
Original value of the runtime fault (0 = no fault). Unrecognized values are still passed through this property.
Example:
raw = svc.error_code_raw
error_message
@property
def error_message(self) -> str
Chinese fault messages. Return a description with the original value for unrecognized codes; do not discard the information.
Example:
print(svc.error_message)
last_service_error
@property
def last_service_error(self) -> str
The most recent failed script for this session (sticky). The success path is not cleared. If this if not connected property is empty during a loop, read it wait_reason.
Example:
print(svc.last_service_error)
Diagnosis
Diagnostic subobjects diagnostics, as well as flat properties that read the same data. When RUN the program, check this section first before working with process data.
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Entry | diagnostics | PnSDiagnostics | read-only | Diagnostics subobjects (snapshots / IOPS / alerts / logs) |
diag_snapshot | PnSServiceDiagSnapshot | read-only | Diagnostic snapshot. | |
io_status | PnSServiceIoStatus | Read-only | IOPS / IOCS.`0x80` = GOOD. | |
alarms / alarm_history | list | read-only | Active alarms / History (last 500 entries). An empty array does not indicate a failure | |
alarm_count / critical_alarm_count | int | read-only | Total number of active alarms / Number of critical alarms. Same source as 'alarms' | |
im_data | PnSServiceImData | read-only | I&M identifier: station name, manufacturer / equipment ID, order number | |
process_data | ProcessData | read-only | Process data view; write Q / read I. |
Example:
snap = svc.diag_snapshot
if not snap.ok:
print(snap.message)
else:
print("Up" if snap.link_up else "Down")
for a in svc.alarms:
print(a.message)
IOPS It depends— svc.io_status.all_good just because the value is there doesn't mean this beat will work.
Complete Example
from darra_pns import PnSErr, PnSService
with PnSService() as svc:
rc = svc.start()
if rc != PnSErr.OK:
print(svc.last_service_error)
else:
while running:
if not svc.connected:
print(svc.state)
print(svc.wait_reason)
print(svc.error_message)
continue
svc.write_bool("Q0.0", True) # Write Q
from_plc = svc.read_bool("I0.0") # Read I
_ = from_plc
svc.stop()