Calling and Using Python Code from ATEasy

Knowledge Base Article # Q200341

Read Prior Article Read Next Article
Summary Article explain how to integrate Python code to your ATEasy application using the ATEasy Python Driver. The Driver allows you to use Python Modules (.py files), call Methods, classes and more.
  
Login to rate article

Introduction

NOTE: This article assumes knowledge of ATEasy and Python.

Overview

This article introduce the Python ATEasy driver that enables you to call Python scripts from ATEasy, The ATEasy Python Driver is a wrapper of the Python/C API that allows you to use and call Python modules (.py files) in ATEasy. ATEasy 2024 comes with ATEasy Python project that includes the following:  
  • Python.prj - ATEasy Python example project file
  • Python.drv - ATEasy Python driver file
  • Python.prg - ATEasy Python example program file
  • Python.sys - ATEasy Python example program system file
  • Python.py - Python example program script file used by the ATEasy example program

Python/C API (Dynamic Link Library)

In order to use the ATEasy Python program you will need first to download Python and install it on your system. The Python installation package gives you access to the Python/C API's Dynamic Link Library (python.dll).

Open the Python Example project

The Python example project can be open from the the ATEasy Drivers folder, For ATEasy 2024 use following path: C:\Program Files (x86)\Marvin Test Solutions\ATEasy\2024\Drivers.
Figure 1: Open Python Example project
Figure 1: Open Python Example project


Example Project


The provided example gives an overview of how to access Classes, Methods, and Variables of a Python Module.

Figure 2: Python Example Project
Figure 2: View of the Python Example Project


ATEasy Python Driver Operation

Below is the ATEasy Python Driver tree command:
Figure 3: Driver Commands
Figure 3: Driver Commands


Initializing and Closing the Python Interpreter
Initializing the Python Interpreter is done automatically by the driver OnInit() event, it calls the Py_Initialize() DLL API  (user can use the driver PYTHON Initialize() command to explicitly initialize the Python Interpreter).
Closing the Python Interpreter is done automatically by the driver OnEnd() event, it calls the Py_Finalize() DLL API which closes the Python Interpreter (user can also use the driver PYTHON Finalize() command to explicitly close the Python Interpreter).

Variables and Data Types - we use the following global variables:
Variable name Variable TypeDescription
avArrayVariant[1]Array of variants
aucStrBytesByte[1]Array of bytes
avArgvVariant[2]Array of Variants
hClientSocketASocketA Winsock socket handle
hpyArgshpyObjectPointer to a Python object representing the tuple array
hpyClasshpyObjectPointer to a Python object returned from PYTHON Get Object FromAttribute() driver command
hpyClassInstancehpyObjectReturned pointer to a Python object
hpyClientSockethpyObjectPointer to a Python Class instance class variable
hpyListhpyObjectPointer to a Python list object
hpyModulehpyObjectPointer to a Python object returned from calling the import example Python module script file
hpyReturnhpyObjectReturned Pointer to a Python object


Example Code: Instantiate Python Class With Initializer From ATEasy
The following example Instantiate Python Class With Initializer From ATEasy, fills an array, and returns the first item of the array
hpyModule=PYTHON Module Import("Python")
if hpyModule<>0
TestStatus=PASS
else
TestStatus=FAIL
endif
! nInitNumber is a const short = 21
hpyObject=PYTHON Get Object FromAttribute(hpyModule, "PythonClass")
hpyArgs=PYTHON Set Args Tuple({nInitNumber})

! Reference and retrieve PythonClass instance class_variable
hpyClassInstance=PYTHON Call Object(hpyObject, hpyArgs)
hpyReturn=PYTHON Get Object FromAttribute(hpyClassInstance, "class_variable")
TestResult=PYTHON Get Long(hpyReturn)

Code Explanation:
hpyModule=PYTHON Module Import("Python"): Import the module named "Python.py": This function returns a Python Object pointer to the imported module, or NULL if an error occurs.
hpyObject=PYTHON Get Object FromAttribute(hpyModule, "PythonClass"): This function passed the hpyModule object, representing the attribute name instead of a C string
hpyArgs=PYTHON Set Args Tuple({nInitNumber}): Create a new tuple of length =21
hpyClassInstance=PYTHON Call Object(hpyObject, hpyArgs): Reference and retrieve PythonClass instance class_variable
hpyReturn=PYTHON Get Object FromAttribute(hpyClassInstance, "class_variable"): Reference hpyObject from attribute name.
TestResult=PYTHON Get Long(hpyReturn): set TestResult to the first long value in the tuple array

Example Code: Return a Struct
The following example reads a structure from the Python.py file, and verify the values of the structure members.
!Python.py file module  section
def GetStruct():
    
    volt=3.64
    cur=3.1
    res=3.64/0.0031

    myStruct=struct.pack('@3d',volt,cur,res)
    
    return myStruct

ATEasy code
hpyModule=PYTHON Module Import("Python")
if hpyModule<>0
TestStatus=PASS
else
TestStatus=FAIL
endif

hpyObject=PYTHON Get Object FromAttribute(hpyModule, "GetStruct")
TestResult=PYTHON Get Data(PYTHON call Object NoArg(hpyObject), &stMeasurement)
if stMeasurement.dVoltage=3.64
TestStatus=PASS
else
TestStatus=FAIL
exittest
endif

if stMeasurement.dCurrent=3.1
TestStatus=PASS
else
TestStatus=FAIL
exittest
endif

if stMeasurement.dResistance=(stMeasurement.dVoltage/0.0031)
TestStatus=PASS
else
TestStatus=FAIL
exittest
endif


Code Explanation:
hpyModule=PYTHON Module Import("Python"): Import the module named "Python.py": This function returns a Python Object pointer to the imported module, or NULL if an error occurs.
hpyObject=PYTHON Get Object FromAttribute(hpyModule, "GetStruct"): returns the python object named "GetStruct" form the file
TestResult=PYTHON Get Data(PYTHON call Object NoArg(hpyObject), &stMeasurement): fill the ATEasy declared structure named stMeasurement with the data form the module.
if stMeasurement.dVoltage=3.64: verifying that the dVoltage structure member is equal to 3.64
if stMeasurement.dCurrent=3.1: verifying that the dCurrent structure  member is equal to 3.1

Conclusion

Embedding the Python interpreter using the ATEasy Python driver allowing users to utilize Python's new and exiting code, and python libraries to enhance and expand ATEasy application.
Article Date 2/11/2025 , 2/12/2025
Keywords Python, ATEasy, Driver, Embedded, DLL, Embedding Python


Login to rate article

1 ratings | 5 out of 5
Read Prior Article Read Next Article