Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

SCPI Commands and Variables

Solved!
Go to solution

Good morning, 

 

I'm writing some software where I need to test 58 switches at 35 different states (decimal 0 to decimal 34).  Rather than write the commands to set the state the port value over and over again for each switch, is it possible to use a for loop to change the decimal value (0) in the below section of code to 1, 2,3 and so on?  I haven't found any way to  insert a changing variable into a SCPI command.  

    Private Sub btnSwitchTest_Click(sender As Object, e As EventArgs) Handles btnSwitchTest.Click
        Dim Port As Integer

        DAQ = li.ibdev(0, 9, NO_SECONDARY_ADDR, TIMEOUT, EOTMODE, EOSMODE) '''''GPIB addressing.  

        For Port = 0 To 34

            '''sets all switches to 0
            wrtbuf = "SOUR:DIG:DATA:BYTE 0, (@201)" '---->>>> the 0 is the variable that I want to change.
            li.ilwrt(DAQ, wrtbuf, Len(wrtbuf))
            System.Threading.Thread.Sleep(1000)

            '''record voltages here/havent written this code yet.

Next Port

Any suggestions would be great!  

Thanks!

 

0 Kudos
Message 1 of 7
(6,630 Views)

It seems that you're just using a string. Can you not run a For loop and plug the iteration value into the string using String.Format method?

 

String.Format Method

Mathew H.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 7
(6,591 Views)

That seems like it will work perfectly!  I've modified and posted my code below.  I can't seem to get the string to write to the test equipment (wrtbuf) though.  

    Private Sub btnSwitchTest_Click(sender As Object, e As EventArgs) Handles btnSwitchTest.Click
        Dim wrtbuf As String
Dim Port As Integer Dim SwitchState As String = String.Format("SOUR:DIG:DATA:BYTE {0}, (@201)", Port) DAQ = li.ibdev(0, 9, NO_SECONDARY_ADDR, TIMEOUT, EOTMODE, EOSMODE) ' GPIB addressing. For Port = 0 To 34 wrtbuf = SwitchState li.ilwrt(DAQ, wrtbuf, Len(wrtbuf)) System.Threading.Thread.Sleep(1000) Next Port

It just write a 0 over and over again.  Not sure what I'm doing wrong.  I tried to put console.writeline(switchstate) after wrtbuf = and I get an error that says "expression does not produce a value".   Suggestions? 

 

Also tried this...I added wrtbuf = to string.format and put console.writeline where wrtbuf was previously. 

    Private Sub btnSwitchTest_Click(sender As Object, e As EventArgs) Handles btnSwitchTest.Click
        Dim wrtbuf As String
        Dim Port As Integer
        Dim SwitchState As String = String.Format(wrtbuf = "SOUR:DIG:DATA:BYTE {0}, (@201)", Port)



        DAQ = li.ibdev(0, 9, NO_SECONDARY_ADDR, TIMEOUT, EOTMODE, EOSMODE) ' GPIB addressing.  

        For Port = 0 To 34

            Console.WriteLine(SwitchState)
            li.ilwrt(DAQ, wrtbuf, Len(wrtbuf))
            System.Threading.Thread.Sleep(1000)

        Next Port
0 Kudos
Message 3 of 7
(6,566 Views)
Solution
Accepted by tbayler

You have to have the String.Format function inside of the for loop because right now it just writes the string once and then in the for loop it doesn't change the string at all. This is more what you're looking to do.

 

For Port = 0 To 34
wrtbuf = String.Format("SOUR:DIG:DATA:BYTE {0}, (@201)", Port) li.ilwrt(DAQ, wrtbuf, Len(wrtbuf)) System.Threading.Thread.Sleep(1000) Next Port

 

Mathew H.
Applications Engineer
National Instruments
Message 4 of 7
(6,556 Views)

YES!  Works like a charm.  Can't believe I missed such an easy solution.  

 

Thanks a lot!

0 Kudos
Message 5 of 7
(6,554 Views)

No problem! I'm glad you've got it working for yourself now!

Mathew H.
Applications Engineer
National Instruments
0 Kudos
Message 6 of 7
(6,551 Views)

Hello sir,

Iam using NI visa and PYvisa to control keysight progrmmable power supply. I want to use variables and loops command but it is not accepting 

For example,

 

For x in range 10 

 myinstrument.write('VOLT x, (@2))

 

 

Is there any way to use variables in python

0 Kudos
Message 7 of 7
(3,948 Views)