Skip to main content

Attributes and State Machines

Properties​

CategoryPropertyTypeAccessDescription
StaticisServiceReady()booleanread-onlyWhether the local service is ready
getLastError()StringRead-onlyThe string of the most recent failure across the entire application (sticky; not cleared upon success)
getLastErrorCode()PnSErrread-onlyThe fault code of the most recent failed call across the entire application. Never returns 'OK' for a successful call
isServiceConnected()booleanread-onlyWhether the IO controller is performing cyclic exchange. If the service is not started, this returns false.
getServiceState()PnSStateread-onlyOperational ST. If the service is not RUN = UNKNOWN
getServiceWaitReason()StringRead-onlyReason for the wait. Service not started = 'Service not ready'
getServiceStatusMessage()StringRead-onlyST message. If the service is not RUN = 'Service not ready'
Connections and SessionsisConnected()booleanread-onlyWhether the IO controller has established cyclic data exchange. Only true means cyclic data is being exchanged.
isAttached()booleanread-onlyWhether this SDK session is connected to the local service. 'stop' does not change this value
isStarted()booleanread-onlyWhether the IO device is enabled (after start, before stop)
Operational STgetStatus()PnSStateread-onlyEnumerates the operational ST. If the actual ST cannot be read, the value is UNKNOWN
getWaitReason()StringRead-onlyReason for this session's wait. Empty when the IO controller is connected
isEverConnected()booleanread-onlyWhether a link has ever been established with the IO controller. If true and isConnected is false, the link has been lost.
fault CodesgetErrorCodeEnum()PnSRuntimeErrorCoderead-onlyThe most recent runtime fault. Unrecognized value: UNKNOWN
getErrorCodeRaw()intread-onlyThe raw value of the runtime fault (0 = no fault). Unrecognized values are still passed through by this method
getErrorMessage()StringRead-onlyRuntime fault message in Chinese
getLastServiceError()StringRead-onlyThe most recent failure message for this session (sticky; not cleared upon success)
Diagnostic PortalgetEvents()PnSEventsread-onlyRuntime events.
getDiagSnapshot()PnSDiagSnapshotread-onlyDiagnostic snapshot.
getIoStatus()PnSIoStatusread-onlyIOPS / IOCS.
getProcessData()ProcessDataread-onlyProcess data.
Local SessiongetHost()StringRead-onlyThe host of the service connected to in this session
getPort()intread-onlyThe service port of the current session link
PnSState

The link and data exchange ST between the IO device and the IO controller (return values of getStatus() / getServiceState()).

public enum PnSState {
NONE(0), // Not Initialized / Stack Not Initialized
IDLE(1), // Started, waiting for the IO controller (no AR)
CONNECTING(2), // The controller has initiated a link (STARTUP)
PARAMETERIZED(3), // Parameterization complete (PRMEND), waiting for the application to be ready
APPLICATION_READY(4), // Application Ready (APPLRDY), parameter exchange complete
DATA_EXCHANGE(5), // Cyclic data exchange is normal
ABORTED(6), // link lost (ABORT)
RESETTING(7), // Device is resetting (reserved ST name; this ST is not triggered 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(Integer.MAX_VALUE) // Unknown / Unable to read the actual ST
}
  • Only DATA_EXCHANGE indicates that periodic data is being exchanged.
  • UNKNOWN This indicates that the actual ST cannot be read; do not interpret this as NONE(not initialized).
PnSRuntimeErrorCode

Server-side runtime fault code (getErrorCodeEnum() the return value, enumerated from the errorCode field exposed by the ST query). This differs in PnSErr semantics from the call return code and should not be used interchangeably.

public enum PnSRuntimeErrorCode {
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), // Slot 0: Insertion Failed
ERR_NULL(-8), // The device has not started (empty session / not started or STOP)
ERR_DIAG(-9), // Invalid diagnostic parameter
WIN32_ERROR(1), // Win32 System fault code (ConfigError exposed during downgrade, e.g., 2048)
UNKNOWN(-10000) // Unrecognized value
}

Unrecognized values are returned UNKNOWN, while the original values are still passed through getErrorCodeRaw() as-is.

Start-Stop​

start()​

public void start()

Enable the IO device. If start has already been called and stop has not, return immediately. This does not mean the IO controller is exchanging data on a cycle basis; whether the controller is currently exchanging data is shown by isConnected().

Throw an exception on failure PnSServiceException, and write to the static variable getLastError().

Example:

try {
device.start();
} catch (PnSServiceException e) {
System.err.println(PnSService.getLastError());
}

stop()​

public void stop()

Deactivate the IO device. If the instance still exists, it can be started again with start(). If it was never started or stop has already been called, it returns immediately. Failures write getLastError; no exception is thrown.

Not close(): stop does not end the SDK session.

Example:

device.stop();

Operating State​

getStatus()​

Operational ST. start / stop simply enable / disable the IO device; this method always reads the actual on-site ST. If the actual ST cannot be read, the value is UNKNOWN. If unreachable, throw PnSServiceException.

