Python RSA API Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 33

Scroll down to view the document on your mobile browser.
How to Use the RSA API in Python *See end of guide for more information about using a separate Python module to import structs and enums One. Before you do anything else, download and install Anaconda: http://continuum.io/downloads. Anaconda is a 64-bit Python distribution that already has graphing and numerical processing modules installed. Having Anaconda before playing with the RSA_API will mitigate confusion and remove the need to explain the process of installing and managing Python modules. These examples are based on Python 3.6, but Python 2.7 code can also be provided if necessary. Two. Download and install the most recent version of the RSA_API. As of 5/17 the most recent version is 3.9.0029 and can be downloaded here: http://www.tek.com/model/rsa306-software Download the documentation for the API. As of 5/17 the most recent version is 077-1031-03 and can be downloaded here: http://www.tek.com/spectrum-analyzer/rsa306-manual-0 Three. This document assumes you have a basic knowledge of Python programming (using variables, calling functions, knowledge of data types, running Python scripts from the command line, etc.) Four. I have a legend. data types = bold, variables = underlined, and functions/methods = italics()  Intro All of the magic in the API happens in RSA_API.dll. A dll is a Dynamic Link Library, which contains precompiled functions that can be accessed by a script or program. This dll was written in and for C, so in order to import the dll and access its functions in Python, we have to use Python’s ctypes module, which allows Python to understand and use data types that are native to C. Navigating this translation can be tricky if you have never written code in C before or are unfamiliar with programming in general.   Object-Oriented Programming Python is an object-oriented language. You can think of objects as complex data types that contain functions (or methods to be more semantically precise) and attributes that can be used by the rest of the program. The RSA_API.dll provides you with an object that has a whole bunch of methods that you can use to send commands to and get data from the connected RSA. In all the following examples, my object is called rsa. In order to use any of the methods contained in the object, use the object.method() notation. For example, to use DEVICE_Disconnect() on the RSA, write the following:  All the methods listed in the API documentation are accessed this way, which you will see as you read further.
How to Use the RSA API in Python *See end of guide for more information about using a separate Python module to import structs and enums Working with ctypes variables. Creating a variable in Python using a C data type is easy after you import the ctypes module. Let’s use the refLevel variable as an example. Based on the API documentation, all functions that utilize refLevel expect to see a double data type. Ctypes can create ctypes objects with a C data types that have certain attributes we can use. To create this variable as a double with a value of 0, type the following:  Creating arrays has one more step between you and a fully usable variable, but it isn’t complicated once you understand how it works. Let’s create an array that contains a set of 1024 points as floats. First, we create a variable that allocates enough space in memory to hold the entire array by multiplying the desired data type (float in this case) by the array length. For those familiar with C, this is like using malloc(). For example, the following code allocates enough memory for the array and then initializes it.   Note that ctypes data can’t always be used directly by Python. In general, if you want to use the number contained in a variable, you need to use the .value attribute. Take the following scenario for example:  This code throws an error because Python can’t add a standard Python int to a c_double.   However, if I change it slightly by using the .value attribute (much like passing an argument by value, which we’ll talk about later) Python uses the value contained in bla rather than using the whole C-packaged data type. Everything is peachy.

Navigation menu