Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables in a SCPI string
#1
Hi, I'm new to Python programming and have been unable to find help with a question.
Can a variable be used within a SCPI command? I would like to set a variable like x = 10000 and then put that variable within the command. I am using Visual Studio Code.
Example:
:DDS:MODE:FREQ <freq>

Set Frequency to 10000
scope.write(":DDS:FREQ 10000")

Can a variable be inserted instead of the actual number?
x = 10000
scope.write(":DDS:FREQ x")

Any help would be appreciated.
Reply
#2
Example use f-string.
>>> x = 10000
>>> mode = f':DDS:FREQ {x}'
>>> mode
':DDS:FREQ 10000'
Reply
#3
In SCPI (Standard Commands for Programmable Instruments), you typically cannot directly insert variables within commands. SCPI commands are usually static strings that are sent to the instrument for execution. However, you can dynamically generate the command string by concatenating the variable value with the command string before sending it to the instrument.

In your case, using Visual Studio Code, you can achieve this by constructing the command string with the variable value and then sending it to the instrument. Here's an example:
# Define the variable
x = 10000

# Construct the command string with the variable value
command = ":DDS:FREQ " + str(x)

# Send the command string to the instrument
scope.write(command)
In the code snippet above, we concatenate the value of the variable x with the command string ":DDS:FREQ " using the + operator. The str() function is used to convert the variable value to a string before concatenation. Finally, the resulting command string is sent to the instrument using scope.write(command).

Note that the exact implementation may depend on the specific instrument and the library or interface you are using to communicate with it. The above example assumes a Python environment and demonstrates the general approach to dynamically generate and send commands with variable values.
Reply
#4
That works, I was just about to give up. This did what I was trying to achieve.

x = 12000
mode = f' :dds:freq {x}'
scope.write(mode)

Thank you so much.

Tom
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Referencing string names in df to outside variables illmattic 1 1,366 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Creating local variables from a string peckjonk 2 2,177 Feb-15-2020, 06:07 PM
Last Post: ndc85430
  How do I print "string involved" if one or more of my variables are strings? Shellburn 3 3,564 Oct-05-2017, 06:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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