GracoPD2K API
#############

Functions (FC)
**************

.. _DecodeDateTime:

DecodeDateTime
==============

Convert from Graco-formatted date and time data into Siemens DTL.

The Graco date format is encoded as four bytes. These bytes correspond to the following (in MSB order):
- byte 1: last two digits in year (0-99). Starts at year 2000 (0) and max year is 2099 (99).
- byte 2: month number, with January=1, February=2, etc. 0 is not a valid month number.
- byte 3: day number (1=the 1st of the month). 0 is not a valid day number.
- byte 4: day of week (1=Monday, ... 7=Sunday). 0 is not a valid day of week number.
The Graco time format is also encoded as four bytes:
- byte 1: not used
- byte 2: hour number (24 hour format). 0=midnight, 1=1AM, etc.
- byte 3: minute number (0-59)
- byte 4: seconds number (0-59)

**dateSource** (INPUT DWord)
  Graco date format source

**timeSource** (INPUT DWord)
  Graco time format source

**dateTimeDest** (OUTPUT DTL)
  Datetime result


RETURN (Void)

.. _DecodeEventCode:

DecodeEventCode
===============

Convert an event/alarm code into a Siemens String. Event codes are always 4 ASCII characters in length.

**source** (INPUT DWord)
  Event code source, packed into DWord

**dest** (OUTPUT STRING)
  String result


RETURN (Void)

.. _DecodeUserID:

DecodeUserID
============

Convert a user ID into a Siemens String. User IDs are sent as 3 DWords each containing up to 4 ASCII characters. The max length of the entire user ID is 9 characters.

**source1** (INPUT DWord)
  User ID source, first four chars, packed as DWord

**source2** (INPUT DWord)
  User ID source, chars 5-8, packed as DWord

**source3** (INPUT DWord)
  User ID source, char 9, packed as DWord

**dest** (OUTPUT STRING)
  String result


RETURN (Void)

.. _EncodeUserID:

EncodeUserID
============

Converts a Siemens String into an equivalent set of DWords for a user ID. The max length of a user ID is 9 characters, and this FC will only process the String up to that length.

**source** (INPUT STRING)
  String source

**dest1** (OUTPUT DWord)
  Result, first four chars, packed as DWord

**dest2** (OUTPUT DWord)
  Result, chars 5-8, packed as DWord

**dest3** (OUTPUT DWord)
  Result, char 9, packed as DWord


RETURN (Void)

.. _GetDataBySlotNum:

GetDataBySlotNum
================

Read data from the specified Profinet device number(defined in target parameter) and a slot number. This FC uses the GEO2LOG and RD_ADDR built-in functions internally to lookup the process data address. The status codes from those functions are forwarded through the return value of this function.

**pnSlotNum** (INPUT UInt)
  Profinet slot number

**target** (INOUT `TypeTarget`_)
  target to operate on

**data** (OUTPUT DWord)
  Current process data


RETURN (`TypeFunctionCmdState`_)

.. _SetDataBySlotNum:

SetDataBySlotNum
================

Writes data to a specified Profinet device number (defined in target parameter) and a slot number. This FC uses the GEO2LOG and RD_ADDR built-in functions internally to lookup the process data address. The status codes from those functions are forwarded through the return value of this function.

**pnSlotNum** (INPUT UInt)
  Profinet slot number

**data** (INPUT DWord)
  Data to write

**target** (INOUT `TypeTarget`_)
  target to operate on


RETURN (`TypeFunctionCmdState`_)

.. _StatusToUDT:

StatusToUDT
===========

Read status data from a PD2K Integrated device and collect it into a user-defined data type. This provides a more-convenient view of the available data compared to accessing it directly. If an error occurs while attempting to read the device data, the state codes from GetDataBySlotNum will be forwarded through the return value of this function.

**target** (INOUT `TypeTarget`_)
  Target to operate on

**statusUDT** (OUTPUT `TypePd2kStatus`_)
  Status data packed in a user-defined data type


RETURN (`TypeFunctionCmdState`_)

Function Blocks (FB)
********************

.. _PumpControl:

PumpControl
===========

