Initialization
Use new PnSService(), wrap it in try-with-resources, adjust it once start(), and write Q and read I in the loop.
First, install the Darra Profinet Slave. Set the station name and network card in the configuration tool; there is no need to specify the structure. Do not use new / in every cycle close().
Constructor
PnSService()
public PnSService()
Create a new IO Device instance. You can create a new instance even if the service hasn't started yet; no exception is thrown. This simply retrieves the object—it hasn't been activated yet IO,IO and the controller isn't connected either. Implementation AutoCloseable.
Example:
try (PnSService device = new PnSService()) {
device.start();
while (running) {
// Write "Q", read "I" — write it here
}
device.stop();
}
try-with-resources Enclose the entire program's lifetime; the loop is written in try Inside. start Once is enough. Q close() is cleared at the end of each cycle, so anything written to it in the next cycle will be discarded.
Start-Stop
start()
public void start()
Enable IO. If it has already been started and hasn't been STOP yet, calling it again will simply return. Success does not necessarily mean the controller IO is already updating the data; that depends on isConnected().
An exception is thrown PnSServiceException, and it is logged getLastError(). Another instance is already RUN and has also thrown an exception PnSServiceException.
Example:
try {
device.start();
} catch (PnSServiceException e) {
System.err.println(PnSService.getLastError());
return;
}
stop()
public void stop()
Disabled IO. The instance is still running; you can try again start(). If it has never been started or has already been stopped, it’s fine to call it again. If it fails getLastError, log it; do not throw an exception.
Completely liberalize the use of close(), No stop().
Example:
device.stop();
Resource Release
close()
public void close()
If it's still RUN stop, release the instance first. Q will be cleared (only the data area); I will be retained for the next program. Idempotent—implemented AutoCloseable. Once released, this instance can no longer be used and must be re-created new PnSService().
Disable IO stop(). stop The instance will still be there afterward, so you can start().
Example:
device.close();