LD - file writing services

The functions below enable you to write sequentially (from the top to the bottom) the source code of a program written in LD language. The program is written ung after rung. Items are placed sequentially on the rung from the left to the right. Files are open using the paFileOpenWriteSrc function. When complete, they must be closed by calling the paFileClose function.

Use the following functions for inserting rungs in the diagram:

paFLdCommentLine - Add a comment line (network break)
paFLdStartRung - Start a new rung
paFLdEndRung - Terminate the rung
paFLdDivergence - Start an "OR" divergence on the current rung
paFLdConvergence - Terminate an "OR" divergence on the current rung
paFLdNextBranch - Add a parallel branch to the current divergence

Use the following functions for adding items on the current rung:

paFLdContact - Add a contact
paFLdCoil - Add a coil
paFLdJump - Add a jump instruction
paFLdReturn - Add a <return> jump instruction
paFLdBlock - Add a block
paFLdHorzSegment - Add a horizontal segment line

See an example...

Below are detailed information about these functions:

paFLdCommentLine - add a network break

OK := paFLdCommentLine (FID, TEXT)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
TEXT : STRING; Text written on the network break
OK : BOOL; True if successful

Description:

This function adds a comment line (network break) in between rungs.

other functions

paFLdStartRung - start a new rung

OK := paFLdStartRung (FID, LABEL)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
LABEL : STRING; Rung label name (empty string if no label defined)
OK : BOOL; True if successful

Description:

This function starts a new rung. After calling this function, you can call other functions to add items on the rung from the left to the right. When complete, you need to call the paFLdEndRung function to terminate the rung.

other functions

paFLdEndRung - terminates the rung

OK := paFLdEndRung (FID)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
OK : BOOL; True if successful

Description:

This function terminates the current rung. You need to call it before starting another rung or adding a comment line.

other functions

paFLdDivergence - starts a divergence

OK := paFLdDivergence (FID)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
OK : BOOL; True if successful

Description:

This function starts a "OR" divergence on the current rung. After calling this function, items can be added on the first (top) branch of the divergence. Parallel branches can be added by calling the paFLdNextBranch function. When complete, you need to call the paFLdConvergence function to terminate the divergence.

other functions

paFLdConvergence - terminates a divergence

OK := paFLdConvergence (FID)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
OK : BOOL; True if successful

Description:

This function terminates the current "OR" divergence. After calling this function, items can be added after the divergence on the parent rung.

other functions

paFLdNextBranch - add a branch to a divergence

OK := paFLdNextBranch (FID)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
OK : BOOL; True if successful

Description:

This function starts a new parallel branch in the current "OR" divergence. After calling this function, items can be added on the new branch.

other functions

paFLdContact - add a contact

OK := paFLdContact (FID, TYP, NAME)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
TYP : STRING; Type of contact (see notes)
NAME : STRING; Name of the variable attached to the contact
OK : BOOL; True if successful

Description:

This function adds a contact on the current rung. The following values are available for the TYP parameter:

_LD_DIR Normal contact
_LD_INV Negated contact
_LD_P Positive pulse contact
_LD_N Negative pulse contact

other functions

paFLdCoil - add a coil

OK := paFLdCoil (FID, TYP, NAME)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
TYP : STRING; Type of coil (see notes)
NAME : STRING; Name of the variable attached to the coil
OK : BOOL; True if successful

Description:

This function adds a coil on the current rung. The following values are available for the TYP parameter:

_LD_DIR Normal coil
_LD_INV Negated coil
_LD_P Positive pulse coil
_LD_N Negative pulse coil
_LD_SET Set coil
_LD_RESET Reset coil

other functions

paFLdJump - add a jump symbol

OK := paFLdJump (FID, LABEL)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
LABEL : STRING; Target label name
OK : BOOL; True if successful

Description:

This function adds a jump instruction on the current rung.

other functions

paFLdReturn - add a <return> jump symbol

OK := paFLdReturn (FID)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
OK : BOOL; True if successful

Description:

This function adds a <RETURN> jump instruction on the current rung.

other functions

paFLdBlock - add a block

OK := paFLdBlock (FID, NAME, INSTANCE, INPUTS, OUTPUTS)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
NAME : STRING; Type of block
INSTANCE : STRING; Name of the instance in case of a function block
INPUTS : STRING; List of input values (see notes)
OUTPUTS : STRING; List of output values (see notes)
OK : BOOL; True if successful

Description:

This function adds a block on the current rung. In case of a function block, you must specify the name of the instance. Blocks are connected to the rung by their first input and output. Name of additional inputs and outputs are passed in strings separated by comas. Below is the example of an "addition" block with 2 additional inputs calle 'i1' and 'i2'and 1 additional output calle 'q':

    paFLdStartRung (f, '');
        paFldBlock (f, '+', '', 'i1,i2', 'q');
        paFldCoil (f, _LD_DIR, '');
    paFLdEndRung (f);

other functions

paFLdHorzSegment - add a horizontal segment

OK := paFLdHorzSegment (FID)

Parameters:

FID : DINT; File identifier answered by paFileOpenWriteSrc
OK : BOOL; True if successful

Description:

This function adds a horizontal line segment on the current rung. This can be used for aligning items vertically in the diagram.

other functions

Example

Below is an example of script that generates a LD file in the target project:

    // make LD code
    f := paFileOpenWriteProgramSrc ('ProgLD');
    if f <> 0 then
        // comment line
        paFLdCommentLine (f, 'Generated by script');
        // a simple rung finishing with a jump
        paFLdStartRung (f, '');
            paFldContact (f, _LD_DIR, 'Cond');
            paFldJump (f, 'TheEnd');
        paFLdEndRung (f);
        // a rung with contacts and coils
        paFLdStartRung (f, '');
            paFldContact (f, _LD_DIR, 'V1');
            paFLdDivergence (f);
                paFldContact (f, _LD_DIR, 'V2');
            paFLdNextBranch (f);
                paFldContact (f, _LD_DIR, 'V3');
            paFLdConvergence (f);
            paFldCoil (f, _LD_INV, 'V');
        paFLdEndRung (f);
        // a rung with blocks
        paFLdStartRung (f, '');
            paFldBlock (f, '+', '', 'i1,i2', 'q');
            paFldBlock (f, 'Blink', 'Blinker', 't#1s', '');
            paFldCoil (f, _LD_DIR, '');
        paFLdEndRung (f);
        // a rung with a label and a RETURN instruction
        paFLdStartRung (f, 'TheEnd');
            paFldReturn (f);
        paFLdEndRung (f);
        // close the file
        paFileClose (f);
    end_if;

Here is the LD program generated by the script: