|
| 1 | +# This file is part of the SmuView project. |
| 2 | +# |
| 3 | +# Copyright (C) 2022 François Revol <revol@free.fr> |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +import smuview |
| 24 | +import time |
| 25 | +import os |
| 26 | +import signal |
| 27 | +import threading |
| 28 | + |
| 29 | +# |
| 30 | +# This generates the mmdata.txt file as required by FlexBV to gather measurements |
| 31 | +# for the OpenBoardData project. |
| 32 | +# |
| 33 | +# https://pldaniels.com/flexbv/ |
| 34 | +# https://openboarddata.org/ |
| 35 | +# |
| 36 | + |
| 37 | +# Replace with the name and configuration of your multimeter |
| 38 | +# eg. "hp-3478a:conn=libgpib/hp3478a" |
| 39 | +device_name = "demo" |
| 40 | + |
| 41 | +# Replace with the name of the channel to relay to FlexBV, or "" for the first one |
| 42 | +# eg. "A1" |
| 43 | +channel_name = "" |
| 44 | + |
| 45 | +# Path to the mmdata.txt to generate or None for auto-detection |
| 46 | +mmdata_path = "mmdata.txt" |
| 47 | + |
| 48 | + |
| 49 | +## end of config variables |
| 50 | + |
| 51 | +print("mmdata.txt generator for FlexBV") |
| 52 | +print("") |
| 53 | + |
| 54 | +tmp_path = mmdata_path.replace(".txt","") + ".tmp" |
| 55 | + |
| 56 | +#if mmdata_path is None: |
| 57 | +# TODO: autodetect |
| 58 | + |
| 59 | +# Connect the demo device (it's just one!) |
| 60 | +print("Connecting to device '%s'" % device_name) |
| 61 | +dmm_dev = Session.connect_device(device_name)[0] |
| 62 | +channels = dmm_dev.channels() |
| 63 | +if channel_name == "": |
| 64 | + channel_name = list(channels.keys())[0] |
| 65 | +print("Using channel '%s'" % channel_name) |
| 66 | + |
| 67 | +# XXX: set on channel or on "" ?? |
| 68 | +dmm_conf = dmm_dev.configurables()[channel_name] |
| 69 | +dmm_conf.set_config(smuview.ConfigKey.MeasuredQuantity, (smuview.Quantity.Voltage, {smuview.QuantityFlag.Diode})) |
| 70 | + |
| 71 | +# Add a user device |
| 72 | +user_dev = Session.add_user_device() |
| 73 | +user_dev_tab = UiProxy.add_device_tab(user_dev) |
| 74 | + |
| 75 | +# Add a generic control view to the device tab |
| 76 | +control_view_1 = UiProxy.add_control_view(user_dev_tab, smuview.DockArea.TopDockArea, dmm_dev.configurables()[""]) |
| 77 | + |
| 78 | +# Add a demo control view to the device tab |
| 79 | +control_view_2 = UiProxy.add_control_view(user_dev_tab, smuview.DockArea.TopDockArea, dmm_dev.configurables()[channel_name]) |
| 80 | + |
| 81 | +# Add a value panel view to the device tab |
| 82 | +value_panel_view_1 = UiProxy.add_value_panel_view(user_dev_tab, smuview.DockArea.TopDockArea, dmm_dev.channels()[channel_name]) |
| 83 | + |
| 84 | +time.sleep(1) |
| 85 | + |
| 86 | +# Actually decimals but well |
| 87 | +digits = 3 |
| 88 | +if smuview.ConfigKey.Digits in dmm_conf.getable_configs(): |
| 89 | + digits = dmm_conf.get_int_config(smuview.ConfigKey.Digits) |
| 90 | + |
| 91 | +quitting = threading.Event() |
| 92 | + |
| 93 | +def handler(signum, frame): |
| 94 | + quitting.set() |
| 95 | + |
| 96 | +signal.signal(signal.SIGINT, handler) |
| 97 | + |
| 98 | +while not quitting.is_set(): |
| 99 | + time.sleep(0.5) |
| 100 | + if os.path.exists(mmdata_path): |
| 101 | + continue |
| 102 | + # FlexBV digested the previous file |
| 103 | + s = dmm_dev.channels()[channel_name].actual_signal() |
| 104 | + # get the unit back from the signal name |
| 105 | + unit = s.name().split("[")[1].split(" ")[0] |
| 106 | + u_in = s.get_last_sample(True)[1] |
| 107 | + with open(tmp_path, 'w', encoding="utf-8") as f: |
| 108 | + f.write(("{:."+str(digits)+"f}").format(u_in) + unit) |
| 109 | + os.rename(tmp_path, mmdata_path) |
| 110 | + |
| 111 | +Session.remove_device(user_dev) |
| 112 | +Session.remove_device(dmm_dev) |
| 113 | +print("Exited") |
0 commit comments