Скрипт pyvisa работает только при пошаговом выполнении кода ⇐ Python

Программы на Python
Anonymous
Скрипт pyvisa работает только при пошаговом выполнении кода

Сообщение Anonymous »

Я написал небольшой скрипт на Python, чтобы включить выход тройного источника питания HAMEG HMG4040.
Я использую pyvisa 1.14.1 вместе с NI-VISA 20.0

Код: Выделить всё

import pyvisa
import time

# Initialize the PyVISA resource manager
pyvisa.log_to_screen()
rm = pyvisa.ResourceManager()

# List all connected VISA devices
def list_devices():
devices = rm.list_resources()
if devices:
print("Available VISA devices:")
for idx, device in enumerate(devices):
print(f"{idx + 1}: {device}")
else:
print("No VISA devices found.")
return devices

# Function to query the device
def query_device(resource_name, command):
try:
# Open the device resource
with rm.open_resource(resource_name) as instrument:
instrument.timeout = 5000  # Set timeout to 1000 ms (1 seconds)
# Write the query command to the device
instrument.write(command)
# Read the response from the device
response = instrument.read()
return response
except Exception as e:
print(f"Error querying device: {e}")
return None

# Function to query the device
def send_cmd_device(resource_name, command):
try:
# Open the device resource
with rm.open_resource(resource_name) as instrument:
instrument.timeout = 5000  # Set timeout to 1000 ms (1 seconds)
# Write the query command to the device
instrument.write(command)
return 1
except Exception as e:
print(f"Error querying device: {e}")
return None

# Main function
if __name__ == "__main__":
# List available devices
devices = list_devices()

if devices:
# Choose the device you want to query
resource_name = devices[0]  # Use the first detected device for this example

# Command to send to the device (replace with actual command)
command = "*IDN?"

# Query the device and get the response
response = query_device(resource_name, command)

if response:
print(f"Response from HMG4040: {response}")
else:
print("No response from the device.")

state = input("specify PSUP state: [ON/OFF]\n")

#select and activate output 1,2 and 4

with rm.open_resource(resource_name) as instrument:
instrument.timeout = 1000  # Set timeout to 1000 ms (1 seconds)
instrument.write("INST OUT1")
instrument.write("OUTP:SEL " + state)
instrument.write("INST OUT3")
instrument.write("OUTP:SEL " + state)
instrument.write("INST OUT4")
instrument.write("OUTP:SEL " + state)
instrument.write("OUTP:GEN " + state)

send_cmd_device(resource_name, "INST OUT1")
send_cmd_device(resource_name, "OUTP:SEL " + state)
send_cmd_device(resource_name, "INST OUT3")
send_cmd_device(resource_name, "OUTP:SEL " + state)
send_cmd_device(resource_name, "INST OUT4")
send_cmd_device(resource_name, "OUTP:SEL " + state)
send_cmd_device(resource_name, "OUTP:GEN " + state)

else:
print("No devices to query.")
Если я закомментирую строки send_cmd_device(), скрипт будет работать нормально как в режиме отладки, так и при обычном выполнении Python.
Однако, если Эту часть я закомментирую:

Код: Выделить всё

        with rm.open_resource(resource_name) as instrument:
instrument.timeout = 1000  # Set timeout to 1000 ms (1 seconds)
instrument.write("INST OUT1")
instrument.write("OUTP:SEL " + state)
instrument.write("INST OUT3")
instrument.write("OUTP:SEL " + state)
instrument.write("INST OUT4")
instrument.write("OUTP:SEL " + state)
instrument.write("OUTP:GEN "  + state)
Скрипт работает только в том случае, если я поставлю точку останова и пройдусь в режиме отладки по каждой строке, включая строки send_cmd_device().
Может ли кто-нибудь объяснить, почему сценарий не работает в обычном режиме на полной скорости при использовании вызова метода? Я пробовал везде добавлять задержки, но это не помогло.

Подробнее здесь: https://stackoverflow.com/questions/790 ... rough-code

Вернуться в «Python»