如何使用 Python 在热键按下时读取万用表并粘贴到 Excel 电子表格单元格
以下 Python 脚本使用 PyVISA / LabInstruments 读取 Rigol DM3058(E) 万用表,并在按 Shift+Alt+Q 时自动将电压读数粘贴到打开的电子表格中。
首先,使用以下命令将 LabInstruments 克隆到当前目录
clone_labinstruments.sh
git clone https://github.com/ulikoehler/LabInstruments.git现在安装以下 requirements.txt 中的依赖项
requirements.txt
system_hotkey310
pyperclip
pyvisa
pyvisa-py并运行脚本:
read_multimeter_paste_excel.py
#!/usr/bin/env python3
"""
This script allows quick reading of multimeter, automatically pasting the reading
into the current e.g. excel spreadsheet and pressing "Enter" to advance to the next cell.
Press Shift+Alt+Q to read the multimeter and paste the reading into the current cell.
"""
import subprocess
import pyautogui
import time
import re
import pyperclip
import sexpdata
import pyvisa
from LabInstruments.DM3058 import DM3058
import locale
locale.setlocale(locale.LC_NUMERIC, f"{locale.getdefaultlocale()[0]}.utf-8")
rm = pyvisa.ResourceManager()
rm.list_resources()
inst = rm.open_resource('USB0::6833::2500::DM3R245003970::0::INSTR')
multimeter = DM3058(inst)
multimeter.set_speed("S")
multimeter.mode_dc_voltage()
def run():
voltage = multimeter.read_voltage()
locale_formatted_voltage = locale.format_string("%.5f", voltage, grouping=False)
print("Voltage: ", locale_formatted_voltage)
# Copy locale-formatted voltage to clipboard
pyperclip.copy(locale_formatted_voltage)
# Paste into cell
pyautogui.hotkey('ctrl', 'v')
# Wait for excel
time.sleep(0.1)
# Press Enter key to save
pyautogui.press('enter')
# Register hotkeys
from system_hotkey import SystemHotkey
hk = SystemHotkey()
hk.register(('shift', 'alt', 'q'), callback=lambda x: run())
# Wait for exit
while True:
time.sleep(10)Check out similar posts by category:
Electronics, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow