|
| 1 | +# This is an introductory python script originally written with pycharm IDE. This shows how to use tm_devices |
| 2 | +# for a relatively realistic short automation routine. Its goal is to find the maximum peak to peak |
| 3 | +# value of a signal on CH1 for a period of three seconds for 10 separate periods. |
| 4 | + |
| 5 | +# to use this script please create a folder under "C" drive called "Test" |
| 6 | + |
| 7 | +# This program uses tm_devices. see https://pypi.org/project/tm-devices/ for more details. |
| 8 | +# Device Manager interacts with pyvisa (and if a third party VISA is installed pyvisa interacts |
| 9 | +# with that). It has the routines that control instruments. |
| 10 | +from tm_devices import DeviceManager |
| 11 | + |
| 12 | +# Now we import the specific drivers that control the individual instrument, in this case a B version 6-series |
| 13 | +from tm_devices.drivers import MSO6B |
| 14 | +# Import time so we can use time.sleep() |
| 15 | +import time |
| 16 | +# Import Numpy (https://pypi.org/project/numpy/) to use numpy's array tools |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +# set up initial values, including the numpy array. |
| 20 | +i = 0 |
| 21 | +NumberOfRuns = 10 |
| 22 | +MaxPKtoPK = np.zeros(NumberOfRuns) |
| 23 | + |
| 24 | +# Set up and connect to scope and check its idn string to ensure we connected. verbose in the DeviceManager |
| 25 | +# means you can see all commands going to instrument and all responses to queries which is useful for debug, or |
| 26 | +# not. Here we chose "false" so that we do not print those out. |
| 27 | +with DeviceManager(verbose=False) as device_manager: |
| 28 | + scope: MSO6B = device_manager.add_scope("MSO68B-B025464", connection_type="USB" ) |
| 29 | + print(scope.idn_string) |
| 30 | + |
| 31 | + # default the oscilloscope so that it is in a standard condition |
| 32 | + scope.reset() |
| 33 | + |
| 34 | + # Run Autoset to get the signal set up on screen. Followed by OPC query to ensure it completes. |
| 35 | + scope.commands.autoset.write("EXEC") |
| 36 | + scope.commands.opc.query() |
| 37 | + # Set up horizontal record length to 1 million points. |
| 38 | + scope.commands.horizontal.mode.write("MANual") |
| 39 | + scope.commands.horizontal.recordlength.write(1e6) |
| 40 | + # Ad Meas1 as a PK2PK measurement and display all stats in the badge |
| 41 | + # Then add a second Risetime measurement using a different method within tm_devices. |
| 42 | + scope.add_new_measurement("MEAS1", "PK2PK", "CH1") |
| 43 | + scope.commands.measurement.meas[1].displaystat.enable.write("ON") |
| 44 | + scope.commands.measurement.addmeas.write("RISETIME") |
| 45 | + scope.commands.measurement.meas[2].source.write("CH1") |
| 46 | + |
| 47 | + # stop the scope |
| 48 | + scope.commands.acquire.state.write("OFF") |
| 49 | + # This loop first clear the previous measurement values, starts the scope and runs it for 3 seconds, stops the |
| 50 | + # scope. and then add the Maximum peak to peak measurement value to the numpy array. |
| 51 | + while i < NumberOfRuns: |
| 52 | + scope.commands.clear.write() |
| 53 | + scope.commands.acquire.state.write("ON") |
| 54 | + time.sleep(3) |
| 55 | + scope.commands.acquire.state.write("OFF") |
| 56 | + MaxPKtoPK[i] = scope.commands.measurement.meas[1].results.allacqs.maximum.query() |
| 57 | + i = i+1 |
| 58 | + # Create the output file in the folder "Test" and use a numpy command to save the data into it. |
| 59 | + filename = "C:\\Test\\MaxAmp.txt" |
| 60 | + np.savetxt(filename, MaxPKtoPK) |
| 61 | + |
0 commit comments