• Formatting a command

    From Bob Liesenfeld@user21406@newsgrouper.org.invalid to comp.lang.tcl on Tue Jul 21 00:45:41 2026
    From Newsgroup: comp.lang.tcl


    Hello,
    I am getting back into Tcl after many years and am struggling how to make up a command. The script is used to communicate with a voltmeter using a visa library.
    This code works:

    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::22::INSTR"]

    The "22" in the inst variable is the meter's GPIB address. With the rest of the code,
    I can capture readings, save them to a file etc.

    What I am trying to figure out is how to get this line formatted to be able to use an entry box feeding a variable named gpib. I can get the variable populated
    via the entry box, but getting the variable contents to work in the set inst command
    is giving me trouble.

    This does -not- work:

    set gpib 22 (via the entry box)
    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    I'm thinking maybe using the format command is the key, but can't figure it out.
    any thoughts?

    Thanks!
    Bob

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From et99@et99@rocketship1.me to comp.lang.tcl on Mon Jul 20 19:02:09 2026
    From Newsgroup: comp.lang.tcl

    try it as "GPIB0::${gpib}::INSTR"

    otherwise tcl thinks you are substituting a variable $gpib::INSTR, the braces protect the variable name from what surrounds it.


    On 7/20/2026 5:45 PM, Bob Liesenfeld wrote:

    Hello,
    I am getting back into Tcl after many years and am struggling how to make up a
    command. The script is used to communicate with a voltmeter using a visa library.
    This code works:

    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::22::INSTR"]

    The "22" in the inst variable is the meter's GPIB address. With the rest of the code,
    I can capture readings, save them to a file etc.

    What I am trying to figure out is how to get this line formatted to be able to
    use an entry box feeding a variable named gpib. I can get the variable populated
    via the entry box, but getting the variable contents to work in the set inst command
    is giving me trouble.

    This does -not- work:

    set gpib 22 (via the entry box)
    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    I'm thinking maybe using the format command is the key, but can't figure it out.
    any thoughts?

    Thanks!
    Bob


    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Rich@rich@example.invalid to comp.lang.tcl on Tue Jul 21 03:58:51 2026
    From Newsgroup: comp.lang.tcl

    Bob Liesenfeld <user21406@newsgrouper.org.invalid> wrote:
    set gpib 22 (via the entry box)
    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    I'm thinking maybe using the format command is the key, but can't figure it out.
    any thoughts?

    et99 gave you the solution to your code above.

    If you prefer the format command instead it would be:

    set inst [visa::open $rm [format "GPIB0::%d::INSTR" $gpib]]

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Christian Gollwitzer@auriocus@gmx.de to comp.lang.tcl on Tue Jul 21 09:06:52 2026
    From Newsgroup: comp.lang.tcl

    Am 21.07.26 um 05:58 schrieb Rich:
    Bob Liesenfeld <user21406@newsgrouper.org.invalid> wrote:
    set gpib 22 (via the entry box)
    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    I'm thinking maybe using the format command is the key, but can't figure it out.
    any thoughts?

    et99 gave you the solution to your code above.

    If you prefer the format command instead it would be:

    set inst [visa::open $rm [format "GPIB0::%d::INSTR" $gpib]]

    This is better, because it normalizes the number. E.g. if you put spaces
    into the entry like " 22 ", it would not work with ${gpib}

    Christian


    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Tue Jul 21 17:43:42 2026
    From Newsgroup: comp.lang.tcl

    * Bob Liesenfeld <user21406@newsgrouper.org.invalid>
    | This does -not- work:

    | set gpib 22 (via the entry box)
    | set rm [visa::open-default-rm]
    | set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    In addition to what others already contributed, it is always a good idea
    to include any error message or diagnostic in the question - even a
    "nothing happens at all" (an error dialog popping up or a message on the console is NOT nothing) helps others to rule out possible causes.

    R'
    http://www.catb.org/~esr/faqs/smart-questions.html
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Bob Liesenfeld@user21406@newsgrouper.org.invalid to comp.lang.tcl on Wed Jul 22 08:32:53 2026
    From Newsgroup: comp.lang.tcl


    Ralf Fassel <ralfixx@gmx.de> posted:

    * Bob Liesenfeld <user21406@newsgrouper.org.invalid>
    | This does -not- work:

    | set gpib 22 (via the entry box)
    | set rm [visa::open-default-rm]
    | set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    In addition to what others already contributed, it is always a good idea
    to include any error message or diagnostic in the question - even a
    "nothing happens at all" (an error dialog popping up or a message on the console is NOT nothing) helps others to rule out possible causes.

    R'
    http://www.catb.org/~esr/faqs/smart-questions.html
    Thanks to all for the help! Script working fine now. Also, Ralf, >great< point
    about the result displayed when the script does not work. Troubleshooting code is an art, and I'm a bit rusty. :-)
    Bob
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Jul 23 12:14:28 2026
    From Newsgroup: comp.lang.tcl

    On 7/20/2026 8:45 PM, Bob Liesenfeld wrote:

    Hello,
    I am getting back into Tcl after many years and am struggling how to make up a
    command. The script is used to communicate with a voltmeter using a visa library.
    This code works:

    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::22::INSTR"]

    The "22" in the inst variable is the meter's GPIB address. With the rest of the code,
    I can capture readings, save them to a file etc.
    ...

    set gpib 22 (via the entry box)
    set rm [visa::open-default-rm]
    set inst [visa::open $rm "GPIB0::$gpib::INSTR"]

    I'm thinking maybe using the format command is the key, but can't figure it out.
    any thoughts?


    For the sake of full error checking, if the GPIB is alphanumeric,
    formatting it as %d may not work.

    Here is a solution that avoids many pitfalls:

    set inst [visa::open $rm "GPIB0::[string trim $gpib]::INSTR"]

    You can also add a check for when/if gpib is empty and print a message.


    --- Synchronet 3.22a-Linux NewsLink 1.2