Python Forum
Wish to write a code for an electronic load and a power supply for measuring Eff
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wish to write a code for an electronic load and a power supply for measuring Eff
#1
Hi there,
I am pretty new in python.
I need some help from your side.

Willing to run a program like this,

import pyvisa

# Initialize the resource manager and open the connection

rm = pyvisa.ResourceManager()
resource_address = "Your_VISA_Address"  # Replace with your actual VISA resource address
power_supply = rm.open_resource(resource_address)

try:
    # Identify the power supply
    print(power_supply.query("*IDN?"))

    # Set parameters (example values; replace as needed)
    voltage = 12.0  # Volts
    current = 5.0   # Amperes

    # Configure the power supply
    power_supply.write(f"VOLT {voltage}")
    power_supply.write(f"CURR {current}")

    # Enable the output
    power_supply.write("OUTP ON")

    # Measure output voltage and current
    measured_voltage = float(power_supply.query("MEAS:VOLT?"))
    measured_current = float(power_supply.query("MEAS:CURR?"))

    # Calculate power efficiency
    input_power = voltage * current  # Input power
    output_power = measured_voltage * measured_current  # Output power
    efficiency = (output_power / input_power) * 100

    print(f"Input Power: {input_power} W")
    print(f"Output Power: {output_power} W")
    print(f"Efficiency: {efficiency:.2f} %")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Turn off the output and close the connection
    power_supply.write("OUTP OFF")
    power_supply.close()
Send me your comments
Reply
#2
What is the question?
Does it work?
Hasan2025 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
It works sometime but sometime not.
I am using VS code IDE or Jupyter.
try:, finally: those does not workout.
Reply
#4
I would use the closing context-manager and won't catch any Exception:

from contextlib import closing

import pyvisa


def get_efficiency(resource_address: str, voltage: float, current: float) -> tuple[str, float, float, float]:
    with closing(pyvisa.ResourceManager()) as rm:
        # closing closes rm before leaving this block
        with closing(rm.open_resource(resource_address)) as power_supply:
            # closing() closes power_supply before leaving this block
            idn = power_supply.query("*IDN?")
            power_supply.write(f"VOLT {voltage}")
            power_supply.write(f"CURR {current}")
            power_supply.write("OUTP ON")
            measured_voltage = float(power_supply.query("MEAS:VOLT?"))
            measured_current = float(power_supply.query("MEAS:CURR?"))
            power_supply.write("OUTP OFF")

    # after all values are available, the calculation could be done without active
    # ResourceManager and the resource
    input_power = voltage * current
    output_power = measured_voltage * measured_current
    efficiency = (output_power / input_power) * 100
    
    return idn, input_power, output_power, efficiency


def main():
    idn, input_power, output_power, efficiency = get_efficiency("address_of_device", 12.0, 5.0)

    print(f"IDN: {idn}")
    print(f"Input Power: {input_power} W")
    print(f"Output Power: {output_power} W")
    print(f"Efficiency: {efficiency:.2f} %")



if __name__ == "__main__":
    main()
Then run it a few times until an Exception happens. Because there is no try ... except, you'll get the whole Traceback inclusive the thrown Exception itself.

I can't test this, because I don't have this expansive Devices for measurement.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 2 1,761 Mar-08-2025, 03:02 AM
Last Post: bevisandrew
  cheap and low power python device kucingkembar 6 1,738 Jun-27-2024, 12:41 PM
Last Post: kucingkembar
  UART write binary code trix 3 1,912 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  write code that resides in parent directory franklin97355 3 1,570 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  Python Coding in Power BI Visual vjrans 0 867 Jan-31-2024, 07:54 AM
Last Post: vjrans
  Power Shells vs Compile and Run programs? RockBlok 2 1,276 Jan-13-2024, 09:08 PM
Last Post: RockBlok
Question Python and Power BI Desktop dangermaus33 1 2,038 Jan-19-2023, 06:54 AM
Last Post: GetOnData
  Model of supply and demand maeva 4 3,656 Nov-18-2021, 09:19 AM
Last Post: maeva
  Raising numbers to power ** GJG 3 3,656 Mar-23-2021, 03:43 PM
Last Post: deanhystad
  How to write a code with İF function? Aycaaxx 1 2,711 Nov-03-2020, 05:46 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020