Manuals >Reference >User C Functions
Print version of this Book (PDF file)
prevnext

Hints

Hints for Instruments

Instrument I/O is undertaken by opening a device file associated with the instrument. As described in the section USERC_open, instrument input and output is best accomplished by dedicating two separate file descriptors, one for reading, and one for writing. The USERC_open function can be called twice, as shown in the following example:

 rdfile = USERC_open("/dev/hpib_721","r")
 ! for reading input
 wrfile = USERC_open("/dev/hpib_721","w")
 ! for writing output
 status=USERC_write(wrfile,1,"*IDN?")
 ! ask for ID of 54120
 status=USERC_readstr(rdfile,1,"%s%*[\n]",readstr)
 ! read ID into readstr, and discard trailing newline
 status=USERC_close(rdfile)
 status=USERC_close(wrfile)

If instrument I/O is intended, you may want to review the documentation provided for the User C prober facility. This facility offers several GPIB routines that could be valuable, for example, serial polling.

Because of the need for instruments to sometimes communicate several data points at once (compared to retrieving data from a file, where you can obtain exactly as much data as desired, in as many reads as desired), it will probably be necessary for the read functions, USERC_readnum and USERC_readstr, to be extended and modified for use with some instruments. One possibility would be new User C functions dedicated to reading entire arrays out of particular instrument types.

Hints for Timeouts

By default, the routines for reading and writing do not employ a timeout. However, you can interrupt them using Ctrl-C. The UNIX library routine io_timeout_ctl can be used to associate a timeout with one of the file descriptors returned by USERC_open. This would require that one or more of the above routines be modified, after which recompiling and linking are necessary. For details, refer to Creating C Language Functions in IC-CAP" in the User's Guide.

Hints for Reading/Writing Same File

If you need to read from and write to the same disk file, you should employ USERC_open once, using one of the following update modes "r+", "w+", or "a+." Then, you should adhere to the following guidelines (which are also described on the UNIX man page fopen(3S)):

    • A write should not be immediately followed by a read without performing an intervening seek.
    • A read should not be immediately followed by a write unless the read encountered end-of-file, or you perform an intervening seek.

Hints for Carriage Returns, Line Feeds, etc.

The functions for reading and writing (USERC_readnum, USERC_readstr, and USERC_write) can handle a limited set of unprintable characters. For example, an instrument can be sent a string with a trailing carriage return and line feed, as shown next.

 status=USERC_write(wrfile,1,"*IDN?\r\n") 
 ! \r and \n => CR and LF

If you want to collect a string from an instrument, and the instrument is going to attach a trailing carriage return and line feed, but you want to retrieve the whole string except for the terminating carriage return and line feed, follow the example shown next.

 status=USERC_readstr(rdfile,1,"%[^\r\n]\r\n",readstr)
! note: the specifier %[^\r\n] collects
! all characters EXCEPT \r and \n

The set of characters that can be specified in this manner is the same as those accepted by the Send String command (Instrument Setup window). For details, refer to Macro File Syntax Rules.


prevnext