Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Name

Description

Return value

getName

returns the name of the submodule

name as string

getDirection

data direction of the submodule

DIR_IN - input

DIR_OUT - output

DIR_INOUT - input/output

getInputSize

the size of input data in bytes

input data size as int

getOutputSize

the size of output data in bytes

output data size as int

getIoDataObjectgetIoInputDataObject

getIoOutputDataObject

returns an io data object describing IoDataObject describing the input or the output data of the submodule.

IoDataObject instance

getSubslot

the subslot where the submodule shall be plugged into

subslot as int

getDeviceToControllerIoCsObject

getControllerToDeviceIoCsObject

returns the cs IoDataObject object used for input data (controller to device) or output data (device to controller)

The following script demonstrates how to access configuration data by printing information about all modules and submodules to the console.

Code Block
languagepy
###
# This function is called once the PNIO stack reports that
# the connection is established. At this point in time, you can
# retrieve the position of the individual data items.
###
def established():
    print("Connected to " + station_name)
    print("===== Connection params =====")
    
    # show basic connection params
    print("reduction ratio: " + str(config.getControllerReductionRatio()))
    print("wd factor: " + str(config.getWatchdogFactor()))
    print("data hold factor: " + str(config.getDataholdFactor()))

    # show module/submodule configuration
    print("===== Modules =====")
    # ignore module 0 as this is the dap module
    for i in range(1, len(config.getModulesToPlug())):
        module = config.getModulesToPlug().get(i)
        print("----- Module " + module.getName() + "-----")
        print("Description: " + module.getInfoText())
        print("Number of submodules: " + str(len(module.getAllSubmodules())))
        subs = module.getAllSubmodules()
        
        # show all submodules
        for j in range(len(subs)):
            sub = subs.get(j)
            direct = str(sub.getDirection())
            print("\t----- Submodule " + sub.getName() + "-----") + "-----")
            print("\tData direction: " + direct)
            if ("DIR_IN" == direct):
                print("\tInput size: " + str(sub.getInputSize()))
            if ("DIR_OUT" == direct):
                print("\tDatatOutput directionsize: " + directstr(sub.getOutputSize()))
            if ("DIR_ININOUT" == direct):
                print("\tInput size: " + str(sub.getInputSize()))
            if ("DIR_OUT" == direct):
                print("\tOutput size: " + str(sub.getOutputSize()))    
        print("")

Positional data within the frame is encoded in IoDataObject instances:

Name

Description

Return value

getFrameOffset

the offset of the data within a input frame (DIR_IN or DIR_INOUT) or an output frame (DIR_OUT or DIR_INOUT)

position as int

The following list provides a more complex example for accessing input and output data. It is based on the example project 09_pnio_io_mirror_new_api for the AC/CC. It sets a value to the 8bit output module and measures the time until the value is mirrored back on the first input module.

...

Code Block
languagepy
#########################################################################
# This script demonstrates how to use the scripting engine of the       #
# PNIO master.                                                          #
# It sets a value to the first output module and measures the time      #
# unitl that value is received on the first input module.               #
#########################################################################

from datetime import datetime


# global vars
in_pos = 0
out_pos = 0
lastval = 0xFF
trigger = False
expected_val = 0


###
# This function is called every time a frame is received
# from the device.
###
def process_input():
    global trigger
    global lastval
    global in_pos
    global expected_val
    global begin
    global end

    if trigger:
        if (expected_val == in_data[in_pos]):
            end = datetime.now()
            lastval = in_data[in_pos] & 0xFF

            # calculate round trip time
            roundtrip_time = end - begin

            # here you could do something with the
            # measured value

            # reactivate the trigger for the next value
            trigger = False


###
# This function is called every time a frame is about to be sent.
# It contains the complete frame incl. all headers. The position of the
# first data can be obtained via variable out_data_offset.
###
def process_output():
    global out_pos
    global trigger
    global out_data_offset
    global expected_val
    global begin

    pos = out_pos + out_data_offset
    new_val = out_data[pos]

    # if last value was received, create next value
    if not trigger:
        if (new_val != 0xFF) :
            new_val = new_val + 1
        else:
            new_val = 0
        begin = datetime.now()
        trigger = True

    # we always set the value in the process output
    out_data[pos] = new_val
    expected_val = new_val
    #pass


###
# This function is called once the PNIO stack reports that
# the connection is established. At this point in time, you can
# retrieve the position of the individual data items as shown
# below.
###
def established():
    global in_pos
    global out_pos

    # get the frame position of the first module, first submodule
    sub = config.getModulesToPlug().get(1).getAllSubmodules().get(0)
    in_pos = sub.getIoDataObjectgetIoInputDataObject().getFrameOffset()

    # get the frame position of the second module, first submodule
    sub = config.getModulesToPlug().get(2).getAllSubmodules().get(0)
    out_pos = sub.getIoDataObjectgetIoOutputDataObject().getFrameOffset()


###
# This function is called once when the connection was closed.
###
def closed():
    pass


###
# Called once during init of the script
###
if __name__ == "__main__":
    pass

...