08-04-2016 08:11 AM
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!
Solved! Go to Solution.
08-05-2016 04:36 PM
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?
08-08-2016 09:06 AM - edited 08-08-2016 09:11 AM
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
08-08-2016 01:34 PM
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
08-08-2016 01:38 PM
YES! Works like a charm. Can't believe I missed such an easy solution.
Thanks a lot!
08-08-2016 01:44 PM
No problem! I'm glad you've got it working for yourself now!
03-19-2020 10:40 AM - edited 03-19-2020 10:42 AM
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