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
Example Project
The provided example gives an overview of how to access Classes, Methods, and Variables of a Python Module.
Figure 2: View of the Python Example Project
ATEasy Python Driver Operation
Below is the ATEasy Python Driver tree command:
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 Type | Description |
---|
avArray | Variant[1] | Array of variants |
aucStrBytes | Byte[1] | Array of bytes |
avArgv | Variant[2] | Array of Variants |
hClientSocket | ASocket | A Winsock socket handle |
hpyArgs | hpyObject | Pointer to a Python object representing the tuple array |
hpyClass | hpyObject | Pointer to a Python object returned from PYTHON Get Object FromAttribute() driver command |
hpyClassInstance | hpyObject | Returned pointer to a Python object |
hpyClientSocket | hpyObject | Pointer to a Python Class instance class variable |
hpyList | hpyObject | Pointer to a Python list object |
hpyModule | hpyObject | Pointer to a Python object returned from calling the import example Python module script file |
hpyReturn | hpyObject | Returned 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.