An interface into the pump controls for the PD2K Integrated system. This allows you to control the pump system state (e.g. mixing, standby, recipe change), clear alarms, complete a job, and set the mix control setpoint. The control inputs come from a user-defined data type - see TypePumpControlInputs.

**enable** (INPUT Bool)
  Enable command

**controls** (INOUT `TypePumpControls`_)
  Controls

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeEnableCmdState`_)
  Command state

State codes
+++++++++++

* **16#3000** - Command is valid, running
* **16#8000** - Error, device number is invalid
* **16#8001** - Internal error
* **16#0000** - Command is disabled

.. _ReadAlarmInfo:

ReadAlarmInfo
=============

Read an alarm record from the PD2K. See `SendDCS`_ for state codes.

**indexNum** (INPUT USInt)
  Index number (0-199)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**eventCode** (OUTPUT STRING[4])
  Event code

**dateTime** (OUTPUT DTL)
  Event datetime

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadEventInfo:

ReadEventInfo
=============

Read an event record from the PD2K. See `SendDCS`_ for state codes.

**indexNum** (INPUT USInt)
  Index number (0-199)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**eventCode** (OUTPUT STRING[4])
  Event code

**dateTime** (OUTPUT DTL)
  Event datetime

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadFluidControlMode:

ReadFluidControlMode
====================

Read the current fluid control mode (flow or pressure). See `SendDCS`_ for state codes.

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**fluidControlMode** (OUTPUT Bool)
  Fluid control mode (False=flow, True=pressure)

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadFlushSequence:

ReadFlushSequence
=================

Read parameters for a flush sequence. See `SendDCS`_ for state codes.

**flushSeqNum** (INPUT USInt)
  Flush sequence number (1-5)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**gunPurgeTime** (OUTPUT UInt)
  Gun purge time (s)

**initialFlushVol** (OUTPUT UInt)
  Initial flush volume (cc)

**finalFlushVol** (OUTPUT UInt)
  Final flush volume (cc)

**numWashCycles** (OUTPUT USInt)
  Number of wash cycles

**strokesPerWashCycle** (OUTPUT USInt)
  Number of strokes per wash cycle

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadGrandTotals:

ReadGrandTotals
===============

Read parameters for a flush sequence. See `SendDCS`_ for state codes.

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**materialATotal** (OUTPUT UInt)
  Material A total (gal)

**materialBTotal** (OUTPUT UInt)
  Material B total (gal)

**materialABTotal** (OUTPUT UInt)
  Material A+B total (gal)

**solventTotal** (OUTPUT USInt)
  Solvent total (gal)

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadGunContents:

ReadGunContents
===============

Read the recipe number currently loaded in a specified gun number. This will return 0 if the gun is filled with solvent, or 61 if the material is unknown. See `SendDCS`_ for state codes.

**gunNum** (INPUT USInt)
  Gun number (1-3)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**recipeNum** (OUTPUT USInt)
  Recipe number

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadJobInfo:

ReadJobInfo
===========

Read a job record from the PD2K. See `SendDCS`_ for state codes.

**indexNum** (INPUT USInt)
  Index number

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**dateTime** (OUTPUT DTL)
  Recorded datetime

**jobNum** (OUTPUT UInt)
  Job number

**recipeNum** (OUTPUT USInt)
  Recipe number

**totalVol** (OUTPUT UDInt)
  Total volume of mixed material (cc)

**userID** (OUTPUT STRING[9])
  User ID

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadMixFillSP:

ReadMixFillSP
=============

Read the current mix fill setpoint. See `SendDCS`_ for state codes.

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**mixFillSP** (OUTPUT UInt)
  Current mix fill setpoint (cc/min for flow mode, psi for pressure mode, 0 if disabled)

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadPumpMaterial:

ReadPumpMaterial
================

Read the material number currently loaded in a specified pump number. See `SendDCS`_ for state codes.

**pumpNum** (INPUT USInt)
  Pump number (1-4)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**materialNum** (OUTPUT USInt)
  Material number

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadRecipe:

ReadRecipe
==========

Read parameters for a given recipe number. See `SendDCS`_ for state codes.

**recipeNum** (INPUT USInt)
  Recipe number (0-60)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**matNumA** (OUTPUT USInt)
  Material number of component A

**matNumB** (OUTPUT USInt)
  Material number of component B

**flushSeqNumA** (OUTPUT USInt)
  Flush sequence number for component A

**flushSeqNumB** (OUTPUT USInt)
  Flush sequence number for component B

**mixRatioSP** (OUTPUT UInt)
  Mix ratio setpoint (A*100 : B)

**potLifeTimeSP** (OUTPUT UInt)
  Potlife time setpoint (min)

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadRecipePotLifeTime:

ReadRecipePotLifeTime
=====================

Read the remaining potlife for a given recipe number if it is currently loaded and mixed. This command will return the max UInt value (65535, or 16#FFFF) if there is no potlife time associated with the recipe or if the timer has not started. See `SendDCS`_ for state codes.

**recipeNum** (INPUT USInt)
  Recipe number (1-60)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**potLifeTimeRem** (OUTPUT UInt)
  Remaining potlife time (min)

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _ReadUserID:

ReadUserID
==========

Read the current User ID. See `SendDCS`_ for state codes.

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**userID** (OUTPUT STRING[9])
  User ID

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _SendDCS:

SendDCS
=======

Send a dynamic command structure (DCS) to the PD2K. See the operation manual for more details regarding the DCS.

**cmdID** (INPUT USInt)
  Command ID

**arg0** (INPUT DWord)
  Argument 0

**arg1** (INPUT DWord)
  Argument 1

**arg2** (INPUT DWord)
  Argument 2

**arg3** (INPUT DWord)
  Argument 3

**arg4** (INPUT DWord)
  Argument 4

**arg5** (INPUT DWord)
  Argument 5

**arg6** (INPUT DWord)
  Argument 6

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**ret0** (OUTPUT DWord)
  Return 0

**ret1** (OUTPUT DWord)
  Return 1

**ret2** (OUTPUT DWord)
  Return 2

**ret3** (OUTPUT DWord)
  Return 3

**ret4** (OUTPUT DWord)
  Return 4

**ret5** (OUTPUT DWord)
  Return 5

**ret6** (OUTPUT DWord)
  Return 6

**ret7** (OUTPUT DWord)
  Return 7

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state

State codes
+++++++++++

* **16#2000** - Command completed
* **16#4000** - Command was aborted
* **16#8000** - Error, command ID must be non-zero
* **16#8001** - Error, device number is invalid
* **16#8002** - Internal error
* **16#8003** - Error, timed out waiting for DCS registers to be available
* **16#8004** - Error, DCS command failed
* **16#8005** - Error, timed out waiting for acknowledge signal

.. _WriteFluidControlMode:

WriteFluidControlMode
=====================

Write a value for the fluid control mode. See `SendDCS`_ for state codes.

**fluidControlMode** (INPUT Bool)
  Fluid control mode (False=flow, True=pressure)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _WriteFlushSequence:

WriteFlushSequence
==================

Write the parameters for a given flush sequence number. See `SendDCS`_ for state codes.

**flushSeqNum** (INPUT USInt)
  Flush sequence number (1-5)

**gunPurgeTime** (INPUT UInt)
  Gun purge time (0-999 s)

**initFlushVol** (INPUT UInt)
  Initial flush volume (0-9999 cc)

**finalFlushVol** (INPUT UInt)
  Final flush volume (0-9999 cc)

**numWashCycles** (INPUT USInt)
  Number of wash cycles (0-99)

**strokesPerWashCycle** (INPUT USInt)
  Number of strokes per wash cycle (0-99)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _WriteMaterialReadyFlag:

WriteMaterialReadyFlag
======================

Write a value for the material ready flag. See `SendDCS`_ for state codes.

**materialReadyFlag** (INPUT Bool)
  Material ready flag

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _WriteMixFillSP:

WriteMixFillSP
==============

Write a value for the mix fill setpoint. See `SendDCS`_ for state codes.

**mixFillSP** (INPUT UInt)
  Mix fill setpoint (1-1600 cc/min for flow mode, 1-1500 psi for pressure mode, 0 for disabled)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _WriteRecipe:

WriteRecipe
===========

Write the parameters for a given recipe number. See `SendDCS`_ for state codes.

**recipeNum** (INPUT USInt)
  Recipe number (0-21)

**matNumA** (INPUT USInt)
  Material number for component A (0-30)

**matNumB** (INPUT USInt)
  Material number for component B (0, 31-34)

**flushSeqNumA** (INPUT USInt)
  Flush sequence number for component A (1-5)

**flushSeqNumB** (INPUT USInt)
  Flush sequence number for component B (1-5)

**mixRatioSP** (INPUT UInt)
  Mix ratio setpoint (0-5000, A*100 : B)

**potLifeTimeSP** (INPUT UInt)
  Potlife time setpoint (0-999 min)

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


.. _WriteUserID:

WriteUserID
===========

Write a value for the user ID. See `SendDCS`_ for state codes.

**userID** (INPUT STRING[9])
  User ID

**execute** (INOUT Bool)
  Execute command

**target** (INOUT `TypeTarget`_)
  Target to operate on

**state** (OUTPUT `TypeExecuteCmdState`_)
  Command state


UDTs
****

.. _TypeDcsAckFlags:

TypeDcsAckFlags
===============
Acknowledge flags for DCS

**nop** (Bool)
  No operation

**busy** (Bool)
  Busy

**ack** (Bool)
  Acknowledged successfully

**nak** (Bool)
  Not acknowledged

**err** (Bool)
  Error

.. _TypeEnableCmdState:

TypeEnableCmdState
==================
State for enable commands

**aborted** (Bool)
  Command aborted

**busy** (Bool)
  Command is busy

**error** (Bool)
  Command error

**valid** (Bool)
  Command is valid

**code** (Word)
  state code

.. _TypeExecuteCmdState:

TypeExecuteCmdState
===================
State for execute commands

**aborted** (Bool)
  Command aborted

**busy** (Bool)
  Command is busy

**error** (Bool)
  Command error

**done** (Bool)
  Command is done

**code** (Word)
  State code

.. _TypeFunctionCmdState:

TypeFunctionCmdState
====================
State for function commands

**error** (Bool)
  Command error

**done** (Bool)
  Command completed

**code** (Word)
  State code

.. _TypePd2kPumpStatusFlags:

TypePd2kPumpStatusFlags
=======================
Pump status flags

**off** (Bool)
  Pump is off

**standby** (Bool)
  Pump is in standby

**busy** (Bool)
  Pump is busy

**flushing** (Bool)
  Pump is flushing

**priming** (Bool)
  Pump is priming

.. _TypePd2kStatus:

TypePd2kStatus
==============
PD2K Integrated status data. See `StatusToUDT`_.

**inStandby** (Bool)
  System is in standby

**gun1Trigger** (Bool)
  Gun 1 trigger status

**gun2Trigger** (Bool)
  Gun 2 trigger status

**gun3Trigger** (Bool)
  Gun 3 trigger status

**safetyInterlock** (Bool)
  Safety interlock status

**systemMode** (USInt)
  System mode

**systemModeFlags** (`TypePd2kSystemModeFlags`_)
  System status flags

**pump1Status** (USInt)
  Pump 1 status

**pump1StatusFlags** (`TypePd2kPumpStatusFlags`_)
  Pump 1 status flags

**pump2Status** (USInt)
  Pump 2 status

**pump2StatusFlags** (`TypePd2kPumpStatusFlags`_)
  Pump 2 status flags

**pump3Status** (USInt)
  Pump 3 status

**pump3StatusFlags** (`TypePd2kPumpStatusFlags`_)
  Pump 3 status flags

**pump4Status** (USInt)
  Pump 4 status

**pump4StatusFlags** (`TypePd2kPumpStatusFlags`_)
  Pump 4 status flags

**actualMixFlow** (UInt)
  Actual mix flow/pressure (cc/min in flow mode, psi in pressure mode)

**actualMixRatio** (UInt)
  Actual mix ratio (A*100 : B)

**actualPotLifeRemaining** (UInt)
  Actual pot life time remaining (sec)

**activeRecipeNum** (USInt)
  Active recipe number

**activeMatNumA** (USInt)
  Active material number for component A

**activeMatNumB** (USInt)
  Active material number for component B

**activeFlushSeqNumA** (USInt)
  Active flush sequence number for component A

**activeFlushSeqNumB** (USInt)
  Active flush sequence number for component B

**activeRatioSP** (UInt)
  Active mix ratio setpoint (A*100 : B)

**activePotLifeSP** (UInt)
  Active pot life setpoint (min)

**actualPump1FlowRate** (UInt)
  Actual pump 1 flow rate (cc/min)

**actualPump2FlowRate** (UInt)
  Actual pump 2 flow rate (cc/min)

**actualPump3FlowRate** (UInt)
  Actual pump 3 flow rate (cc/min)

**actualPump4FlowRate** (UInt)
  Actual pump 4 flow rate (cc/min)

**actualPump1Pressure** (UInt)
  Actual pump 1 pressure (psi)

**actualPump2Pressure** (UInt)
  Actual pump 2 pressure (psi)

**actualPump3Pressure** (UInt)
  Actual pump 3 pressure (psi)

**actualPump4Pressure** (UInt)
  Actual pump 4 pressure (psi)

**activeGunNum** (USInt)
  Active gun number

**dcsAck** (USInt)
  DCS acknowledge

**dcsAckFlags** (`TypeDcsAckFlags`_)
  DCS acknowledge flags

**dcsRet0** (DWord)
  DCS return 0

**dcsRet1** (DWord)
  DCS return 1

**dcsRet2** (DWord)
  DCS return 2

**dcsRet3** (DWord)
  DCS return 3

**dcsRet4** (DWord)
  DCS return 4

**dcsRet5** (DWord)
  DCS return 5

**dcsRet6** (DWord)
  DCS return 6

**dcsRet7** (DWord)
  DCS return 7

.. _TypePd2kSystemModeFlags:

TypePd2kSystemModeFlags
=======================
System mode flags

**pumpOff** (Bool)
  System is off

**colorChange** (Bool)
  Color change is active

**colorChangePurgeA** (Bool)
  Color change is active, purging component A

**colorChangePurgeB** (Bool)
  Color change is active, purging component B

**colorChangeFilling** (Bool)
  Color change is active, filling

**mixFill** (Bool)
  System in mix fill mode

**mix** (Bool)
  System in mix mode

**mixIdle** (Bool)
  System in mix idle mode

**purgeA** (Bool)
  Purging component A

**purgeB** (Bool)
  Purging component B

**standbyMixReady** (Bool)
  Standby, mix ready

**standbyFillReady** (Bool)
  Standby, ready for fill

**standbyMixNotReady** (Bool)
  Standby, mix is not ready

**standbyAlarm** (Bool)
  Standby, alarm is active

**lineFillingFlushing** (Bool)
  Line is filling/flushing

**pumpPrimeFlush** (Bool)
  Priming/flushing a pump

**maintenance** (Bool)
  Maintenance

**solventPush** (Bool)
  Solvent push

.. _TypePumpControls:

TypePumpControls
================
Pump control inputs. See `PumpControl`_.

**powerOnCmd** (Bool)
  Power on command

**powerOffCmd** (Bool)
  Power off command

**quickStopCmd** (Bool)
  Quick stop command

**recipeChangeCmd** (Bool)
  Recipe change command

**clearAlarm** (Bool)
  Clear alarm command

**completeJob** (Bool)
  Complete job command

**mixCmd** (Bool)
  Mix mode command

**mixFillCmd** (Bool)
  Mix fill mode command

**recipePurgeCmd** (Bool)
  Recipe purge command

**standbyCmd** (Bool)
  Standby mode command

**mixCtrlSP** (UInt)
  Mix control SP (cc/min for flow mode, psi for pressure mode)

**nextRecipeNum** (UInt)
  Next recipe number

.. _TypeTarget:

TypeTarget
==========
Defines a target Profinet device. See :ref:`using-typetarget`.

**simulate** (Bool)
  Simulation mode select (1=on)

**pnDeviceNum** (UInt)
  Profinet device number

**simData** (Array[1..60] of DWord)
  Simulation data

