home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 September / CHIP_CD_1997_09_PL.iso / software / testsoft / labwind / demo.6 / main / instr / hp5313xa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-02  |  70.3 KB  |  1,565 lines

  1.  
  2. #include <visa.h>
  3. #include <ansi_c.h>
  4. #include <string.h>
  5. #include <formatio.h>
  6. #include "hp5313xA.h"
  7.  
  8. #define hp5313xa_REVISION     "Rev 1.0, 9/95, CVI 3.1" /*  Instrument driver revision */
  9. #define BUFFER_SIZE         512L         /* File I/O buffer size */
  10.     
  11. /*= HP 53131A/132A Univeral Counter ===================================================*/
  12. /* LabWindows/CVI 3.1 Instrument Driver                                      */
  13. /* Original Release: January 1994                                            */
  14. /*   By: DT LWILDP, National Instruments, Austin, Texas.                     */
  15. /*     PH. (800)433-3488   Fax (512)794-5678                                 */
  16. /* Originally written in C                                                   */
  17. /*                                                                           */
  18. /* Modification History:                                                     */
  19. /*                                                                           */
  20. /*      June 1995 -  Modified the LabWindows/CVI instrument driver to use    */
  21. /*                   VISA I/O calls.                                         */
  22. /*                                                                           */
  23. /*                   Modified by: CSC, National Instruments, Austin, Texas    */
  24. /*                                                                           */
  25. /*===========================================================================*/
  26.  
  27. /*****************************************************************************/
  28. /*= INSTRUMENT-DEPENDENT COMMAND ARRAYS =====================================*/
  29. /*****************************************************************************/
  30. static ViString model[3] = {"", "HEWLETT-PACKARD,53131A", "HEWLETT-PACKARD,53132A"};
  31. static ViInt16 atten[2] = {1,10}; 
  32. static ViString coupl[2] = {"ac","dc"};
  33. static ViString impeda[2] = {"50", "1E6"};
  34. static ViString switchs[2] = {"off", "on"};
  35. static ViInt16 hyster[3] = {0,50,100};
  36. static ViString slope[2] = {"neg","pos"};
  37. static ViString lmShow[2] = {"numb","grap"};
  38. static ViString frp[7] = {"","freq 1","freq 2","freq 3","freq:rat 1,2","freq:rat 1,3","per 1"};
  39. static ViString stopcom[4] = {"ext","ext","tim","imm"};
  40. static ViString twd[5] = {"rise:time","fall:time","pwid","nwid","dcyc"};
  41. static ViString common[2] = {"inp2","inp1"};
  42. static ViString mathShow[5] = {"","sdev","mean","max","min"};
  43. static ViString exeMode[4] = {":init",":init:cont on","init:cont off","abor"};
  44. static ViString configFunc[23] = {"dcyc 50","fall:time 10,90","rise:time 10,90",
  45.                                   "freq (@1)","freq (@2)","freq (@3)",
  46.                                   "freq:rat (@1),(@2)","freq:rat (@1),(@3)",
  47.                                   "max (@1)","min (@1)",
  48.                                   "max (@2)","min (@2)",
  49.                                   "nwid 50,(@1)","pwid 50,(@1)",
  50.                                   "per (@1)","per (@2)","per (@3)",
  51.                                   "phas (@1),(@2)","ptp (@1)","ptp (@2)",
  52.                                   "tint (@1),(@2)","tot:cont (@1)","tot:tim (@1)"};
  53. static ViString postProcs[3] = {":calc2:imm","calc2:imm:auto on",":calc2:imm:auto off"};                              
  54. static ViString measFunc[20] = {  "dcyc? 50","fall:time? 10,90","rise:time? 10,90",
  55.                                   "freq? (@1)","freq? (@2)","freq? (@3)",
  56.                                   "freq:rat? (@1),(@2)","freq:rat? (@1),(@3)",
  57.                                   "max? (@1)","min? (@1)",
  58.                                   "max? (@2)","min? (@2)",
  59.                                   "nwid? 50,(@1)","pwid? 50,(@1)",
  60.                                   "per? (@1)","per? (@2)","per? (@3)",
  61.                                   "phas? (@1),(@2)","ptp? (@1)","ptp? (@2)"};
  62. static ViString method[3] = {"meas","read","fetch"};
  63.   
  64. /*****************************************************************************/
  65. /*= INSTRUMENT-DEPENDENT STATUS/RANGE STRUCTURE  ============================*/
  66. /*****************************************************************************/
  67. /* hp5313xa_stringValPair is used in the hp5313xa_errorMessage function          */
  68. /* hp5313xa_statusDataRanges is used to track session dependent status & ranges*/
  69. /*===========================================================================*/
  70. typedef struct  hp5313xa_stringValPair
  71. {
  72.    ViStatus stringVal;
  73.    ViString stringName;
  74. }  hp5313xa_tStringValPair;
  75.  
  76. struct hp5313xa_statusDataRanges {
  77.     ViInt16 triggerMode;
  78.     ViInt16 val2;
  79.     ViInt16 val3;
  80.     ViChar instrDriverRevision[256];
  81. };
  82.  
  83. typedef struct hp5313xa_statusDataRanges *hp5313xa_instrRange;
  84.  
  85. /*****************************************************************************/
  86. /*= UTILITY ROUTINE DECLARATIONS (Non-Exportable Functions) =================*/
  87. /*****************************************************************************/
  88. ViBoolean hp5313xa_invalidViBooleanRange (ViBoolean val);
  89. ViBoolean hp5313xa_invalidViInt16Range (ViInt16 val, ViInt16 min, ViInt16 max);
  90. ViBoolean hp5313xa_invalidViInt32Range (ViInt32 val, ViInt32 min, ViInt32 max);
  91. ViBoolean hp5313xa_invalidViUInt16Range (ViUInt16 val, ViUInt16 min, ViUInt16 max);
  92. ViBoolean hp5313xa_invalidViUInt32Range (ViUInt32 val, ViUInt32 min, ViUInt32 max);
  93. ViBoolean hp5313xa_invalidViReal32Range (ViReal32 val, ViReal32 min, ViReal32 max);
  94. ViBoolean hp5313xa_invalidViReal64Range (ViReal64 val, ViReal64 min, ViReal64 max);
  95. ViStatus hp5313xa_initCleanUp (ViSession openRMSession, ViPSession openInstrSession, ViStatus currentStatus);
  96. ViStatus hp5313xa_readToFile (ViSession instrSession, ViString filename, ViUInt32 readBytes, ViPUInt32 retCount);
  97. ViStatus hp5313xa_writeFromFile (ViSession instrSession, ViString filename, ViUInt32 writeBytes, ViPUInt32 retCount);
  98. ViStatus hp5313xa_get_error (ViSession instrSession);
  99. ViStatus hp5313xa_poll (ViSession instrSession, ViInt16 *);
  100. ViStatus hp5313xa_set_timeout (ViSession instrSession, ViAttrState timeOutCode);   
  101. ViStatus hp5313xa_defaultInstrSetup (ViSession openInstrSession);
  102.  
  103. /*****************************************************************************/
  104. /*====== USER-CALLABLE FUNCTIONS (Exportable Functions) =====================*/
  105. /*****************************************************************************/
  106.  
  107. /*===========================================================================*/
  108. /* Function: Initialize                                                      */
  109. /* Purpose:  This function opens the instrument, queries the instrument      */
  110. /*           for its ID, and initializes the instrument to a known state.    */
  111. /*===========================================================================*/
  112. ViStatus _VI_FUNC hp5313xa_init (ViRsrc resourceName, ViBoolean IDQuery,
  113.                         ViBoolean resetDevice, ViInt16 instrumentModel,
  114.                         ViSession *instrSession)
  115. {
  116.     ViStatus hp5313xa_status = VI_SUCCESS;
  117.     ViSession rmSession = 0;
  118.     ViUInt32 retCnt = 0;
  119.     ViByte rdBuffer[BUFFER_SIZE];
  120.  
  121.     /*- Check input parameter ranges ----------------------------------------*/
  122.  
  123.     if (hp5313xa_invalidViBooleanRange (IDQuery))
  124.         return VI_ERROR_PARAMETER2;
  125.     if (hp5313xa_invalidViBooleanRange (resetDevice))
  126.         return VI_ERROR_PARAMETER3;
  127.     if (hp5313xa_invalidViInt16Range (instrumentModel, 1, 2))
  128.         return VI_ERROR_PARAMETER4;
  129.  
  130.     /*- Open instrument session ---------------------------------------------*/
  131.  
  132.     if ((hp5313xa_status = viOpenDefaultRM (&rmSession)) < 0)
  133.         return hp5313xa_status;
  134.  
  135.     if ((hp5313xa_status = viOpen (rmSession, resourceName, VI_NULL, VI_NULL, instrSession)) < 0) {
  136.         viClose (rmSession);
  137.         return hp5313xa_status;
  138.     }
  139.  
  140.     /*- Configure VISA Formatted I/O ----------------------------------------*/
  141.  
  142.     if ((hp5313xa_status = viSetAttribute (*instrSession, VI_ATTR_TMO_VALUE, 10000)) < 0)
  143.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  144.     if ((hp5313xa_status = viSetBuf (*instrSession, VI_READ_BUF|VI_WRITE_BUF, 4000)) < 0)
  145.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  146.     if ((hp5313xa_status = viSetAttribute (*instrSession, VI_ATTR_WR_BUF_OPER_MODE,
  147.                             VI_FLUSH_ON_ACCESS)) < 0)
  148.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  149.     if ((hp5313xa_status = viSetAttribute (*instrSession, VI_ATTR_RD_BUF_OPER_MODE,
  150.                             VI_FLUSH_ON_ACCESS)) < 0)
  151.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  152.  
  153.     /*- Identification Query ------------------------------------------------*/
  154.  
  155.     if (IDQuery) {
  156.         if ((hp5313xa_status = viWrite (*instrSession, "*IDN?", 5, &retCnt)) < 0)
  157.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  158.         if ((hp5313xa_status = viRead (*instrSession, rdBuffer, BUFFER_SIZE, &retCnt)) < 0)
  159.             return hp5313xa_status;
  160.                     
  161.         Scan (rdBuffer, model[instrumentModel]);
  162.         if (NumFmtdBytes () != 22)
  163.             return hp5313xa_initCleanUp (rmSession, instrSession, VI_ERROR_FAIL_ID_QUERY);
  164.     }
  165.  
  166.     /*- Reset instrument ----------------------------------------------------*/
  167.  
  168.     if (resetDevice) {
  169.         if ((hp5313xa_status = hp5313xa_reset (*instrSession)) < 0)
  170.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  171.     }       
  172.     else  /*- Send Default Instrument Setup ---------------------------------*/
  173.         if ((hp5313xa_status = hp5313xa_defaultInstrSetup (*instrSession)) < 0)
  174.             return hp5313xa_initCleanUp (rmSession, instrSession, hp5313xa_status);
  175.         
  176.           
  177.     return hp5313xa_status;
  178. }
  179.  
  180. /*=========================================================================*/
  181. /* Function: Input Conditions                                              */
  182. /* Purpose:  Controls the characteristics of the Counter's input ports     */
  183. /*           (channels 1 and 2).                                           */
  184. /*=========================================================================*/
  185. ViStatus _VI_FUNC hp5313xa_inputCond (ViSession instrSession, ViInt16 channel,
  186.                              ViBoolean coupling, ViBoolean impedanceOhms,
  187.                              ViBoolean attenuation, ViBoolean filter)
  188. {
  189.     ViStatus hp5313xa_status = VI_SUCCESS;
  190.     ViChar wrtBuf[256];
  191.     ViUInt32 retCnt = 0;
  192.     
  193.     if (hp5313xa_invalidViInt16Range (channel, 1, 2))
  194.         return VI_ERROR_PARAMETER2;
  195.     if (hp5313xa_invalidViBooleanRange (coupling))
  196.         return VI_ERROR_PARAMETER3;
  197.     if (hp5313xa_invalidViBooleanRange (impedanceOhms))
  198.         return VI_ERROR_PARAMETER4;
  199.     if (hp5313xa_invalidViBooleanRange (attenuation))
  200.         return VI_ERROR_PARAMETER5;
  201.     if (hp5313xa_invalidViBooleanRange (filter))
  202.         return VI_ERROR_PARAMETER6;
  203.  
  204.     Fmt (wrtBuf, "%s<:inp%d:att %d;coup %s;imp %s;filt %s", channel, atten[attenuation],
  205.                               coupl[coupling], impeda[impedanceOhms], switchs[filter]);
  206.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  207.         return hp5313xa_status;
  208.  
  209.     if (hp5313xa_get_error (instrSession) != 0)
  210.         return hp5313xa_status;
  211.  
  212.     return hp5313xa_status;
  213. }
  214. /*=========================================================================*/
  215. /* Function: Trigger & Sensitivity                                         */
  216. /* Purpose:  Sets the Trigger Level/Sensitivity for the selected channel.  */
  217. /*=========================================================================*/
  218. ViStatus _VI_FUNC hp5313xa_trigSens (ViSession instrSession, ViInt16 channel,
  219.                             ViBoolean triggerLevel, ViReal64 voltageLevel,
  220.                             ViBoolean attenuation, ViBoolean triggerSlope,
  221.                             ViInt16 sensitivity)
  222. {
  223.     ViStatus hp5313xa_status = VI_SUCCESS;
  224.     ViChar wrtBuf[256];
  225.     ViInt16 total = 0;
  226.     ViUInt32 retCnt = 0;
  227.  
  228.     if (hp5313xa_invalidViInt16Range (channel, 1, 2))
  229.         return VI_ERROR_PARAMETER2;
  230.     if (hp5313xa_invalidViBooleanRange (triggerLevel))
  231.         return VI_ERROR_PARAMETER3;
  232.     if (hp5313xa_invalidViBooleanRange (attenuation))
  233.         return VI_ERROR_PARAMETER5;
  234.     if (hp5313xa_invalidViBooleanRange (triggerSlope))
  235.         return VI_ERROR_PARAMETER6;
  236.     if (hp5313xa_invalidViInt16Range (sensitivity, 0, 2))
  237.         return VI_ERROR_PARAMETER7;
  238.  
  239.     if (triggerLevel) {
  240.         if (attenuation == 0) {
  241.             if (hp5313xa_invalidViReal64Range (voltageLevel, -5.125, 5.125))
  242.                 return VI_ERROR_PARAMETER4;
  243.         }
  244.         else
  245.             if (hp5313xa_invalidViReal64Range (voltageLevel, -51.25, 51.25))
  246.                 return VI_ERROR_PARAMETER4;
  247.  
  248.         Fmt (wrtBuf, "%s<:inp%d:att %d;:even%d:lev %f;", channel, atten[attenuation], channel
  249.                     , voltageLevel);
  250.     }
  251.     else {
  252.         if (hp5313xa_invalidViInt16Range ((ViInt16) voltageLevel, 0, 100))
  253.             return VI_ERROR_PARAMETER4;
  254.  
  255.         Fmt (wrtBuf, "%s<:inp%d:att %d;:even%d:lev:auto on;rel %d;", channel,
  256.                     atten[attenuation], channel, (ViInt16) voltageLevel);
  257.     }
  258.     total += NumFmtdBytes ();
  259.     Fmt (wrtBuf, "%s[a]<:even%d:slop %s;hyst:rel %d;", channel, slope[triggerSlope],
  260.                 hyster[sensitivity]);
  261.     total += NumFmtdBytes ();
  262.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, total, &retCnt)) < 0)
  263.         return hp5313xa_status;
  264.     if (hp5313xa_get_error (instrSession) != 0)
  265.         return hp5313xa_status;
  266.  
  267.     return hp5313xa_status;
  268. }
  269.  
  270.  
  271. /*=========================================================================*/
  272. /* Function: Configure Limits                                              */
  273. /* Purpose:  Controls the limit test. It defines the lower and upper limit */
  274. /*           test.                                                         */
  275. /*=========================================================================*/
  276. ViStatus _VI_FUNC hp5313xa_limit (ViSession instrSession, ViBoolean limitTest,
  277.                          ViBoolean onFail, ViBoolean show, ViReal64 lowerLimit,
  278.                          ViReal64 upperLimit)
  279. {
  280.     ViStatus hp5313xa_status = VI_SUCCESS;
  281.     ViChar wrtBuf[256];
  282.     ViUInt32 retCnt = 0;
  283.     
  284.     if (hp5313xa_invalidViBooleanRange (limitTest))
  285.         return VI_ERROR_PARAMETER2;
  286.     if (hp5313xa_invalidViBooleanRange (onFail))
  287.         return VI_ERROR_PARAMETER3;
  288.     if (hp5313xa_invalidViBooleanRange (show))
  289.         return VI_ERROR_PARAMETER4;
  290.     if (upperLimit <= lowerLimit) {
  291.         hp5313xa_status = VI_ERROR_UPLOW_LIMIT;
  292.         return hp5313xa_status;
  293.     }
  294.     if (lowerLimit != 0.0)
  295.         if (hp5313xa_invalidViReal64Range (lowerLimit, -9.999999e+12, -1.0e-13))
  296.             if (hp5313xa_invalidViReal64Range (lowerLimit, 1.0e-13, 9.999999e+12))
  297.                 return VI_ERROR_PARAMETER5;
  298.     if (upperLimit != 0.0)
  299.         if (hp5313xa_invalidViReal64Range (upperLimit, -9.999999e+12, -1.0e-13))
  300.             if (hp5313xa_invalidViReal64Range (upperLimit, 1.0e-13, 9.999999e+12))
  301.                 return VI_ERROR_PARAMETER6;
  302.  
  303.     Fmt (wrtBuf, "%s<:calc2:lim:stat %s;disp %s;upp %f;low %f;:init:auto %s;",
  304.                  switchs[limitTest], lmShow[show], upperLimit, lowerLimit, switchs[onFail]);
  305.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  306.         return hp5313xa_status;
  307.     if (hp5313xa_get_error (instrSession) != 0)
  308.         return hp5313xa_status;
  309.  
  310.     return hp5313xa_status;
  311. }
  312.  
  313. /*=========================================================================*/
  314. /* Function: Configure Math                                                */
  315. /* Purpose:  Controls the math (scale/offset) processing. It sets the scale*/
  316. /*           and offset values. The counter has only one fixed math        */
  317. /*           operation and is as follows: Math = "Meas" * Scale + Offset   */
  318. /*=========================================================================*/
  319. ViStatus _VI_FUNC hp5313xa_math (ViSession instrSession, ViBoolean math,
  320.                         ViReal64 scaleValue, ViReal64 offsetValue)
  321. {
  322.     ViStatus hp5313xa_status = VI_SUCCESS;
  323.     ViChar wrtBuf[256];
  324.     ViUInt32 retCnt = 0;          
  325.  
  326.     if (hp5313xa_invalidViBooleanRange (math))
  327.         return VI_ERROR_PARAMETER2;
  328.     if (scaleValue != 0.0)
  329.         if (hp5313xa_invalidViReal64Range (scaleValue, -9.999999e+12, -1.0e-13))
  330.             if (hp5313xa_invalidViReal64Range (scaleValue, 1.0e-13, 9.999999e+12))
  331.                 return VI_ERROR_PARAMETER3;
  332.     if (offsetValue != 0.0)
  333.         if (hp5313xa_invalidViReal64Range (offsetValue, -9.999999e+12, -1.0e-13))
  334.             if (hp5313xa_invalidViReal64Range (offsetValue, 1.0e-13, 9.999999e+12))
  335.                 return VI_ERROR_PARAMETER4;
  336.  
  337.     Fmt (wrtBuf, "%s<:trac scale, %f;:trac offset, %f;:calc:math:stat %s;",
  338.                     scaleValue, offsetValue, switchs[math]);
  339.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  340.         return hp5313xa_status;
  341.     if (hp5313xa_get_error (instrSession) != 0)
  342.         return hp5313xa_status;
  343.  
  344.     return hp5313xa_status;
  345. }
  346.  
  347. /*=========================================================================*/
  348. /* Function: Save/Recall Settings                                          */
  349. /* Purpose:  Saves/Recalls instrument settings to/from the specified       */
  350. /*           instrument memory location.                                   */
  351. /*=========================================================================*/
  352. ViStatus _VI_FUNC hp5313xa_savRecSet (ViSession instrSession, ViBoolean saveRecall,
  353.                              ViInt16 memoryLocation)
  354. {
  355.     ViStatus hp5313xa_status = VI_SUCCESS;
  356.     ViChar wrtBuf[256];
  357.     ViUInt32 retCnt = 0;
  358.     
  359.     if (hp5313xa_invalidViBooleanRange (saveRecall))
  360.         return VI_ERROR_PARAMETER2;
  361.  
  362.     if (saveRecall) {
  363.         if (hp5313xa_invalidViInt16Range (memoryLocation, 1, 20))
  364.             return VI_ERROR_PARAMETER3;
  365.  
  366.         Fmt (wrtBuf, "%s<*sav %d", memoryLocation);
  367.     }
  368.     else {
  369.         if (hp5313xa_invalidViInt16Range (memoryLocation, 0, 20))
  370.             return VI_ERROR_PARAMETER3;
  371.  
  372.         Fmt (wrtBuf, "%s<*rcl %d", memoryLocation);
  373.     }
  374.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  375.         return hp5313xa_status;
  376.     if (hp5313xa_get_error (instrSession) != 0)
  377.         return hp5313xa_status;
  378.  
  379.     return hp5313xa_status;
  380. }
  381.  
  382. /*=========================================================================*/
  383. /* Function: Freq, Period, & Ratio                                         */
  384. /* Purpuse:  Configures the selected function for measurement.It sets the  */
  385. /*           start/stop arming sources.                                    */
  386. /*=========================================================================*/
  387. ViStatus _VI_FUNC hp5313xa_freqPeriodRatio (ViSession instrSession,
  388.                                    ViInt16 selectFunction, ViInt16 selectArming,
  389.                                    ViReal64 timeDigits, ViBoolean startSlope,
  390.                                    ViInt16 stop)
  391. {
  392.     ViStatus hp5313xa_status = VI_SUCCESS;
  393.     ViChar wrtBuf[256];
  394.     ViUInt32 retCnt = 0;
  395.     ViInt16 total = 0;
  396.  
  397.     if (hp5313xa_invalidViInt16Range (selectFunction, 1, 6))
  398.         return VI_ERROR_PARAMETER2;
  399.     if (hp5313xa_invalidViInt16Range (selectArming, 0, 3))
  400.         return VI_ERROR_PARAMETER3;
  401.  
  402.     Fmt (wrtBuf, "%s<:func \"%s\";", frp [selectFunction]);
  403.     total += NumFmtdBytes();
  404.     if (selectArming == 0) {
  405.         Fmt (wrtBuf, "%s[a]<:freq:arm:sour imm;:freq:arm:stop:sour imm");
  406.         total += NumFmtdBytes();
  407.     }
  408.     else if (selectArming == 1) {
  409.         if (hp5313xa_invalidViInt16Range ((ViInt16)timeDigits, 3, 15))
  410.             return VI_ERROR_PARAMETER4;
  411.         Fmt (wrtBuf, "%s[a]<:freq:arm:sour imm;:freq:arm:stop:sour dig;dig %d",
  412.                         (ViInt16)timeDigits);
  413.         total += NumFmtdBytes();
  414.     }
  415.     else if (selectArming == 2) {
  416.         if (hp5313xa_invalidViReal64Range (timeDigits, 100e-5, 999e-5))
  417.             if (hp5313xa_invalidViReal64Range (timeDigits, 10e-3, 1000.00))
  418.                 return VI_ERROR_PARAMETER4;
  419.         Fmt (wrtBuf, "%s[a]<:freq:arm:sour imm;:freq:arm:stop:sour tim;tim %f",
  420.                         timeDigits);
  421.         total += NumFmtdBytes();
  422.     }
  423.     if (selectArming == 3) {
  424.         if (hp5313xa_invalidViBooleanRange (startSlope))
  425.             return VI_ERROR_PARAMETER5;
  426.         if (hp5313xa_invalidViInt16Range (stop, 0, 3))
  427.             return VI_ERROR_PARAMETER6;
  428.         Fmt (wrtBuf, "%s[a]<:freq:arm:sour ext;slop %s;stop:sour %s;",
  429.                         slope[startSlope], stopcom[stop]);
  430.         total += NumFmtdBytes();
  431.         if (stop == 2) {
  432.             if (hp5313xa_invalidViReal64Range (timeDigits, 100e-5, 999e-5))
  433.                 if (hp5313xa_invalidViReal64Range (timeDigits, 10e-3, 1000.00))
  434.                     return VI_ERROR_PARAMETER4;
  435.             Fmt (wrtBuf, "%s[a]<tim %f;", timeDigits);
  436.             total += NumFmtdBytes();
  437.         }
  438.         else if (stop != 3) {
  439.             Fmt (wrtBuf, "%s[a]<slop %s", slope[stop]);
  440.             total += NumFmtdBytes();
  441.         }
  442.     }
  443.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, total, &retCnt)) < 0)
  444.         return hp5313xa_status;
  445.     if (hp5313xa_get_error (instrSession) != 0)
  446.         return hp5313xa_status;
  447.  
  448.     return hp5313xa_status;
  449. }
  450.  
  451. /*=========================================================================*/
  452. /* Function: Phase (Ch1 To Ch2)                                            */
  453. /* Purpose:  Configures the Phase function for measurement.It sets the     */
  454. /*           start arming source.                                          */
  455. /*=========================================================================*/
  456. ViStatus _VI_FUNC hp5313xa_phase (ViSession instrSession, ViBoolean selectArming,
  457.                          ViBoolean startSlope)
  458. {
  459.     ViStatus hp5313xa_status = VI_SUCCESS;
  460.     ViChar wrtBuf[256];
  461.     ViUInt32 retCnt = 0;
  462.  
  463.     if (hp5313xa_invalidViBooleanRange (selectArming))
  464.         return VI_ERROR_PARAMETER2;
  465.  
  466.     if (selectArming == 0)
  467.         Fmt (wrtBuf, "%s<:func \"phas\";:phas:arm:sour imm");
  468.     else {
  469.         if (hp5313xa_invalidViBooleanRange (startSlope))
  470.             return VI_ERROR_PARAMETER3;
  471.  
  472.         Fmt (wrtBuf, "%s<:func \"phas\";:phas:arm:sour ext;slop %s", slope[startSlope]);
  473.     }
  474.  
  475.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  476.         return hp5313xa_status;
  477.     if (hp5313xa_get_error (instrSession) != 0)
  478.         return hp5313xa_status;
  479.  
  480.     return hp5313xa_status;
  481. }
  482.  
  483. /*=========================================================================*/
  484. /* Function: Totalize (Ch1)                                                */
  485. /* Purpose:  Configures the Totalize function for measurement.It sets the  */
  486. /*           start/stop arming sources.                                    */
  487. /*=========================================================================*/
  488. ViStatus _VI_FUNC hp5313xa_totalize (ViSession instrSession, ViInt16 selectArming,
  489.                             ViReal64 timeValue, ViBoolean startSlope, ViInt16 stop)
  490. {
  491.     ViStatus hp5313xa_status = VI_SUCCESS;
  492.     ViChar wrtBuf[256];
  493.     ViUInt32 retCnt = 0;
  494.     ViInt16 total = 0;
  495.  
  496.  
  497.     if (hp5313xa_invalidViInt16Range (selectArming, 0, 2))
  498.         return VI_ERROR_PARAMETER2;
  499.  
  500.     Fmt (wrtBuf, "%s<:func \"tot\";");
  501.     total += NumFmtdBytes();
  502.     if (selectArming == 0) {
  503.         Fmt (wrtBuf, "%s[a]<:tot:arm:sour imm;:tot:arm:stop:sour imm");
  504.         total += NumFmtdBytes();
  505.     }
  506.     else if (selectArming == 1) {
  507.         if (hp5313xa_invalidViReal64Range (timeValue, 100e-5, 999e-5))
  508.             if (hp5313xa_invalidViReal64Range (timeValue, 10e-3, 1000.00))
  509.                 return VI_ERROR_PARAMETER3;
  510.         Fmt (wrtBuf, "%s[a]<:tot:arm:sour imm;:tot:arm:stop:sour tim;tim %f",
  511.                                                                       timeValue);
  512.         total += NumFmtdBytes();
  513.     }
  514.     if (selectArming == 2) {
  515.         if (hp5313xa_invalidViBooleanRange (startSlope))
  516.             return VI_ERROR_PARAMETER4;
  517.         if (hp5313xa_invalidViInt16Range (stop, 0, 2))
  518.             return VI_ERROR_PARAMETER5;
  519.         Fmt (wrtBuf, "%s[a]<:tot:arm:sour ext;slop %s;stop:sour %s;",
  520.                         slope[startSlope], stopcom[stop]);
  521.         total += NumFmtdBytes();
  522.         if (stop == 2) {
  523.             if (hp5313xa_invalidViReal64Range (timeValue, 100e-5, 999e-5))
  524.                 if (hp5313xa_invalidViReal64Range (timeValue, 10e-3, 1000.00))
  525.                     return VI_ERROR_PARAMETER3;
  526.             Fmt (wrtBuf, "%s[a]<tim %f;", timeValue);
  527.             total += NumFmtdBytes();
  528.         }
  529.         else {
  530.             Fmt (wrtBuf, "%s[a]<slop %s", slope [stop]);
  531.             total += NumFmtdBytes();
  532.         }
  533.     }
  534.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, total, &retCnt)) < 0)
  535.         return hp5313xa_status;
  536.     if (hp5313xa_get_error (instrSession) != 0)
  537.         return hp5313xa_status;
  538.  
  539.     return hp5313xa_status;
  540. }
  541.  
  542. /*=========================================================================*/
  543. /* Function:  Time, Width, Dutycycle                                       */
  544. /* Purpose:   Configures the selected function for measurement.It sets the */
  545. /*            start arming source.                                        */
  546. /*=========================================================================*/
  547.  
  548. ViStatus _VI_FUNC hp5313xa_timWidDuty (ViSession instrSession, ViInt16 selectfunction,
  549.                               ViBoolean selectArming, ViBoolean startSlope)
  550. {
  551.     ViStatus hp5313xa_status = VI_SUCCESS;
  552.     ViChar wrtBuf[256];
  553.     ViUInt32 retCnt = 0;
  554.     ViInt16 total = 0;
  555.     
  556.     if (hp5313xa_invalidViInt16Range (selectfunction, 0, 4))
  557.         return VI_ERROR_PARAMETER2;
  558.     if (hp5313xa_invalidViBooleanRange (selectArming))
  559.         return VI_ERROR_PARAMETER3;
  560.  
  561.     if (selectArming == 0)
  562.         Fmt (wrtBuf, "%s<:func \"%s\";:tint:arm:sour imm", twd [selectfunction]);
  563.     else {
  564.         if (hp5313xa_invalidViBooleanRange (startSlope))
  565.             return VI_ERROR_PARAMETER4;
  566.         Fmt (wrtBuf, "%s<:func \"%s\";:tint:arm:sour ext;slop %s", twd [selectfunction],
  567.                         slope[startSlope]);
  568.     }
  569.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  570.         return hp5313xa_status;
  571.     if (hp5313xa_get_error (instrSession) != 0)
  572.         return hp5313xa_status;
  573.  
  574.     return hp5313xa_status;
  575. }
  576.  
  577. /*=========================================================================*/
  578. /* Function: Time Interval Ch1 To Ch2                                      */
  579. /* Purpose:  Configures the Time Interval function for measurement. It sets*/
  580. /*           the start/stop arming sources.                                */
  581. /*=========================================================================*/
  582. ViStatus _VI_FUNC hp5313xa_timeInterval (ViSession instrSession, ViBoolean startArming,
  583.                                 ViBoolean startSlope, ViBoolean stop,
  584.                                 ViReal64 timeValue, ViBoolean commonMode)
  585.  
  586. {
  587.     ViStatus hp5313xa_status = VI_SUCCESS;
  588.     ViChar wrtBuf[256];
  589.     ViUInt32 retCnt = 0;
  590.     ViInt16 total = 0;
  591.  
  592.     if (hp5313xa_invalidViBooleanRange (startArming))
  593.         return VI_ERROR_PARAMETER2;
  594.     if (hp5313xa_invalidViBooleanRange (stop))
  595.         return VI_ERROR_PARAMETER4;
  596.     if (hp5313xa_invalidViBooleanRange (commonMode))
  597.         return VI_ERROR_PARAMETER4;
  598.  
  599.     Fmt (wrtBuf, "%s<:func \"tint 1,2\";:even2:feed \"%s\";", common[commonMode]);
  600.     total += NumFmtdBytes();
  601.     if (startArming == 0)
  602.         Fmt (wrtBuf, "%s[a]<:tint:arm:sour imm;");
  603.     else {
  604.         if (hp5313xa_invalidViBooleanRange (startSlope))
  605.             return VI_ERROR_PARAMETER3;
  606.         Fmt (wrtBuf, "%s[a]<:tint:arm:sour ext;slop %s;", slope[startSlope]);
  607.     }
  608.     total += NumFmtdBytes();
  609.  
  610.     if (stop == 0)
  611.         Fmt (wrtBuf, "%s[a]<stop:sour imm;");
  612.     else {
  613.         if (hp5313xa_invalidViReal64Range (timeValue, 100e-5, 999e-5))
  614.             if (hp5313xa_invalidViReal64Range (timeValue, 10e-3, 1000.00))
  615.                 return VI_ERROR_PARAMETER5;
  616.         Fmt (wrtBuf, "%s[a]<stop:sour tim;tim %f", timeValue);
  617.     }
  618.     total += NumFmtdBytes();
  619.  
  620.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, total, &retCnt)) < 0)
  621.         return hp5313xa_status;
  622.     if (hp5313xa_get_error (instrSession) != 0)
  623.         return hp5313xa_status;
  624.  
  625.     return hp5313xa_status;
  626. }
  627.  
  628. /*=========================================================================*/
  629. /* Function: Configure For Block Measurement                               */
  630. /* Purpose:  Configures the counter for a block measurrement.It specifies  */
  631. /*           the number of measurements to combine for statistics          */
  632. /*           processing. It specifies which measurements will be used in   */
  633. /*           computing statistics; out-of-limit measurements can be        */
  634. /*           filtered out of the statistics processing.                    */
  635. /*=========================================================================*/
  636. ViStatus _VI_FUNC hp5313xa_confblock (ViSession instrSession, ViBoolean statistics,
  637.                              ViInt32 ofMeasurements, ViBoolean filterData,
  638.                              ViInt16 show, ViReal64 lowerLimit, ViReal64 upperLimit)
  639. {
  640.     ViStatus hp5313xa_status = VI_SUCCESS;
  641.     ViChar wrtBuf[256];
  642.     ViUInt32 retCnt = 0;
  643.     ViInt16 total = 0;
  644.  
  645.     if (hp5313xa_invalidViBooleanRange (statistics))
  646.         return VI_ERROR_PARAMETER2;
  647.     if (hp5313xa_invalidViInt32Range (ofMeasurements, 2, 1000000))
  648.         return VI_ERROR_PARAMETER3;
  649.     if (hp5313xa_invalidViBooleanRange (filterData))
  650.         return VI_ERROR_PARAMETER4;
  651.     if (lowerLimit != 0.0)
  652.         if (hp5313xa_invalidViReal64Range (lowerLimit, -9.999999e+12, -1.0e-13))
  653.             if (hp5313xa_invalidViReal64Range (lowerLimit, 1.0e-13, 9.999999e+12))
  654.                 return VI_ERROR_PARAMETER6;
  655.     if (upperLimit != 0.0)
  656.         if (hp5313xa_invalidViReal64Range (upperLimit, -9.999999e+12, -1.0e-13))
  657.             if (hp5313xa_invalidViReal64Range (upperLimit, 1.0e-13, 9.999999e+12))
  658.                 return VI_ERROR_PARAMETER7;
  659.     if (hp5313xa_invalidViInt16Range (show, 0, 4))
  660.         return VI_ERROR_PARAMETER5;
  661.  
  662.  
  663.     Fmt (wrtBuf, "%s<:calc3:aver %s;aver:count %d[b4];:calc3:lfil:stat %s;low %f;upp %f;:trig:count:auto on;",
  664.                     switchs[statistics], ofMeasurements, switchs[filterData], lowerLimit, upperLimit);
  665.     total += NumFmtdBytes();
  666.     if (show == 0)
  667.         Fmt (wrtBuf, "%s[a]<:disp:text:feed \"calc2\"");
  668.     else
  669.         Fmt (wrtBuf, "%s[a]<:disp:text:feed \"calc3\";:calc3:aver:type %s",
  670.                                                          mathShow[show]);
  671.     total += NumFmtdBytes();
  672.  
  673.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, total, &retCnt)) < 0)
  674.         return hp5313xa_status;
  675.  
  676.     if (hp5313xa_get_error (instrSession) != 0)
  677.         return hp5313xa_status;
  678.  
  679.     return hp5313xa_status;
  680. }
  681.  
  682. /*=========================================================================*/
  683. /* Function: Start/Stop Measurement                                        */
  684. /* Purpose:  Controls the initiation and termination of a measurement.     */
  685. /*=========================================================================*/
  686. ViStatus _VI_FUNC hp5313xa_strStpMeas (ViSession instrSession, ViInt16 executionMode)
  687.  
  688. {
  689.     ViStatus hp5313xa_status = VI_SUCCESS;
  690.     ViChar wrtBuf[256];
  691.     ViUInt32 retCnt = 0;
  692.  
  693.     if (hp5313xa_invalidViInt16Range (executionMode, 0, 3))
  694.         return VI_ERROR_PARAMETER2;
  695.  
  696.     Fmt (wrtBuf, "%s<%s", exeMode[executionMode]);
  697.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  698.         return hp5313xa_status;
  699.  
  700.     if (hp5313xa_get_error (instrSession) != 0)
  701.         return hp5313xa_status;
  702.  
  703.     return hp5313xa_status;
  704. }
  705.  
  706. /*=========================================================================*/
  707. /* Function: Initiate Registers                                            */
  708. /* Purpose:  Clears the Standard Event Status Enable, Service Request      */
  709. /*           Enable, and presets the enable registers and transition       */
  710. /*           filters associated with the operation and questionable status */
  711. /*           reporting structures.                                         */
  712. /*=========================================================================*/
  713. ViStatus _VI_FUNC hp5313xa_initReg (ViSession instrSession)
  714. {
  715.     ViStatus hp5313xa_status = VI_SUCCESS;
  716.     ViChar wrtBuf[256];
  717.     ViUInt32 retCnt = 0;
  718.  
  719.     Fmt (wrtBuf, "%s<*cls;*sre 0;*ese 0;:stat:pres");
  720.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  721.         return hp5313xa_status;
  722.  
  723.     if (hp5313xa_get_error (instrSession) != 0)
  724.         return hp5313xa_status;
  725.  
  726.     return hp5313xa_status;
  727. }
  728.  
  729. /*=========================================================================*/
  730. /* Function: Set SRQ Enable Condition                                      */
  731. /* Purpose:  Sets the condition at which the service request (SRQ) bit will*/
  732. /*           be set.                                                       */
  733. /*=========================================================================*/
  734. ViStatus _VI_FUNC hp5313xa_setSrqCond (ViSession instrSession, ViInt16 SRQCondition)
  735. {
  736.     ViStatus hp5313xa_status = VI_SUCCESS;
  737.     ViChar wrtBuf[256];
  738.     ViUInt32 retCnt = 0;
  739.     
  740.     if (hp5313xa_invalidViInt16Range(SRQCondition, 0, 2))
  741.         return VI_ERROR_PARAMETER2;
  742.  
  743.     if (SRQCondition == 0)
  744.         Fmt (wrtBuf, "%s<*ese 1;*sre 32;*opc");
  745.     else if (SRQCondition == 1)
  746.         Fmt (wrtBuf, "%s<:stat:ques:enab 1024;*sre 8");
  747.     else
  748.         Fmt (wrtBuf, "%s<:stat:oper:enable 256;ntr 256;ptr 0;*sre 128");
  749.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  750.         return hp5313xa_status;
  751.  
  752.     if (hp5313xa_get_error (instrSession) != 0)
  753.         return hp5313xa_status;
  754.  
  755.     return hp5313xa_status;
  756. }
  757. /*=========================================================================*/
  758. /* Function: Wait For SRQ                                                  */
  759. /* Purpose:  Waits until the SRQ bit is set.                               */
  760. /*=========================================================================*/
  761. ViStatus _VI_FUNC hp5313xa_waitSrq (ViSession instrSession)
  762. {
  763.     ViStatus hp5313xa_status = VI_SUCCESS;
  764.     ViBoolean done;
  765.     ViInt16 spoll;
  766.  
  767.     done = 0;
  768.     while (!done) {
  769.         if ((hp5313xa_status = viReadSTB (instrSession, &spoll)) != 0)
  770.             return hp5313xa_status;
  771.         if (spoll & 0x40)
  772.             done = 1;
  773.     }
  774.     if (hp5313xa_get_error (instrSession) != 0)
  775.         return hp5313xa_status;
  776.  
  777.     return hp5313xa_status;
  778. }
  779.  
  780. /*=========================================================================*/
  781. /* Function: Configure Function                                            */
  782. /* Purpose:  Configures the counter for measurement of the selected        */
  783. /*           function. This function is used when "reading" or "fetching"  */
  784. /*           measurement methods are used.                                 */
  785. /*=========================================================================*/
  786. ViStatus _VI_FUNC hp5313xa_confFunc (ViSession instrSession, ViInt16 selectFunction)
  787. {
  788.     ViStatus hp5313xa_status = VI_SUCCESS;
  789.     ViChar wrtBuf[256];
  790.     ViUInt32 retCnt = 0;
  791.  
  792.     if (hp5313xa_invalidViInt16Range (selectFunction, 0, 22))
  793.         return VI_ERROR_PARAMETER2;
  794.     Fmt (wrtBuf, ":conf:%s", configFunc[selectFunction]);
  795.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  796.         return hp5313xa_status;
  797.  
  798.     if (hp5313xa_get_error (instrSession) != 0)
  799.         return hp5313xa_status;
  800.  
  801.     return hp5313xa_status;
  802. }
  803.  
  804. /*=========================================================================*/
  805. /* Function: Initiate Measurement                                          */
  806. /* Purpose:  Starts the measurement when the "fetching" measurement is     */
  807. /*           used. After calling this function, one can fetch for the      */
  808. /*           configured function or any related function.                  */
  809. /*=========================================================================*/
  810. ViStatus _VI_FUNC hp5313xa_initMeas (ViSession instrSession)
  811. {
  812.     ViStatus hp5313xa_status = VI_SUCCESS;
  813.     ViUInt32 retCnt = 0;
  814.  
  815.     if ((hp5313xa_status = viWrite (instrSession, ":init", 5, &retCnt)) < 0)
  816.         return hp5313xa_status;
  817.  
  818.     if (hp5313xa_get_error (instrSession) != 0)
  819.         return hp5313xa_status;
  820.  
  821.     return hp5313xa_status;
  822. }
  823.  
  824. /*=========================================================================*/
  825. /* Function: Invoke Post-Processing                                        */
  826. /* Purpose:  Controls the post processing of data. When enabled, the       */
  827. /*           counter recalculates existing data without re-acquiring data. */
  828. /*=========================================================================*/
  829. ViStatus _VI_FUNC hp5313xa_postProcess (ViSession instrSession, ViInt16 postProcess)
  830. {
  831.     ViStatus hp5313xa_status = VI_SUCCESS;
  832.     ViChar wrtBuf[256];
  833.     ViUInt32 retCnt = 0;
  834.     
  835.                                     
  836.     if (hp5313xa_invalidViInt16Range (postProcess, 0, 2))
  837.         return VI_ERROR_PARAMETER2;
  838.  
  839.     Fmt (wrtBuf, "%s<%s", postProcs[postProcess]);
  840.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  841.         return hp5313xa_status;
  842.  
  843.     if (hp5313xa_get_error (instrSession) != 0)
  844.         return hp5313xa_status;
  845.  
  846.     return hp5313xa_status;
  847. }
  848.  
  849. /*=========================================================================*/
  850. /* Function: Query Measurement                                             */
  851. /* Purpose:   Performs direct, read, or fetch measurement of the selected  */
  852. /*            function at the selected channel.                            */
  853. /*=========================================================================*/
  854. ViStatus _VI_FUNC hp5313xa_queryMeas (ViSession instrSession, ViInt16 selectFunction,
  855.                              ViInt16 selectMeasMethod, ViReal64 *measuredValue)
  856. {
  857.  
  858.     ViStatus hp5313xa_status = VI_SUCCESS;
  859.     ViChar wrtBuf[256];
  860.     ViChar rdBuf[256];
  861.     ViUInt16 response = 0;         
  862.     ViUInt32 retCnt = 0;
  863.     
  864.     if (hp5313xa_invalidViInt16Range (selectFunction, 0, 21))
  865.         return VI_ERROR_PARAMETER2;
  866.     if (hp5313xa_invalidViInt16Range (selectMeasMethod, 0, 2))
  867.         return VI_ERROR_PARAMETER3;
  868.     if (selectMeasMethod == 0)
  869.         Fmt (wrtBuf, "%s<:%s:%s", method[selectMeasMethod], measFunc[selectFunction]);
  870.      else
  871.         Fmt (wrtBuf, "%s<:%s:%s[t32]", method[selectMeasMethod], measFunc[selectFunction]);
  872.  
  873.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  874.         return hp5313xa_status;
  875.      do { 
  876.         if ((hp5313xa_status = viReadSTB (instrSession, &response)) != 0)
  877.             return hp5313xa_status;
  878.      } while (!(response & 0x10));
  879.  
  880.     if ((hp5313xa_status = viRead (instrSession, rdBuf, 50, &retCnt)) < 0)
  881.         return hp5313xa_status;
  882.     if (Scan (rdBuf, "%s>%f", measuredValue) != 1)  {
  883.         hp5313xa_status = VI_ERROR_INTERPRETING_RESPONSE;
  884.         return hp5313xa_status;
  885.     }
  886.  
  887.     if (hp5313xa_get_error (instrSession) != 0)
  888.         return hp5313xa_status;
  889.  
  890.     return hp5313xa_status;
  891. }
  892.  
  893. /*=========================================================================*/
  894. /* Function: Data Results                                                  */
  895. /* Purpose:  Returns the data displayed on the instrument's front panel.   */
  896. /*=========================================================================*/
  897. ViStatus _VI_FUNC hp5313xa_dataRes (ViSession instrSession, ViReal64 *measuredData)
  898. {
  899.     ViStatus hp5313xa_status = VI_SUCCESS;
  900.     ViChar rdBuf[256];
  901.     ViUInt32 retCnt = 0;
  902.  
  903.  
  904.     if ((hp5313xa_status = viWrite (instrSession, ":data?", 6, &retCnt)) < 0)
  905.         return hp5313xa_status;
  906.     if ((hp5313xa_status = viRead (instrSession, rdBuf, 50, &retCnt)) < 0)
  907.         return hp5313xa_status;
  908.  
  909.     if (Scan (rdBuf, "%s>%f", measuredData) != 1)  {
  910.         hp5313xa_status = VI_ERROR_INTERPRETING_RESPONSE;
  911.         return hp5313xa_status;
  912.     }
  913.  
  914.     if (hp5313xa_get_error (instrSession) != 0)
  915.         return hp5313xa_status;
  916.  
  917.     return hp5313xa_status;
  918. }
  919.  
  920. /*=========================================================================*/
  921. /* Function: Math Results                                                  */
  922. /* Purpose:  Returns the current scaled and offset measurement result. The */
  923. /*           counter has only one fixed math operation and is as follows:  */
  924. /*                  Math = "Meas" * Scale + Offset                         */
  925. /*=========================================================================*/
  926. ViStatus _VI_FUNC hp5313xa_mathRes (ViSession instrSession, ViReal64 *scaledOffsetData)
  927.  
  928. {
  929.  
  930.     ViStatus hp5313xa_status = VI_SUCCESS;
  931.     ViChar rdBuf[256];
  932.     ViUInt32 retCnt = 0;
  933.     
  934.     if ((hp5313xa_status = viWrite (instrSession, ":calc1:data?", 12, &retCnt)) < 0)
  935.         return hp5313xa_status;
  936.     if ((hp5313xa_status = viRead (instrSession, rdBuf, 50, &retCnt)) < 0)
  937.         return hp5313xa_status;
  938.  
  939.     if (Scan (rdBuf, "%s>%f", scaledOffsetData) != 1)  {
  940.         hp5313xa_status = VI_ERROR_INTERPRETING_RESPONSE;
  941.         return hp5313xa_status;
  942.     }
  943.  
  944.     if (hp5313xa_get_error (instrSession) != 0)
  945.         return hp5313xa_status;
  946.  
  947.     return hp5313xa_status;
  948. }
  949.  
  950. /*=========================================================================*/
  951. /* Function: Limit Results                                                 */
  952. /* Purpose:  Reports the results of the limit test on a multiple of        */
  953. /*           measurements.                                                 */
  954. /*=========================================================================*/
  955. ViStatus _VI_FUNC hp5313xa_limitRes (ViSession instrSession, ViInt16 *dataWithinLimit,
  956.                             ViInt16 *failCount, ViInt16 *passCount,
  957.                             ViInt16 *lowerFailures, ViInt16 *upperFailures)
  958. {
  959.     ViStatus hp5313xa_status = VI_SUCCESS;
  960.     ViChar wrtBuf[256] = "", rdBuf[256] = "";
  961.     ViUInt16 response = 0;
  962.     ViUInt32 retCnt = 0;
  963.     
  964.     Fmt (wrtBuf, "%s<:calc2:lim:fail?;:calc2:lim:fco?;fco:low?;upp?;:calc2:lim:pco?");
  965.  
  966.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  967.         return hp5313xa_status;
  968.      do { 
  969.         if ((hp5313xa_status = viReadSTB (instrSession, &response)) != 0)
  970.             return hp5313xa_status;
  971.      } while (!(response & 0x10));
  972.     
  973.     if ((hp5313xa_status = viRead (instrSession, rdBuf, 50, &retCnt)) < 0)
  974.         return hp5313xa_status;
  975.  
  976.  
  977.     if (Scan (rdBuf, "%s>%d[b2];%d[b2];%d[b2];%d[b2];%d[b2]", dataWithinLimit, failCount, lowerFailures,
  978.                                                 upperFailures, passCount) != 5)  {
  979.         hp5313xa_status = VI_ERROR_INTERPRETING_RESPONSE;
  980.         return hp5313xa_status;
  981.     }
  982.  
  983.     if (hp5313xa_get_error (instrSession) != 0)
  984.         return hp5313xa_status;
  985.  
  986.     return hp5313xa_status;
  987. }
  988.  
  989. /*=========================================================================*/
  990. /* Function: Statistics Results                                            */
  991. /* Purpose:  Reports the statistical results of a block of measurements.   */
  992. /*=========================================================================*/
  993. ViStatus _VI_FUNC hp5313xa_statisticsRes (ViSession instrSession, ViReal64 *mean,
  994.                                  ViReal64 *stdDeviation, ViReal64 *maximum,
  995.                                  ViReal64 *minimum)
  996. {
  997.     ViStatus hp5313xa_status = VI_SUCCESS;
  998.     ViChar wrtBuf[256];
  999.     ViChar rdBuf[256];
  1000.     ViUInt32 retCnt = 0;
  1001.  
  1002.     Fmt (wrtBuf, "%s<:calc3:aver:all?");
  1003.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  1004.         return hp5313xa_status;
  1005.     if ((hp5313xa_status = viRead (instrSession, rdBuf, 200, &retCnt)) < 0)
  1006.         return hp5313xa_status;
  1007.  
  1008.     if (Scan (rdBuf, "%s>%f,%f,%f,%f", mean, stdDeviation, minimum, maximum) != 4)  {
  1009.         hp5313xa_status = VI_ERROR_INTERPRETING_RESPONSE;
  1010.         return hp5313xa_status;
  1011.     }
  1012.  
  1013.     if (hp5313xa_get_error (instrSession) != 0)
  1014.         return hp5313xa_status;
  1015.  
  1016.     return hp5313xa_status;
  1017. }
  1018.  
  1019. /*=========================================================================*/
  1020. /* Function: Query Chan 3's Settings                                       */
  1021. /* Purpose:  Queries the input channel for its input conditions (coupling &*/
  1022. /*           Impedance).                                                   */
  1023. /*=========================================================================*/
  1024. ViStatus _VI_FUNC hp5313xa_queryChan3 (ViSession instrSession, ViBoolean *inputCoupling,
  1025.                               ViBoolean *inputImpedance)
  1026. {
  1027.     ViStatus hp5313xa_status = VI_SUCCESS;
  1028.     ViChar wrtBuf[256];
  1029.     ViChar rdBuf[256];
  1030.     ViUInt32 retCnt = 0;
  1031.     
  1032.     Fmt (wrtBuf, "%s<:inp3:coup?;:inp3:imp?");
  1033.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  1034.         return hp5313xa_status;
  1035.     if ((hp5313xa_status = viRead (instrSession, rdBuf, 200, &retCnt)) < 0)
  1036.         return hp5313xa_status;
  1037.  
  1038.     if (rdBuf[0] == 'a' || rdBuf[0] == 'A')
  1039.         *inputCoupling = 1;
  1040.     else
  1041.         *inputImpedance = 0;
  1042.     if (rdBuf[4] == '5')
  1043.         *inputImpedance = 1;
  1044.     else
  1045.         *inputImpedance = 0;
  1046.  
  1047.     if (hp5313xa_get_error (instrSession) != 0)
  1048.         return hp5313xa_status;
  1049.  
  1050.     return hp5313xa_status;
  1051. }
  1052.  
  1053. /*=========================================================================*/
  1054. /* Function: Enable/Disable Settings                                       */
  1055. /* Purpose:  Enables or disables some major counter settings such as limit */
  1056. /*           testing, math, filtering data, and statistics.                */
  1057. /*=========================================================================*/
  1058. ViStatus _VI_FUNC hp5313xa_enaDisSet (ViSession instrSession, ViBoolean limitTest,
  1059.                              ViBoolean math, ViBoolean filterData,
  1060.                              ViBoolean statistics)
  1061. {
  1062.     ViStatus hp5313xa_status = VI_SUCCESS;
  1063.     ViChar wrtBuf[256];
  1064.     ViUInt32 retCnt = 0;
  1065.     
  1066.     if (hp5313xa_invalidViBooleanRange (limitTest))
  1067.         return VI_ERROR_PARAMETER2;
  1068.     if (hp5313xa_invalidViBooleanRange (math))
  1069.         return VI_ERROR_PARAMETER3;
  1070.     if (hp5313xa_invalidViBooleanRange (filterData))
  1071.         return VI_ERROR_PARAMETER4;
  1072.     if (hp5313xa_invalidViBooleanRange (statistics))
  1073.         return VI_ERROR_PARAMETER5;
  1074.  
  1075.     Fmt (wrtBuf, "%s<:calc3:aver %s;:calc3:lfil:stat %s;:calc:math:stat %s;:calc2:lim:stat %s",
  1076.             switchs[statistics], switchs[filterData], switchs[math], switchs[limitTest]);
  1077.     if ((hp5313xa_status = viWrite (instrSession, wrtBuf, NumFmtdBytes(), &retCnt)) < 0)
  1078.         return hp5313xa_status;
  1079.  
  1080.     if (hp5313xa_get_error (instrSession) != 0)
  1081.         return hp5313xa_status;
  1082.  
  1083.     return hp5313xa_status;
  1084. }
  1085.  
  1086. /*===========================================================================*/
  1087.  
  1088. /*****************************************************************************/
  1089. /*-------- INSERT USER-CALLABLE INSTRUMENT-DEPENDENT ROUTINES HERE ----------*/
  1090. /*****************************************************************************/
  1091.            
  1092. /*===========================================================================*/
  1093. /* Function: Write To Instrument                                             */
  1094. /* Purpose:  This function writes a command string to the instrument.        */
  1095. /*===========================================================================*/
  1096. ViStatus _VI_FUNC hp5313xa_writeInstrData (ViSession instrSession, ViString writeBuffer)
  1097. {
  1098.     ViStatus hp5313xa_status = VI_SUCCESS;
  1099.     
  1100.     if ((hp5313xa_status = viPrintf (instrSession, "%s", writeBuffer)) < 0)
  1101.         return hp5313xa_status;
  1102.  
  1103.     return hp5313xa_status;
  1104. }
  1105.  
  1106. /*===========================================================================*/
  1107. /* Function: Read Instrument Buffer                                          */
  1108. /* Purpose:  This function reads the output buffer of the instrument.        */
  1109. /*===========================================================================*/
  1110. ViStatus _VI_FUNC hp5313xa_readInstrData (ViSession instrSession, ViInt16 numBytes,
  1111.                     ViChar _VI_FAR rdBuf[], ViPInt32 bytesRead)
  1112. {
  1113.     ViStatus hp5313xa_status = VI_SUCCESS;
  1114.     *bytesRead = 0L;
  1115.         
  1116.     if ((hp5313xa_status = viRead (instrSession, rdBuf, numBytes, bytesRead)) < 0)
  1117.         return hp5313xa_status;
  1118.  
  1119.     return hp5313xa_status;
  1120. }
  1121.  
  1122. /*===========================================================================*/
  1123. /* Function: Reset                                                           */
  1124. /* Purpose:  This function resets the instrument.  If the reset function     */
  1125. /*           is not supported by the instrument, this function returns       */
  1126. /*           the warning VI_WARN_NSUP_RESET.                                 */
  1127. /*===========================================================================*/
  1128. ViStatus _VI_FUNC hp5313xa_reset (ViSession instrSession)
  1129. {
  1130.     ViUInt32 retCnt = 0;
  1131.     ViStatus hp5313xa_status = VI_SUCCESS;
  1132.  
  1133.     /*  Initialize the instrument to a known state.  */
  1134.     if ((hp5313xa_status = viWrite (instrSession, "*RST", 4, &retCnt)) < 0)
  1135.         return hp5313xa_status;
  1136.  
  1137.     if ((hp5313xa_status = hp5313xa_defaultInstrSetup (instrSession)) < 0)  
  1138.         return hp5313xa_status;
  1139.         
  1140.        
  1141.     return hp5313xa_status;
  1142. }
  1143.  
  1144. /*===========================================================================*/
  1145. /* Function: Self-Test                                                       */
  1146. /* Purpose:  This function executes the instrument self-test and returns     */
  1147. /*           the result. If the self test function is not supported by the   */
  1148. /*           instrument, this function returns the warning                   */
  1149. /*           VI_WARN_NSUP_SELF_TEST.                                         */
  1150. /*===========================================================================*/
  1151. ViStatus _VI_FUNC hp5313xa_selfTest (ViSession instrSession, ViPInt16 testResult,
  1152.                     ViChar _VI_FAR testMessage[])
  1153. {
  1154.     ViUInt32 retCnt = 0;
  1155.     ViStatus hp5313xa_status = VI_SUCCESS;
  1156.  
  1157.     if ((hp5313xa_status = viWrite (instrSession, "*TST?", 5, &retCnt)) < 0)
  1158.         return hp5313xa_status;
  1159.  
  1160.     if ((hp5313xa_status = viScanf (instrSession, "%hd,\"%[^\"]", testResult, testMessage)) < 0)
  1161.         return hp5313xa_status;
  1162.     
  1163.     return hp5313xa_status;
  1164. }
  1165.  
  1166. /*===========================================================================*/
  1167. /* Function: Error Query                                                     */
  1168. /* Purpose:  This function queries the instrument error queue, and returns   */
  1169. /*           the result. If the error query function is not supported by the  */
  1170. /*           instrument, this function returns the warning                   */
  1171. /*           VI_WARN_NSUP_ERROR_QUERY.                                       */
  1172. /*===========================================================================*/
  1173. ViStatus _VI_FUNC hp5313xa_errorQuery (ViSession instrSession, ViPInt32 errCode,
  1174.                     ViChar _VI_FAR errMessage[])
  1175. {
  1176.     ViUInt32 retCnt = 0;
  1177.     ViStatus hp5313xa_status = VI_SUCCESS;
  1178.  
  1179.     if ((hp5313xa_status = viWrite (instrSession, ":SYST:ERR?", 10, &retCnt)) < 0)
  1180.         return hp5313xa_status;
  1181.  
  1182.     if ((hp5313xa_status = viScanf (instrSession, "%hd,\"%[^\"]", errCode, errMessage)) < 0)
  1183.         return hp5313xa_status;
  1184.  
  1185.    
  1186.     return hp5313xa_status;
  1187. }
  1188.  
  1189. /*===========================================================================*/
  1190. /* Function: Error Message                                                   */
  1191. /* Purpose:  This function translates the error return value from the        */
  1192. /*           instrument driver into a user-readable string.                  */
  1193. /*===========================================================================*/
  1194. ViStatus _VI_FUNC hp5313xa_errorMessage (ViSession instrSession, ViStatus errorCode,
  1195.                     ViChar _VI_FAR errMessage[])
  1196. {
  1197.     ViStatus hp5313xa_status = VI_SUCCESS;
  1198.     ViInt16 i;
  1199.     static hp5313xa_tStringValPair statusDescArray[] = {
  1200.         {VI_WARN_NSUP_ID_QUERY,     "WARNING: ID Query not supported"},
  1201.         {VI_WARN_NSUP_RESET,        "WARNING: Reset not supported"},
  1202.         {VI_WARN_NSUP_SELF_TEST,    "WARNING: Self-test not supported"},
  1203.         {VI_WARN_NSUP_ERROR_QUERY,  "WARNING: Error Query not supported"},     
  1204.         {VI_WARN_NSUP_REV_QUERY,    "WARNING: Revision Query not supported"},
  1205.         {VI_ERROR_PARAMETER1,   "ERROR: Parameter 1 out of range"},
  1206.         {VI_ERROR_PARAMETER2,   "ERROR: Parameter 2 out of range"},
  1207.         {VI_ERROR_PARAMETER3,   "ERROR: Parameter 3 out of range"},
  1208.         {VI_ERROR_PARAMETER4,   "ERROR: Parameter 4 out of range"},
  1209.         {VI_ERROR_PARAMETER5,   "ERROR: Parameter 5 out of range"},
  1210.         {VI_ERROR_PARAMETER6,   "ERROR: Parameter 6 out of range"},
  1211.         {VI_ERROR_PARAMETER7,   "ERROR: Parameter 7 out of range"},
  1212.         {VI_ERROR_PARAMETER8,   "ERROR: Parameter 8 out of range"},
  1213.         {VI_ERROR_FAIL_ID_QUERY,"ERROR: Identification query failed"},
  1214.         {VI_ERROR_INV_RESPONSE, "ERROR: Interpreting instrument response"},
  1215.         {VI_ERROR_FILE_OPEN,    "ERROR: Opening the specified file"},
  1216.         {VI_ERROR_FILE_WRITE,   "ERROR: Writing to the specified file"},
  1217.         {VI_ERROR_INTERPRETING_RESPONSE, "ERROR: Interpreting the instrument's response"},
  1218.         {VI_ERROR_UPLOW_LIMIT,  "ERROR: Upper Limit <= Lower Limit"},
  1219.  
  1220.         {VI_NULL, VI_NULL}
  1221.     };
  1222.  
  1223.     hp5313xa_status = viStatusDesc (instrSession, errorCode, errMessage);
  1224.     if (hp5313xa_status == VI_WARN_UNKNOWN_STATUS) {
  1225.         for (i=0; statusDescArray[i].stringName; i++) {
  1226.             if (statusDescArray[i].stringVal == errorCode) {
  1227.                 strcpy (errMessage, statusDescArray[i].stringName);
  1228.                 return (VI_SUCCESS);
  1229.             }
  1230.         }
  1231.         sprintf (errMessage, "Unknown Error 0x%08lX", errorCode);
  1232.         return (VI_WARN_UNKNOWN_STATUS);
  1233.     }
  1234.     
  1235.     hp5313xa_status = VI_SUCCESS;
  1236.     return hp5313xa_status;
  1237. }
  1238.  
  1239. /*===========================================================================*/
  1240. /* Function: Revision Query                                                  */
  1241. /* Purpose:  This function returns the driver and instrument revisions.      */
  1242. /*           If the revision query function is not supported by the          */ 
  1243. /*           instrument, this function returns the warning                   */
  1244. /*           VI_WARN_NSUP_REV_QUERY.                                         */
  1245. /*===========================================================================*/
  1246. ViStatus _VI_FUNC hp5313xa_revisionQuery (ViSession instrSession,
  1247.                     ViChar _VI_FAR driverRev[], ViChar _VI_FAR instrRev[])
  1248. {
  1249.     ViUInt32 retCnt = 0;
  1250.     ViStatus hp5313xa_status = VI_SUCCESS;
  1251.  
  1252.     if ((hp5313xa_status = viWrite (instrSession, "*IDN?", 5, &retCnt)) < 0)
  1253.         return hp5313xa_status;
  1254.  
  1255.     if ((hp5313xa_status = viScanf (instrSession, "%*[^,],%*[^,],%*[^,],%[^\n]", instrRev)) < 0)
  1256.         return hp5313xa_status;
  1257.  
  1258.     strcpy (driverRev, hp5313xa_REVISION);
  1259.     
  1260.     return hp5313xa_status;
  1261. }
  1262.  
  1263. /*===========================================================================*/
  1264. /* Function: Close                                                           */
  1265. /* Purpose:  This function closes the instrument.                            */
  1266. /*===========================================================================*/
  1267. ViStatus _VI_FUNC hp5313xa_close (ViSession instrSession)
  1268. {
  1269.     hp5313xa_instrRange instrPtr;
  1270.     ViSession rmSession;
  1271.     ViStatus hp5313xa_status = VI_SUCCESS;
  1272.  
  1273.     if ((hp5313xa_status = viGetAttribute (instrSession, VI_ATTR_RM_SESSION, &rmSession)) < 0)
  1274.         return hp5313xa_status;
  1275.     if ((hp5313xa_status = viGetAttribute (instrSession, VI_ATTR_USER_DATA, &instrPtr)) < 0)
  1276.         return hp5313xa_status;
  1277.     
  1278.     free (instrPtr);
  1279.     
  1280.     hp5313xa_status = viClose (instrSession);
  1281.     viClose (rmSession);
  1282.  
  1283.     return hp5313xa_status;
  1284. }
  1285.  
  1286. /*****************************************************************************/
  1287. /*= UTILITY ROUTINES (Non-Exportable Functions) =============================*/
  1288. /*****************************************************************************/
  1289.  
  1290. /*===========================================================================*/
  1291. /* Function: Boolean Value Out Of Range - ViBoolean                          */
  1292. /* Purpose:  This function checks a Boolean to see if it is equal to VI_TRUE */
  1293. /*           or VI_FALSE. If the value is out of range, the return value is  */
  1294. /*           VI_TRUE, otherwise the return value is VI_FALSE.                */
  1295. /*===========================================================================*/
  1296. ViBoolean hp5313xa_invalidViBooleanRange (ViBoolean val)
  1297. {
  1298.     return ((val != VI_FALSE && val != VI_TRUE) ? VI_TRUE : VI_FALSE);
  1299. }
  1300.  
  1301. /*===========================================================================*/
  1302. /* Function: Short Signed Integer Value Out Of Range - ViInt16               */
  1303. /* Purpose:  This function checks a short signed integer value to see if it  */  
  1304. /*           lies between a minimum and maximum value.  If the value is out  */
  1305. /*           of range, the return value is VI_TRUE, otherwise the return      */
  1306. /*           value is VI_FALSE.                                              */
  1307. /*===========================================================================*/
  1308. ViBoolean hp5313xa_invalidViInt16Range (ViInt16 val, ViInt16 min, ViInt16 max)
  1309. {
  1310.     return ((val < min || val > max) ? VI_TRUE : VI_FALSE);
  1311. }
  1312.  
  1313. /*===========================================================================*/
  1314. /* Function: Long Signed Integer Value Out Of Range - ViInt32                */
  1315. /* Purpose:  This function checks a long signed integer value to see if it   */  
  1316. /*           lies between a minimum and maximum value.  If the value is out  */
  1317. /*           of range,  the return value is VI_TRUE, otherwise the return      */
  1318. /*           value is VI_FALSE.                                              */
  1319. /*===========================================================================*/
  1320. ViBoolean hp5313xa_invalidViInt32Range  (ViInt32 val, ViInt32 min, ViInt32 max)
  1321. {
  1322.     return ((val < min || val > max) ? VI_TRUE : VI_FALSE);
  1323. }
  1324.  
  1325. /*===========================================================================*/
  1326. /* Function: Short Unsigned Integer Value Out Of Range - ViUInt16            */
  1327. /* Purpose:  This function checks a short unsigned integer value to see if it*/  
  1328. /*           lies between a minimum and maximum value.  If the value is out  */
  1329. /*           of range,  the return value is VI_TRUE, otherwise the return      */
  1330. /*           value is VI_FALSE.                                              */
  1331. /*===========================================================================*/
  1332. ViBoolean hp5313xa_invalidViUInt16Range  (ViUInt16 val, ViUInt16 min, ViUInt16 max)
  1333. {
  1334.     return ((val < min || val > max) ? VI_TRUE : VI_FALSE);
  1335. }
  1336.  
  1337. /*===========================================================================*/
  1338. /* Function: Long Unsigned Integer Value Out Of Range - ViUInt32             */
  1339. /* Purpose:  This function checks a long unsigned integer value to see if it */  
  1340. /*           lies between a minimum and maximum value.  If the value is out  */
  1341. /*           of range,  the return value is VI_TRUE, otherwise the return      */
  1342. /*           value is VI_FALSE.                                              */
  1343. /*===========================================================================*/
  1344. ViBoolean hp5313xa_invalidViUInt32Range  (ViUInt32 val, ViUInt32 min, ViUInt32 max)
  1345. {
  1346.     return ((val < min || val > max) ? VI_TRUE : VI_FALSE);
  1347. }
  1348.  
  1349. /*===========================================================================*/
  1350. /* Function: Real (Float) Value Out Of Range - ViReal32                      */
  1351. /* Purpose:  This function checks a real (float) value to see if it lies     */  
  1352. /*           between a minimum and maximum value.  If the value is out of    */
  1353. /*           range, the return value is VI_TRUE, otherwise the return value  */
  1354. /*           is VI_FALSE.                                                    */
  1355. /*===========================================================================*/
  1356. ViBoolean hp5313xa_invalidViReal32Range  (ViReal32 val, ViReal32 min, ViReal32 max)
  1357. {
  1358.     return ((val < min || val > max) ? VI_TRUE : VI_FALSE);
  1359. }
  1360.  
  1361. /*===========================================================================*/
  1362. /* Function: Real (Double) Value Out Of Range - ViReal64                     */
  1363. /* Purpose:  This function checks a real (double) value to see if it lies    */  
  1364. /*           between a minimum and maximum value.  If the value is out of    */
  1365. /*           range, the return value is VI_TRUE, otherwise the return value  */
  1366. /*           is VI_FALSE.                                                    */
  1367. /*===========================================================================*/
  1368. ViBoolean hp5313xa_invalidViReal64Range  (ViReal64 val, ViReal64 min, ViReal64 max)
  1369. {
  1370.     return ((val < min || val > max) ? VI_TRUE : VI_FALSE);
  1371. }
  1372.  
  1373. /*===========================================================================*/
  1374. /* Function: Initialize Clean Up                                             */
  1375. /* Purpose:  This function is used only by the hp5313xa_init function.  When   */
  1376. /*           an error is detected this function is called to close the       */
  1377. /*           open resource manager and instrument object sessions and to     */
  1378. /*           set the instrSession that is returned from hp5313xa_init to       */
  1379. /*           VI_NULL.                                                        */
  1380. /*===========================================================================*/
  1381. ViStatus hp5313xa_initCleanUp (ViSession openRMSession,
  1382.                     ViPSession openInstrSession, ViStatus currentStatus)
  1383. {
  1384.     viClose (*openInstrSession);
  1385.     viClose (openRMSession);
  1386.     *openInstrSession = VI_NULL;
  1387.     
  1388.     return currentStatus;
  1389. }
  1390.  
  1391. /*===========================================================================*/
  1392. /* Function: Read To File From Instrument                                    */
  1393. /* Purpose:  This function is used to read data from the instrument and      */
  1394. /*           write it to a user specified file.                              */
  1395. /*===========================================================================*/
  1396. ViStatus hp5313xa_readToFile (ViSession instrSession, ViString filename,
  1397.                     ViUInt32 readBytes, ViPUInt32 retCount)
  1398. {
  1399.     ViStatus  hp5313xa_status = VI_SUCCESS;
  1400.     ViByte    buffer[BUFFER_SIZE];
  1401.     ViUInt32  bytesReadInstr = 0, bytesWrittenFile = 0;
  1402.     FILE     *targetFile;
  1403.  
  1404.     *retCount = 0L;
  1405.     if ((targetFile = fopen (filename, "wb")) == VI_NULL)
  1406.         return VI_ERROR_FILE_OPEN; /* not defined by VTL */
  1407.  
  1408.     for (;;) {
  1409.         if (readBytes > BUFFER_SIZE)
  1410.             hp5313xa_status = viRead (instrSession, buffer, BUFFER_SIZE, &bytesReadInstr);
  1411.         else
  1412.             hp5313xa_status = viRead (instrSession, buffer, readBytes, &bytesReadInstr);
  1413.  
  1414.         bytesWrittenFile = fwrite (buffer, sizeof (ViByte), (size_t)bytesReadInstr, targetFile);
  1415.         *retCount += bytesWrittenFile;
  1416.         if (bytesWrittenFile < bytesReadInstr)
  1417.             hp5313xa_status = VI_ERROR_FILE_WRITE; /* not defined by VTL */
  1418.  
  1419.         if ((readBytes <= BUFFER_SIZE) || (hp5313xa_status <= 0) || (hp5313xa_status == VI_SUCCESS_TERM_CHAR))
  1420.             break;
  1421.  
  1422.         readBytes -= BUFFER_SIZE;
  1423.     }
  1424.  
  1425.     fclose (targetFile);
  1426.     return hp5313xa_status;
  1427. }
  1428.  
  1429. /*===========================================================================*/
  1430. /* Function: Write From File To Instrument                                   */
  1431. /* Purpose:  This function is used to read data from a user specified file   */
  1432. /*           and write it to the instrument.                                 */
  1433. /*===========================================================================*/
  1434. ViStatus hp5313xa_writeFromFile (ViSession instrSession, ViString filename,
  1435.                     ViUInt32 writeBytes, ViPUInt32 retCount)
  1436. {
  1437.     ViStatus  hp5313xa_status = VI_SUCCESS;
  1438.     ViByte    buffer[BUFFER_SIZE];
  1439.     ViUInt32  bytesRead = 0, bytesWritten = 0;
  1440.     FILE     *sourceFile;
  1441.     ViBoolean sendEnd = VI_FALSE;
  1442.  
  1443.     *retCount = 0L;
  1444.     if ((sourceFile = fopen (filename, "rb")) == VI_NULL)
  1445.         return VI_ERROR_FILE_OPEN; /* not defined by VTL */
  1446.  
  1447.     while (!feof (sourceFile)) {
  1448.         bytesRead = (ViUInt32)fread (buffer, sizeof (ViByte), BUFFER_SIZE, sourceFile);
  1449.         if ((writeBytes > BUFFER_SIZE) && (bytesRead == BUFFER_SIZE)) {
  1450.             viGetAttribute (instrSession, VI_ATTR_SEND_END_EN, &sendEnd);
  1451.             viSetAttribute (instrSession, VI_ATTR_SEND_END_EN, VI_FALSE);
  1452.             hp5313xa_status = viWrite (instrSession, buffer, BUFFER_SIZE, &bytesWritten);
  1453.             viSetAttribute (instrSession, VI_ATTR_SEND_END_EN, sendEnd);
  1454.             writeBytes -= BUFFER_SIZE;
  1455.             *retCount += bytesWritten;
  1456.             if (hp5313xa_status < 0)
  1457.                 break;
  1458.         }
  1459.         else {
  1460.             hp5313xa_status = viWrite (instrSession, buffer, ((bytesRead < writeBytes) ? bytesRead : writeBytes), &bytesWritten);
  1461.             *retCount += bytesWritten;
  1462.             break;
  1463.         }
  1464.     }
  1465.  
  1466.     fclose (sourceFile);
  1467.     return hp5313xa_status;
  1468. }
  1469.  
  1470. /*****************************************************************************/
  1471. /*----------- INSERT INSTRUMENT-DEPENDENT UTILITY ROUTINES HERE -------------*/
  1472. /*****************************************************************************/
  1473.  
  1474. /*=========================================================================*/
  1475. /* Function: Get Instrument Error                                          */
  1476. /* Purpose:  Check for an error. If a error occured within the hp5313xa then */
  1477. /*           that error will add an additional 740 to it to indicate an    */
  1478. /*           instrument error.                                             */
  1479. /*=========================================================================*/
  1480. ViStatus hp5313xa_get_error (ViSession instrSession)
  1481. {
  1482.     ViStatus hp5313xa_status = VI_SUCCESS;
  1483.     ViChar wrtBuf[256];
  1484.     ViUInt32 retCnt = 0;
  1485.     ViInt16 errCode = 0;
  1486.  
  1487.     if ((hp5313xa_status = viWrite (instrSession, ":SYST:ERR?", 10, &retCnt)) < 0)
  1488.         return hp5313xa_status;
  1489.              
  1490.     if ((hp5313xa_status = viScanf (instrSession, "%hd", &errCode)) < 0)
  1491.         return hp5313xa_status;
  1492.  
  1493.     if (errCode != 0)
  1494.         hp5313xa_status = VI_SYSTEM_ERROR;
  1495.  
  1496.     return hp5313xa_status;
  1497. }
  1498.  
  1499. /*=========================================================================*/
  1500. /* Function: Serial Poll                                                   */
  1501. /* Purpose:  This function performs a serial poll on the instrument.       */
  1502. /*           The status byte of the instrument is placed in the response   */
  1503. /*           variable.                                                     */
  1504. /*=========================================================================*/
  1505. ViStatus hp5313xa_poll (ViSession instrSession, ViInt16 *response)
  1506. {
  1507.     ViStatus hp5313xa_status = VI_SUCCESS;   
  1508.  
  1509.     if ((hp5313xa_status = viReadSTB (instrSession, response)) != 0)
  1510.         return hp5313xa_status;
  1511.  
  1512.     return hp5313xa_status;
  1513. }
  1514. /*=========================================================================*/
  1515. /* Function: Set Timeout                                                   */
  1516. /* Purpose:  This function changes or disables the timeout of the device.  */
  1517. /*           Refer to the LabWindows Standard Libraries Reference Manual   */
  1518. /*           for timeout codes.                                            */
  1519. /*=========================================================================*/
  1520. ViStatus hp5313xa_set_timeout (ViSession instrSession, ViAttrState timeOutCode)
  1521. {
  1522.      ViStatus hp5313xa_status = VI_SUCCESS;
  1523.     
  1524.     if (viSetAttribute (instrSession, VI_ATTR_TMO_VALUE, timeOutCode) != 0)
  1525.         return hp5313xa_status;                
  1526.  
  1527.     return hp5313xa_status;
  1528. }
  1529.  
  1530. /*===========================================================================*/
  1531. /* Function: Default Instrument Setup                                        */
  1532. /* Purpose:  This function sends a default setup to the instrument.  This    */
  1533. /*           function is called by the hp5313xa_reset operation and by the     */
  1534. /*           hp5313xa_init function if the reset option has not been           */
  1535. /*           selected.  This function is useful for configuring any          */
  1536. /*           instrument settings that are required by the rest of the        */
  1537. /*           instrument driver functions such as turning headers ON or OFF   */
  1538. /*           or using the long or short form for commands, queries, and data.*/                                    
  1539. /*===========================================================================*/
  1540. ViStatus hp5313xa_defaultInstrSetup (ViSession instrSession)
  1541. {
  1542.     ViStatus hp5313xa_status = VI_SUCCESS;
  1543.     ViUInt32 retCnt = 0;
  1544.     
  1545.     hp5313xa_instrRange instrPtr;
  1546.     instrPtr = malloc (sizeof (struct hp5313xa_statusDataRanges));
  1547.  
  1548.     instrPtr -> triggerMode = 0;
  1549.     instrPtr -> val2 = 0;
  1550.     instrPtr -> val3 = 0;
  1551.     strcpy (instrPtr -> instrDriverRevision, hp5313xa_REVISION);
  1552.     
  1553.     if (viSetAttribute (instrSession, VI_ATTR_USER_DATA, (ViUInt32)instrPtr) != 0)
  1554.         return hp5313xa_status;                                       
  1555.  
  1556.     if (viSetAttribute (instrSession, VI_ATTR_TMO_VALUE, 13) != 0)
  1557.         return hp5313xa_status;                                       
  1558.  
  1559.     return hp5313xa_status;
  1560. }
  1561.  
  1562. /*****************************************************************************/
  1563. /*=== END INSTRUMENT DRIVER SOURCE CODE =====================================*/
  1564. /*****************************************************************************/
  1565.