public PnSState getStatus()

Example:

if (device.getStatus() == PnSState.DATA_EXCHANGE)
System.out.println("周期数据交换中");

getStatusEx()​

public ServiceStatus getStatusEx()

Retrieves the operational ST, whether a swap has occurred, and the operational fault code in a single call. Throws "Session not established" or "Unreachable" if the session cannot be established PnSServiceException.

ServiceStatus
public static final class ServiceStatus {
public PnSState getState() { }
public boolean isConnected() { }
public int getErrorCode() { }
public PnSRuntimeErrorCode getErrorCodeEnum() { }
public String getWaitReason() { }
public String getStatusMessage() { }
public int getAlarmCount() { }
public int getCriticalAlarmCount() { }
public boolean isEverConnected() { }
}

Example:

PnSService.ServiceStatus snap = device.getStatusEx();
if (!snap.isConnected())
System.out.println(snap.getState() + " " + snap.getWaitReason());

getWaitReason()​

public String getWaitReason()

Reason for this session to wait. Empty when the IO controller is connected. In the if (!device.isConnected()) loop, read this method or getLastServiceError().

Example:

if (!device.isConnected())
System.out.println(device.getWaitReason());

isEverConnected()​

public boolean isEverConnected()

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

Example:

if (!device.isConnected() && device.isEverConnected())
System.out.println("掉线");

Error Code​

getErrorCodeEnum()​

public PnSRuntimeErrorCode getErrorCodeEnum()

Last runtime fault (enumeration). Unrecognized value UNKNOWN. The original value is still passed getErrorCodeRaw() through. Use for Chinese messages getErrorMessage().

Example:

PnSRuntimeErrorCode err = device.getErrorCodeEnum();
String text = device.getErrorMessage();
int raw = device.getErrorCodeRaw();

getErrorCodeRaw()​

public int getErrorCodeRaw()

Original value of the runtime fault (0 = no fault). Unrecognized values are still passed through by this method.

Example:

int raw = device.getErrorCodeRaw();

getErrorMessage()​

public String getErrorMessage()

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

Example:

System.out.println(device.getErrorMessage());

getLastServiceError()​

public String getLastServiceError()

The most recent failure message for this session (sticky). The success path is not cleared. If this if (!isConnected()) method is null during a loop, read getWaitReason().

Example:

System.out.println(device.getLastServiceError());

Diagnosis​

getDiagSnapshot()​

public PnSDiagSnapshot getDiagSnapshot()

Retrieve the link, frame count, watchdog, and jitter in a single operation. If a value cannot be read isOk() == false, read it again getMessage(); this is not considered an exception.

Example:

PnSDiagSnapshot snap = device.getDiagSnapshot();
if (!snap.isOk())
System.out.println(snap.getMessage());

getIoStatus()​

public PnSIoStatus getIoStatus()

IOPS / IOCS Snapshot. 0x80 = GOOD, 0x00 = BAD. If the link is lost, all values are set to BAD. If the data cannot be read, return PnSIoStatus.getEmpty().

Example:

if (!device.getIoStatus().isAllGood())
System.out.println(device.getIoStatus().toString());

Call Exception​

Throw an exception PnSServiceException(inherited com.darra.pns.PnSSdkException, carrying an PnSErr fault code and details).

public class PnSServiceException extends PnSSdkException
CategoryPropertyTypeAccessDescription
Call ExceptionsgetMessage()StringRead-onlyChinese message (code description + details)
getCode()PnSErrread-onlySDK fault code
getDetail()StringRead-onlyFailure details
getServerErrorCode()intread-onlyThe original value of the runtime fault. 0 = not present

Example:

try {
device.writeInt16("QW0", (short) 1);
} catch (PnSServiceException e) {
System.err.println(e.getMessage());
System.err.println(e.getCode());
if (e.getServerErrorCode() != 0)
System.err.println(e.getServerErrorCode());
}

PnSErr​

SDK Call fault code: 0 = Success; all other codes indicate failure.

public enum PnSErr {
OK(0), // Success
INVALID_PARAM(1), // Invalid parameter
NOT_INITIALIZED(2), // Device not initialized
ALREADY_INITIALIZED(3), // The device has been initialized.
TIMEOUT(4), // Operation timed out
NO_DEVICE(5), // No devices available
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), // Runtime library not found
UNKNOWN(999) // Unknown fault
}

Unrecognized values are assigned to UNKNOWN. Static PnSService.getLastError() / Read the most recent failure (sticky) getLastErrorCode() when no instance is available.

Complete Example​

import com.darra.pns.service.PnSService;
import com.darra.pns.service.PnSServiceException;

try (PnSService device = new PnSService()) {
device.start();
while (running) {
if (!device.isConnected()) {
System.out.println(device.getStatus());
System.out.println(device.getWaitReason());
System.out.println(device.getErrorMessage());
continue;
}

device.writeBool("Q0.0", true);
boolean fromPlc = device.readBool("I0.0");
}
device.stop();
} catch (PnSServiceException e) {
System.err.println(e.getMessage());
}