#include <userint.h>


//  *****************************************************************************************
//
//  Module HP3458
//  This module contains I/O functions for the HP3458 multimeter.                 
//
// Zachary Wolf & Seva
//  8/3/07
//
 static char module_name[] = {"HP3458"}; 
 //#define HP3458_BINARY
//  *****************************************************************************************

// To avoid compiling errors when using local copy of repository
// locate local copy of repository.h file and change all pathes there
// to your local copies location. Do not forget change location of repository.h
// file in the project
<<<<<<< HEAD
=======
#include "repository.h"
#include DELAY //adds most commonly used functions
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b

#include <utility.h>
#include <gpib.h>
#include <formatio.h>
#include <ansi_c.h>
<<<<<<< HEAD
#include <toolbox.h>
=======
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
#include "hp3458.h" //public parameters declaration - software
#include "hp3458_private.h"//private parameters declaration - hardware

//  *****************************************************************************************

//  PRIVATE FUNCTIONS  
int hp3458_open_dev(int gpib_board_addr, int gpib_dev_addr);
void hp3458_close_dev(int dev_ID);
int hp3458_out(int dev_ID, char* buf);
int hp3458_in(int dev_ID, char* buf);
int hp3458_bin(int dev_ID);
int hp3458_in_buf(int dev_ID, char* in_buf);
int hp3458_serial_poll(int dev_ID, int* spoll);
int hp3458_check_dev_open(int dev_ID);
int hp3458_get_errors(int dev_ID);
double GetDintValue(int idx, double scale);

//  *****************************************************************************************

//  PRIVATE DEVICE TABLE  
//
//  This table allows several devices of the same type to be
//  used in the system.
//  dev_addr, contains the GPIB addresses of opened devices
//  dev_descr, contains the device descriptors of opened devices
//  dev_count, contains the number of devices open of this type
 
static int dev_addr[HP3458_MAX_NUM_DEV + 1] =  {-1};
static int dev_descr[HP3458_MAX_NUM_DEV + 1] = {-1};
static int dev_count = 0;

//  *****************************************************************************************

//  PRIVATE GLOBAL VARIABLES  

//  cmd, buffer for GPIB I/O strings
<<<<<<< HEAD
//  msg, buffer for printfs to Standard I/O
=======
//  msg, buffer for messages to Standard I/O
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b

typedef union tag_CHAR_ULONG_BUFFER
{
    DWORD   dw[HP3458_MAX_IN_BUF_4];
    char    in_buf[HP3458_MAX_IN_BUF];
}
CHAR_ULONG_BUFFER;

static CHAR_ULONG_BUFFER inBuf;  

#define HP3458_MAX_CMD 128 

//temporary buffer
static double v_temp[HP3458_MAX_NUM_SAMPLES];

//private variable, use setBreak public function to terminate readings
static int m_break = TRUE;

//  *****************************************************************************************
// ------------------------------------------------------------------------------------------
//  *****************************************************************************************

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_init ---
//!\brief  Opens the device, queries for ID, and initializes the device to a known state.
//!\param  int
//!\param  int
//!\param  int*
//!\retval BOOL
//!\author Seva
//  ***************************************************************************************** 
BOOL hp3458_init(int gpib_board_addr, int gpib_dev_addr, int* ID)
{
    int dev_ID = 0;
    int err;
    char buf[HP3458_MAX_CMD];
    
<<<<<<< HEAD
    printf("\n");
    printf("Initializing the HP3458...\n");
=======
    message("");
    message("Initializing the HP3458...");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b

    // Check input parameters 
    if (gpib_board_addr < 0 || gpib_board_addr > 10)
    {
<<<<<<< HEAD
        Fmt(buf, "%s<gpib_board_addr = %i, out of range\n", gpib_board_addr);
       	printf("%s %s\n", module_name,buf);
=======
        Fmt(buf, "%s<gpib_board_addr = %i, out of range", gpib_board_addr);
       	stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return FALSE;
    }
    if (gpib_dev_addr < 1 || gpib_dev_addr > 30)
    {
<<<<<<< HEAD
        Fmt(buf, "%s<gpib_dev_addr = %i, out of range\n", gpib_dev_addr);
        printf("%s %s\n", module_name,buf);
=======
        Fmt(buf, "%s<gpib_dev_addr = %i, out of range", gpib_dev_addr);
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return FALSE;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        *ID = 1;
        return TRUE;
    #endif
    
    dev_ID = hp3458_open_dev(gpib_board_addr, gpib_dev_addr);
    if (dev_ID <= 0)
    {
<<<<<<< HEAD
        printf("Device was not opened\n");
=======
        //stop_error_msg(module_name,"Device was not opened");
        message("Device was not opened");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return FALSE;
    }

    // Get the HP3458 ID 
    hp3458_out(dev_ID, "ID?");
    err = hp3458_in(dev_ID, buf);
    if (err != 0||strlen(buf)==0)
    {
        hp3458_close_dev(dev_ID);
        //hp3458_out(dev_ID, "RESET");
<<<<<<< HEAD
        printf("%s Problem reading ID\n", module_name);
        
        return FALSE;
    }
    printf(buf);
=======
        stop_error_msg(module_name,"Problem reading ID");
        
        return FALSE;
    }
    message(buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    
    // Leave the HP3458 in a known default state 
    hp3458_out(dev_ID, "PRESET");
    
    hp3458_out(dev_ID, "LFREQ LINE");
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
        hp3458_close_dev(dev_ID);
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        
        return FALSE;
    }

    // Return the ID number 
    *ID = dev_ID;
    
    return TRUE;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_get_voltage ---
//
//!\brief  Gets the voltage applied to the HP3458.
//!\param  int
//!\param  double*
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_get_voltage(int dev_ID, double* voltage)
{
    int dev_open = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};
    int err = 0;

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
   
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Set the A/D integration time 
    hp3458_out(dev_ID, "NPLC 10");
    
 #ifndef HP3458_BINARY
    
    hp3458_out(dev_ID, "OFORMAT ASCII");
    // Trigger the measurement 
    hp3458_out(dev_ID, "TRIG SGL");
    // Get the reading /
    hp3458_in(dev_ID, buf);
    /* Extract the voltage */
    err = Scan(buf, "%s>%f", voltage);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltage", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltage");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
    
 #else
 
    
    hp3458_out(dev_ID, "OFORMAT DINT");
    hp3458_out(dev_ID, "ISCALE?");
    hp3458_in(dev_ID, buf);
    vScale = atof(buf);
    // Trigger the measurement 
    hp3458_out(dev_ID, "TRIG SGL");
    // Get the reading 
    hp3458_bin(dev_ID);
    *voltage = GetDintValue(0,vScale);
#endif 
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_get_frequency ---
//
//!\brief  Gets the frequency of the voltage applied to the HP3458.
//!\param  int
//!\param  double*
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_get_frequency(int dev_ID, double* frequency)
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Set the AC bandwidth 
    hp3458_out(dev_ID, "ACBAND 1 20000");
    
    // Frequency source 
    hp3458_out(dev_ID, "FSOURCE ACV");
    
    // Tell the meter to measure frequency 
    hp3458_out(dev_ID, "FREQ AUTO");
    
    // Trigger the measurement 
    hp3458_out(dev_ID, "TRIG SGL");

    // Get the reading
    hp3458_in(dev_ID, buf);
    
    // Extract the frequency 
    err = Scan(buf, "%s>%f", frequency);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 frequency\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 frequency");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_get_resistance ---
//!\brief  Measures the resistance of the device attached 
//!\param  int
//!\param  double*
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_get_resistance(int dev_ID, double* resistance)
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Set the number of power line cycles to reject noise 
    hp3458_out(dev_ID, "NPLC 10");
    
    // Tell the meter to measure resistance, autorange, % resolution 
    hp3458_out(dev_ID, "FUNC OHM, AUTO, .001");
    
    // Trigger the measurement 
    hp3458_out(dev_ID, "TRIG SGL");

    // Get the reading 
    hp3458_in(dev_ID, buf);
    
    // Extract the frequency 
    err = Scan(buf, "%s>%f", resistance);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 resistance\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 resistance");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
    
    // Put the DVM back in a high impedance state 
    hp3458_out(dev_ID, "PRESET");
 
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_get_temperature ---
//
//!\brief  Measures the temperature of an HP thermistor attached to the HP3458
//!\param  int
//!\param  double*
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_get_temperature(int dev_ID, double* temperature)
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Set the number of power line cycles to reject noise 
    hp3458_out(dev_ID, "NPLC 10");
    
    // Tell the meter to measure resistance, autorange, % resolution 
    hp3458_out(dev_ID, "FUNC OHM, AUTO, .01");
    
    // Set up the math function to convert resistance to temperature 
    hp3458_out(dev_ID, "MATH CTHRM");
    
    // Trigger the measurement 
    hp3458_out(dev_ID, "TRIG SGL");

    // Get the reading 
    hp3458_in(dev_ID, buf);
    
    // Extract the frequency 
    err = Scan(buf, "%s>%f", temperature);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 temperature\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 temperature");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
    
    // Put the DVM back in a high impedance state 
    hp3458_out(dev_ID, "PRESET");
}


//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_setup_timed_voltage_samples ---
//
//!\brief  Sets up voltmeter to sample the voltages
//!\param  int
//!\param  double
//!\param  double
//!\param  int
//!\param  double   
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_setup_timed_voltage_samples(int dev_ID, double v_range, double num_plc, int num_samples, double delta_t)
{
    int dev_open;
    int err;
    char buf[HP3458_MAX_CMD];

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s \n", "%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (delta_t < .001 || delta_t > 10)
    {
        Fmt(buf, "%s<delta_t = %f, out of range", delta_t);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Turn off autorange 
    hp3458_out(dev_ID, "ARANGE OFF");
    
    // Turn off the autozero 
    hp3458_out(dev_ID, "AZERO OFF");
    
    // Turn off the display 
    hp3458_out(dev_ID, "DISP OFF");
    
    // Turn off math functions 
    hp3458_out(dev_ID, "MATH OFF");
    
    // Turn on the keyboard lock 
    hp3458_out(dev_ID, "LOCK ON");
    
    // Set the output format 
    hp3458_out(dev_ID, "OFORMAT ASCII");
     
    // Set the voltage range 
    Fmt(buf, "%s<DCV %f", v_range);
    hp3458_out(dev_ID, buf);
    
    // Set the A/D integration time 
    Fmt(buf, "%s<NPLC %f", num_plc);
    hp3458_out(dev_ID, buf);
    
    // Set the timer 
    Fmt(buf, "%s<TIMER %f", delta_t);
    hp3458_out(dev_ID, buf);
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
    hp3458_out(dev_ID, "MEM FIFO");
    hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Set up the trigger 
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, TIMER", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
        hp3458_close_dev(dev_ID);
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name); 
=======
        stop_error_msg(module_name,"The HP3458 has an error"); 
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    } 
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_set_samples_number ---
//
//!\brief  Saves poles positions to file
//!\param  int
//!\param  int
//!\param  double    
//!\retval  void int double char
//!\author  Seva
//  *****************************************************************************************
void hp3458_set_samples_number(int dev_ID, int num_samples, double delta_t)  
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD]= {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (delta_t < .001 || delta_t > 100)
    {
        Fmt(buf, "%s<delta_t = %f, out of range", delta_t);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
  //  hp3458_out(dev_ID, "MEM FIFO");
  //  hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Set the timer 
    Fmt(buf, "%s<TIMER %f", delta_t);
    hp3458_out(dev_ID, buf);
    // Set up the trigger 
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, TIMER", num_samples);
    hp3458_out(dev_ID, buf);
}

//
//  *****************************************************************************************

//                    --- HP3458.c::hp3458_trigger_timed_voltage_samples ---
//
//!\brief   Triggers internally the voltage sampling for number of samples defined before
//!\param  int
//!\retval  none
//!\author  Zachary Wolf 
//  *****************************************************************************************

void hp3458_trigger_timed_voltage_samples(int dev_ID)
{
    int dev_open = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};
    
    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Turn on the input buffer 
    hp3458_out(dev_ID, "INBUF ON");
    
    // Trigger the readings 
    hp3458_out(dev_ID, "TRIG SGL");
}

//
//  *****************************************************************************************

//                    --- HP3458.c::hp3458_collect_timed_voltage_samples ---
//
//!\brief   Reads from memory requested number of time samples
//!\param  int
//!\param  int
//!\param  double[]
//!\retval  none
//!\author  Seva
//  *****************************************************************************************

void hp3458_collect_timed_voltage_samples(int dev_ID, int num_samples, double voltages[])
{
    int dev_open = 0;
    int err = 0;
    int spoll = 0;
    int mcount = 0;
    int i = 0; 
    
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
       printf("%s %s\n", module_name,buf);
=======
       stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        for (i = 0; i <= num_samples; i++) voltages[i] = (double) i / (double) num_samples;
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    m_break = FALSE;
    
    // Wait until the readings are complete 
    do
    {
        hp3458_serial_poll(dev_ID, &spoll);
        //printf("spoll = %i\n", spoll);
<<<<<<< HEAD
        Delay(1.0);
=======
        delay(1.0);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }while ((spoll & 16) == 0 && !m_break); // while bit 4 is off 
    
    if(m_break)
        return;
        
    // Turn off the input buffer 
    hp3458_out(dev_ID, "INBUF OFF");
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        hp3458_close_dev(dev_ID);
        return;
    }
    
    // Check the number of readings in memory 
    hp3458_out(dev_ID, "MCOUNT?");
    hp3458_in(dev_ID, buf);
    Scan(buf, "%s>%i", &mcount);
    if (mcount != num_samples)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Put all the readings in the HP3458 output buffer 
    Fmt(buf, "%s<RMEM 1, %i", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Get the readings 
    hp3458_in_buf(dev_ID, inBuf.in_buf);
    
    // Extract the voltages from the string 
    for (i = 0; i < num_samples; i++) v_temp[i] = -1000.;
    err = Scan(inBuf.in_buf, "%s>%*f[x]", num_samples, v_temp);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }

    // Using RMEM, the order is last in first out, reverse the order 
    for (i = 0; i < num_samples; i++)
    {
        voltages[i] = v_temp[num_samples - 1 - i];
<<<<<<< HEAD
        if (voltages[i] == -1000.) printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        if (voltages[i] == -1000.) stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    } 
}


//
//  *****************************************************************************************

//                    --- HP3458.c::hp3458_waitfor_timed_voltage_samples ---
//
//!\brief   Waits for time samples to be collected
//!\param  int
//!\retval  none
//!\author  Seva
//  *****************************************************************************************

void hp3458_waitfor_timed_voltage_samples(int dev_ID)
{
    int dev_open = 0;
    int err = 0;
    int spoll = 0;
    int mcount = 0;
    int i = 0; 
    
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    m_break = FALSE;
    
    // Wait until the readings are complete 
    do
    {
        hp3458_serial_poll(dev_ID, &spoll);
        //printf("spoll = %i\n", spoll);
<<<<<<< HEAD
        Delay(0.5);
=======
        delay(0.5);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }while ((spoll & 16) == 0 && !m_break); // while bit 4 is off
}


//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_digitize ---
//
//!\brief  Samples the voltage applied to the HP3458.
//!\param  int
//!\param  double
//!\param  double
//!\param  double []
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_digitize(int dev_ID, int num_samples, double delta_t, double voltages[])
{
    
    const double max_input = 10.;               // Determines voltage range 
	
    int dev_open = 0;
    int err = 0;
    int spoll = 0;
    int mcount = 0;
    int i = 0;
    char buf[HP3458_MAX_CMD];

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (delta_t < .00006 || delta_t > 1.)
    {
        Fmt(buf, "%s<delta_t = %f, out of range", delta_t);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Turn off autorange 
    hp3458_out(dev_ID, "ARANGE OFF");
    
    // Turn off the autozero 
    hp3458_out(dev_ID, "AZERO OFF");
    
    // Turn off the display 
    hp3458_out(dev_ID, "DISP OFF");
    
    // Turn off math functions 
    hp3458_out(dev_ID, "MATH OFF");
    
    // Turn on the keyboard lock 
    hp3458_out(dev_ID, "LOCK ON");
    
    // Set the output format 
    hp3458_out(dev_ID, "OFORMAT ASCII");
     
    // Set the voltage range 
    Fmt(buf, "%s<DCV %f", max_input);
    hp3458_out(dev_ID, buf);
    
    // Set the A/D integration time 
    Fmt(buf, "%s<APER %f", delta_t - 50.e-6);
    hp3458_out(dev_ID, buf);
    
    // Set the timer 
    Fmt(buf, "%s<TIMER %f", delta_t);
    hp3458_out(dev_ID, buf);
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
    hp3458_out(dev_ID, "MEM FIFO");
    hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Set up the trigger 
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, TIMER", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
        hp3458_close_dev(dev_ID); 
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name); 
=======
        stop_error_msg(module_name,"The HP3458 has an error"); 
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Turn on the input buffer 
    hp3458_out(dev_ID, "INBUF ON");
    
    // Trigger the readings 
    hp3458_out(dev_ID, "TRIG SGL");
    
    // Wait until the readings are complete 
    do
    {
        hp3458_serial_poll(dev_ID, &spoll);
        //printf("spoll = %i\n", spoll);
<<<<<<< HEAD
        Delay(0.5);
=======
        delay(0.5);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }while ((spoll & 16) == 0); // while bit 4 is off 
    
    // Turn off the input buffer 
    hp3458_out(dev_ID, "INBUF OFF");
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
        hp3458_close_dev(dev_ID); 
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Check the number of readings in memory 
    hp3458_out(dev_ID, "MCOUNT?");
    hp3458_in(dev_ID, buf);
    Scan(buf, "%s>%i", &mcount);
    if (mcount != num_samples)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Put all the readings in the HP3458 output buffer 
    Fmt(buf, "%s<RMEM 1, %i", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Get the readings 
    hp3458_in_buf(dev_ID, inBuf.in_buf);
    
    // Extract the voltages from the string 
    for (i = 0; i < num_samples; i++) v_temp[i] = -1000.;
    err = Scan(inBuf.in_buf, "%s>%*f[x]", num_samples, v_temp);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }

    // Using RMEM, the order is last in first out, reverse the order 
    for (i = 0; i < num_samples; i++)
    {
        voltages[i] = v_temp[num_samples - 1 - i];
<<<<<<< HEAD
        if (voltages[i] == -1000.) printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        if (voltages[i] == -1000.) stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_get_triggered_voltage_samples ---
//
//!\brief  Generates samples, waiting for data, and read them
//!\param  int
//!\param  double
//!\param  double 
//!\param  int
//!\param  double[]    
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_get_triggered_voltage_samples(int dev_ID, double v_range, double num_plc,
	int num_samples, double voltages[])
{
    int dev_open = 0;
    int err = 0;
    int spoll = 0;
    int mcount = 0;
    int i;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
   
    // Turn off autorange 
    hp3458_out(dev_ID, "ARANGE OFF");
    
    // Turn off the autozero 
    hp3458_out(dev_ID, "AZERO OFF");
    
    // Turn off the display 
    hp3458_out(dev_ID, "DISP OFF");
    
    // Turn off math functions 
    hp3458_out(dev_ID, "MATH OFF");
    
    // Turn on the keyboard lock 
    hp3458_out(dev_ID, "LOCK ON");
    
    // Set the output format 
    hp3458_out(dev_ID, "OFORMAT ASCII");
     
    // Set the voltage range 
    Fmt(buf, "%s<DCV %f", v_range);
    hp3458_out(dev_ID, buf);
    
    // Set the A/D integration time 
    Fmt(buf, "%s<NPLC %f", num_plc);
    hp3458_out(dev_ID, buf);
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
    hp3458_out(dev_ID, "MEM FIFO");
    hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Set up the trigger 
    hp3458_out(dev_ID, "TRIG HOLD");
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, EXT", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        hp3458_close_dev(dev_ID);
        return;
    }
    
    // Turn on the input buffer 
    hp3458_out(dev_ID, "INBUF ON");
    
    // Trigger the readings 
    hp3458_out(dev_ID, "TRIG SGL");
    
    // Wait until the readings are complete 
    do
    {
        hp3458_serial_poll(dev_ID, &spoll);
        //printf("spoll = %i\n", spoll);
<<<<<<< HEAD
        Delay(0.5);
=======
        delay(0.5);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }while ((spoll & 16) == 0); // while bit 4 is off 
    
    // Turn off the input buffer 
    hp3458_out(dev_ID, "INBUF OFF");
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
        hp3458_close_dev(dev_ID);
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Check the number of readings in memory 
    hp3458_out(dev_ID, "MCOUNT?");
    hp3458_in(dev_ID, buf);
    Scan(buf, "%s>%i", &mcount);
    if (mcount != num_samples)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages: mcount!=num_samples\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages: mcount!=num_samples");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Put the readings in the HP3458 output buffer 
    Fmt(buf, "%s<RMEM 1, %i", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Get the readings 
    hp3458_in_buf(dev_ID, inBuf.in_buf);
    
    // Extract the voltages from the string
    for (i = 0; i < num_samples; i++) v_temp[i] = -1000.;
    err = Scan(inBuf.in_buf, "%s>%*f[x]", num_samples, v_temp);
    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }

    // Using RMEM, the order is last in first out, reverse the order 
    for (i = 0; i < num_samples; i++)
    {
        voltages[i] = v_temp[num_samples - 1 - i];
<<<<<<< HEAD
        if (voltages[i] == -1000.) printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        if (voltages[i] == -1000.) stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    } 
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_setup_triggered_voltage_samples ---
//
//!\brief  Setup device for externally triggered voltage samples
//!\param  int
//!\param  double
//!\param  double
//!\param  int
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_setup_triggered_voltage_samples(int dev_ID, double v_range, double num_plc, int num_samples)
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET NORM");
    
    // Turn off autorange 
    hp3458_out(dev_ID, "ARANGE OFF");
    
    // Turn off the autozero 
    hp3458_out(dev_ID, "AZERO OFF");
    
    // Turn off the display 
    hp3458_out(dev_ID, "DISP OFF");
    
    // Turn off math functions 
    hp3458_out(dev_ID, "MATH OFF");
    
    // Turn on the keyboard lock 
    hp3458_out(dev_ID, "LOCK ON");
    
    // Set the output format 
    hp3458_out(dev_ID, "OFORMAT ASCII");
     
    // Set the voltage range 
    Fmt(buf, "%s<DCV %f", v_range);
    hp3458_out(dev_ID, buf);
    
    // Set the A/D integration time 
    Fmt(buf, "%s<NPLC %f", num_plc);
    hp3458_out(dev_ID, buf);
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
    hp3458_out(dev_ID, "MEM FIFO");
    hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Hold the readings until commanded to begin 
    hp3458_out(dev_ID, "TRIG HOLD");
    
    // Set up the trigger 
    hp3458_out(dev_ID, "TBUFF OFF"); //prevents error 4 - trigger to fast, page 4-8 of manual, use for debug purposes only
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, EXT", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        hp3458_close_dev(dev_ID);
        return;
    }
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_set_samples_number ---
//
//!\brief  Adds extra timer samples at the end of scan
//!\param  int
//!\param  int
//!\param  double 
//!\param  double 
//!\retval  void int double char
//!\author  Seva
//  *****************************************************************************************
void hp3458_add_samples_number(int dev_ID, int num_samples, double delta_t, double num_plc)  
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD]= {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (delta_t < .001 || delta_t > 100)
    {
        Fmt(buf, "%s<delta_t = %f, out of range", delta_t);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
	
	// Set the A/D integration time 
	if (num_plc < -1000. || num_plc > 1000) 
	{
		Fmt(buf, "%s<num_plc = %f, out of range", num_plc);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
	}
    Fmt(buf, "%s<NPLC %f", num_plc);
    hp3458_out(dev_ID, buf);
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
  //  hp3458_out(dev_ID, "MEM FIFO");
  //  hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Set the timer 
    Fmt(buf, "%s<TIMER %f", delta_t);
    hp3458_out(dev_ID, buf);
    // Set up the trigger 
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, TIMER", num_samples);
    hp3458_out(dev_ID, buf);
}


//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_set_triggered_voltage_samples ---
//
//!\brief  Setup devive for externally triggered voltage samples, binary data!
//!\param  int
//!\param  double
//!\param  double
//!\param  int    
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_set_triggered_voltage_samples(int dev_ID, double v_range, double num_plc, int num_samples)
{
    int dev_open = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    // Clear memory the HP3458 
    hp3458_out(dev_ID, "SCRATCH");
    // Preset the HP3458 
    hp3458_out(dev_ID, "PRESET");
    
    // Turn off autorange 
    hp3458_out(dev_ID, "ARANGE OFF");
    
    // Turn off the autozero 
    hp3458_out(dev_ID, "AZERO OFF");
    
    // Turn off the display 
    //hp3458_out(dev_ID, "DISP OFF");
    hp3458_out(dev_ID, "DISP OFF");  
	
    // Turn off math functions 
    hp3458_out(dev_ID, "MATH OFF");
    
    // Turn on the keyboard lock 
    hp3458_out(dev_ID, "LOCK ON");
    
   // Set the output format 
   // hp3458_out(dev_ID, "OFORMAT DINT");
    hp3458_out(dev_ID, "OFORMAT SREAL");
     
    // Set the voltage range 
    Fmt(buf, "%s<DCV %f", v_range);
    hp3458_out(dev_ID, buf);
    
    // Set the A/D integration time 
    Fmt(buf, "%s<NPLC %f", num_plc);
    hp3458_out(dev_ID, buf);
    
    // Send the readings to the memory 
    Fmt(buf, "%s<MSIZE %i", num_samples * 4 + 10);
    hp3458_out(dev_ID, buf);
    hp3458_out(dev_ID, "MEM FIFO");
    hp3458_out(dev_ID, "MFORMAT DINT");
    
    // Hold the readings until commanded to begin 
    hp3458_out(dev_ID, "TRIG HOLD");
    
    // Set up the trigger 
    hp3458_out(dev_ID, "TARM AUTO");
    Fmt(buf, "%s<NRDGS %i, EXT", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        hp3458_close_dev(dev_ID);
        return;
    }
}


//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_start_triggered_voltage_samples ---
//
//!\brief  Begins the externally triggered voltage sampling.
//!\param  int
//!\retval void
//!\author Seva
//  *****************************************************************************************
void hp3458_start_triggered_voltage_samples(int dev_ID)
{
    int dev_open = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
   
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Turn on the input buffer 
    hp3458_out(dev_ID, "INBUF ON");
    
    // Begin sampling 
    hp3458_out(dev_ID, "TRIG SGL");
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_collect_triggered_voltage_samples ---
//
//!\brief  Collects the triggered voltage samples
//!\param  int
//!\param  int 
//!\param  double[]
//!\retval int
//!\author  Seva
//  *****************************************************************************************
int hp3458_collect_triggered_voltage_samples(int dev_ID, int num_samples, double voltages[])
{
    int dev_open = 0;
    int err = 0;
    int spoll = 0;
    int mcount = 0;
    int i = 0;
	int result=0;
	char substr[100]= {'\0'};
    char buf[HP3458_MAX_CMD] = {'\0'};
	char datastr[100000];
    
    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return 0;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return 0;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        for (i = 0; i < num_samples; i++) voltages[i] = (double)(i - (num_samples - 1) / 2.) / (double)((num_samples - 1) / 2.);
        return 0;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return 0;
    }
    
    m_break = FALSE;
    
    // Wait until the readings are complete 
    do
    {
        hp3458_serial_poll(dev_ID, &spoll);
        //printf("spoll = %i\n", spoll);
<<<<<<< HEAD
        Delay(0.5);
=======
        delay(0.5);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }while ((spoll & 16) == 0 && !m_break); // while bit 4 is off 
    
    if(m_break)
    {
        // Leave the HP3458 in a known default state 
    //  hp3458_out(dev_ID, "PRESET");
   //   //hp3458_exit( dev_ID);
        return 1;
    }
    // Turn off the input buffer 
    hp3458_out(dev_ID, "INBUF OFF");
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
        hp3458_close_dev(dev_ID); 
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return 0;
    }
    
    // Check the number of readings in memory 
    hp3458_out(dev_ID, "MCOUNT?");
    hp3458_in(dev_ID, buf);
    Scan(buf, "%s>%i", &mcount);
<<<<<<< HEAD
    if (mcount != num_samples) printf("%s Problem getting HP3458 voltages\n", module_name);
=======
    if (mcount != num_samples) stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    
    // Put all the readings in the HP3458 output buffer 
    Fmt(buf, "%s<RMEM 1, %i", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Get the readings 
    hp3458_in_buf(dev_ID, inBuf.in_buf);
    
    // Extract the voltages from the string 
    for (i = 0; i < num_samples; i++) v_temp[i] = -1000.;
	num_samples=num_samples-1;
    err = Scan(inBuf.in_buf, "%s>%*f[x]", num_samples, v_temp);
	 //err = Scan(inBuf.in_buf, "%s>%5f[p10x]", v_temp);  
	
	strcpy(datastr,inBuf.in_buf);
	
	result = sscanf(inBuf.in_buf,"%[^',']",substr);
	strlen(substr);

    if (err != 1)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }

    // Using RMEM, the order is last in first out, reverse the order 
    for (i = 0; i < num_samples; i++)
    {
        voltages[i] = v_temp[num_samples - 1 - i];
		
        if (voltages[i] == -1000.)
<<<<<<< HEAD
            printf("%s Problem getting HP3458 voltages\n", module_name);
=======
            stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
    
    return 0;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_collect_triggered_voltages ---
//
//!\brief  Collects the triggered voltage samples
//!\param  int
//!\param  int
//!\param  double[]
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_collect_triggered_voltages(int dev_ID, int num_samples, double voltages[])
{
    int dev_open = 0;
    int err = 0;
    int spoll = 0;
    int mcount = 0;
    long i = 0;
    float res =0.;
    int it=0;
    char t[4]= {0,0,0,0};
    char buf[HP3458_MAX_CMD] = {'\0'};
    
    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    if (num_samples < 1 || num_samples > HP3458_MAX_NUM_SAMPLES)  
    {
        Fmt(buf, "%s<num_samples = %i, out of range", num_samples);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        for (i = 0; i < num_samples; i++) voltages[i] = (double)(i - (num_samples - 1) / 2.) / (double)((num_samples - 1) / 2.);
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Wait until the readings are complete 
    do
    {
        hp3458_serial_poll(dev_ID, &spoll);
        //printf("spoll = %i\n", spoll);
        Delay(0.5);
    }
    while ((spoll & 16) == 0);  // while bit 4 is off 
    
    // Turn off the input buffer 
    hp3458_out(dev_ID, "INBUF OFF");
    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
         hp3458_close_dev(dev_ID); 
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Check the number of readings in memory 
    hp3458_out(dev_ID, "MCOUNT?");
    hp3458_in(dev_ID, buf);
    Scan(buf, "%s>%i", &mcount);
<<<<<<< HEAD
    if (mcount != num_samples) printf("%s Problem getting HP3458 voltages\n", module_name);
=======
    if (mcount != num_samples) stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    
    // Put the readings in the HP3458 output buffer 
    Fmt(buf, "%s<RMEM 1, %i", num_samples);
    hp3458_out(dev_ID, buf);
    
    // Get the readings 
    hp3458_in_buf(dev_ID, inBuf.in_buf);
    
    
    
    // Extract the voltages from the string
    for (i = 0; i < num_samples; i++)
        v_temp[i] = -1000.;
//  err = Scan(inBuf.in_buf, "%s>%*f[x]", num_samples, v_temp);
//      if (err != 1)
//      {
//          hp3458_error("Problem getting HP3458 voltages");
//      }

    // Using RMEM, the order is last in first out, reverse the order 
    for (i = 0; i < num_samples; i++)
    {
        t[0] = inBuf.in_buf[i*4];
        t[1] = inBuf.in_buf[i*4+1];
        t[2] = inBuf.in_buf[i*4+2];
        t[3] = inBuf.in_buf[i*4+3];
        
        //reverse order 
        inBuf.in_buf[i*4]   =   t[3];
        inBuf.in_buf[i*4+1] =   t[2];
        inBuf.in_buf[i*4+2] =   t[1];
        inBuf.in_buf[i*4+3] =   t[0]; 
        
        //reverse bytes order in words
  /*        inBuf.in_buf[i*4]   =   t[1];
        inBuf.in_buf[i*4+1] =   t[0];
        inBuf.in_buf[i*4+2] =   t[3];
        inBuf.in_buf[i*4+3] =   t[2];*/ 
        
        //reverse bytes order in reverse words
    /*  inBuf.in_buf[i*4]   =   t[2];
        inBuf.in_buf[i*4+1] =   t[3];
        inBuf.in_buf[i*4+2] =   t[0];
        inBuf.in_buf[i*4+3] =   t[1];*/ 
         res =   inBuf.dw[i]; 
         it = t[0];
         it = t[1];
         it = t[2];
         it = t[3];
        voltages[i] = inBuf.dw[i];//v_temp[num_samples - 1 - i];
        
        if (voltages[i] == -1000.)
<<<<<<< HEAD
            printf("%s Problem getting HP3458 voltages\n", module_name);
=======
            stop_error_msg(module_name,"Problem getting HP3458 voltages");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_exit ---
//
//!\brief  Leaves the HP3458 in a desired state and closes the device.
//!\retval  void
//!\author  Seva
//  *****************************************************************************************
void hp3458_exit(int dev_ID)
{
    int dev_open = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};
    m_break = TRUE;
    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
        return;        // OK, already closed 
    }

    // Leave the device in the desired state 
    // Reset the HP3458 to the power on state 
    hp3458_out(dev_ID, "RESET");

    // Close the device 
    hp3458_close_dev(dev_ID);
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_open_dev ---
//
//!\brief  Opens the device and returns an identifier for future references to the device.
//!\param  int
//!\param  int 
//!\retval int
//!\author  Seva
//  *****************************************************************************************
int hp3458_open_dev(int gpib_board_addr, int gpib_dev_addr)
{
    int i = 0;
    int dev_ID = -1;
    int descr = 0;
    int err = 0;
    char buf[HP3458_MAX_CMD] = {'\0'};

    // Check input parameters 
    if (gpib_board_addr < 0 || gpib_board_addr > 10)
    {
        Fmt(buf, "%s<gpib_board_addr = %i, out of range", gpib_board_addr);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }
    if (gpib_dev_addr < 1 || gpib_dev_addr > 30)
    {
        Fmt(buf, "%s<gpib_dev_addr = %i, out of range", gpib_dev_addr);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }

    // Check to see if the device is already in the device table 
    for (i = 1; i <= HP3458_MAX_NUM_DEV; i++)
    {
        if (dev_addr[i] == gpib_dev_addr)
        {
            dev_ID = i;
            return dev_ID; //already open
        }
    }
    
    // Find an ID for the device 
    for (i = 1; i <= HP3458_MAX_NUM_DEV; i++)
    {
        if (dev_addr[i] == 0)
        {
            dev_ID = i;
            break;
        }
    }
     
    // If an entry could not be opened in the device table, return an error 
    if (dev_ID <= 0)
    {
<<<<<<< HEAD
        printf("%s Problem getting HP3458 voltages\n", module_name);
=======
        stop_error_msg(module_name,"The device could not be opened");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return  -1;
    }
    
        SetBreakOnLibraryErrors (0); 
    
    // Open the device 
    // Do not terminate write with EOI, add CR LF in sending routine 
    // Terminate read on LF, remove CR LF in read routine 
    descr = ibdev(gpib_board_addr, gpib_dev_addr, 0, T300s, 0, 0x140A);
    if (descr <= 0)
    {
<<<<<<< HEAD
        //printf("%s Problem getting HP3458 voltages\n", module_name);
        printf("The device could not be opened\n");
=======
        //stop_error_msg(module_name,"The device could not be opened");
        message("The device could not be opened");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        SetBreakOnLibraryErrors (1); 
        return  -1;
    }
    
    // Open successful, update device table 
    dev_addr[dev_ID] = gpib_dev_addr;
    dev_descr[dev_ID] = descr;
    dev_count++;
    
    // Clear the device 
    err = ibclr(dev_descr[dev_ID]);
    if (err & 0x8000)
    {
<<<<<<< HEAD
        printf("%s Problem clearing device\n", module_name);
=======
        stop_error_msg(module_name,"Problem clearing device");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
    
    return dev_ID;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_close_dev ---
//
//!\brief  Close the device
//!\param  int
//!\retval void
//!\author Seva
//  *****************************************************************************************
void hp3458_close_dev(int dev_ID)
{
    //  
    if (dev_descr[dev_ID] != 0)
    {
        ibonl(dev_descr[dev_ID], 0);
        dev_descr[dev_ID] = 0;
        dev_addr[dev_ID] = 0;
        dev_count--;
    }
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_out ---
//!\brief  Writes a buffer of data to the device.
//!\param  int
//!\param  char*    
//!\retval int
//!\author  Seva
//  *****************************************************************************************
int hp3458_out(int dev_ID, char* buf)
{
    char out_buf[HP3458_MAX_CMD] = {'\0'};
    int nbytes = 0;
    int err = 0;
    
#ifdef DUMMY_DEVICES
    return 0;
#endif
    
<<<<<<< HEAD
    // Append CR LF to the printf //
    Fmt(out_buf, "%s<%s%c%c", buf, 13,10); 
    
    // Send the printf //
=======
    // Append CR LF to the message //
    Fmt(out_buf, "%s<%s%c%c", buf, 13,10); 
    
    // Send the message //
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    nbytes = StringLength(out_buf);
    err = ibwrt(dev_descr[dev_ID], out_buf, (long)nbytes);
    
    // Check for errors //
    if (err & 0x8000)
    {
        Fmt(out_buf, "%s<ibsta = %x, problem sending GPIB command", err);
<<<<<<< HEAD
        printf("%s %s\n", module_name,out_buf);
=======
        stop_error_msg(module_name,out_buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }
    return 0;
}


//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_in ---
//
//!\brief  Reads a buffer of data from the device
//!\param  int
//!\param  char[]    
//!\retval int 
//!\author  Seva
//  *****************************************************************************************
int hp3458_in(int dev_ID, char* buf)
{
    int err = 0;
    char in_buf[HP3458_MAX_CMD] = {'\0'};
    
#ifdef DUMMY_DEVICES
    strcpy(buf,"Dummy");
    return 0;
#endif
    
<<<<<<< HEAD
    // Read the printf 
=======
    // Read the message 
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    err = ibrd(dev_descr[dev_ID], inBuf.in_buf, HP3458_MAX_CMD);
    
    // Check for errors //
    if (err & 0x8000)
    {
        Fmt(in_buf, "%s<ibsta = %x, problem receiving GPIB input", err);
<<<<<<< HEAD
        printf("%s %s\n", module_name,in_buf);
=======
        stop_error_msg(module_name,in_buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }
    
    // Remove the CR LF terminator //
    Scan(inBuf.in_buf, "%s[t13]>%s[t-]", buf);
    
    return 0;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_bin ---
//
//!\brief  Reads a binary buffer of data from the device
//!\param  int
//!\param  char[]    
//!\retval int 
//!\author  Seva
//  *****************************************************************************************
int hp3458_bin(int dev_ID)
{
    int err = 0;
    char buf[HP3458_MAX_CMD];
    
<<<<<<< HEAD
    // Read the printf 
=======
    // Read the message 
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    err = ibrd(dev_descr[dev_ID], &inBuf.dw[0], 4);
    
    // Check for errors 
    if (err & 0x8000)
    {
        Fmt(buf, "%s<ibsta = %x, problem receiving GPIB input", err);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    } 
    return 0;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_in_buf ---
//
//!\brief  Reads a buffer of data from the device
//!\param  int
//!\param  char[]    
//!\retval int
//!\author  Seva
//  *****************************************************************************************
int hp3458_in_buf(int dev_ID, char* buf)
{
    int err = 0;
    char in_buf[HP3458_MAX_CMD]; 
    
<<<<<<< HEAD
    // Read the printf
=======
    // Read the message
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    err = ibrd(dev_descr[dev_ID], inBuf.in_buf, HP3458_MAX_IN_BUF);
    
    // Check for errors 
    if (err & 0x8000)
    {
        Fmt(in_buf, "%s<ibsta = %x, problem receiving GPIB input", err);
<<<<<<< HEAD
        printf("%s %s\n", module_name,in_buf);
=======
        stop_error_msg(module_name,in_buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }
    
    // Remove the CR LF terminator 
    Scan(inBuf.in_buf, "%s[t13]>%s[t-]", buf);
    return 0;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_serial_poll ---
//
//!\brief  A serial poll of the device. A value of 16 indicates the HP3458 is ready for
//\a command.  A value of 32 indicates an error.
//!\param  int
//!\param  int*
//!\retval int
//!\author Seva
//  *****************************************************************************************
int hp3458_serial_poll(int dev_ID, int* spoll)
{
    int err = 0;
    char spoll_char = 0;
    char buf[HP3458_MAX_CMD];
    
    // Perform the serial poll
    err = ibrsp(dev_descr[dev_ID], &spoll_char);
    
    // Check for errors 
    if (err & 0x8000)
    {
        Fmt(buf, "%s<ibsta = %x, problem performing GPIB serial poll", err);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }
    
    // Return the result as an integer 
    Scan(&spoll_char, "%c>%i", spoll);
    return 0;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_check_dev_open ---
//
//!\brief  Checks whether device is open. If it is opened, a 1 is returned, 0 otherwise.
//!\param  int
//!\retval int 
//!\author  Seva
//  *****************************************************************************************
int hp3458_check_dev_open(int dev_ID)
{
    // See if the board descriptor has a positive value 
    if (dev_descr[dev_ID] > 0)
    {
        return  1;    // Open 
    }
    else
    {
        return 0;     // Not open 
    }
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp3458_get_errors ---
//!\brief  Get device errors
//!\param  int
//!\retval  int
//!\author  Seva
//  *****************************************************************************************
int hp3458_get_errors(int dev_ID)
{
    int status = 0;
    char buf[HP3458_MAX_CMD]= {'\0'};
//	char error_descr[HP3458_MAX_CMD]= {'\0'};

    // Get the HP3458 error status 
    hp3458_out(dev_ID, "ERRSTR?");
    hp3458_in(dev_ID, buf);
    
    // Extract the status value 
	Scan (buf, "%s>%i[w1]", &status); //, error_descr
    
	// Alert the operator if there is an error 
    if (status != 0)
    {
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return -1;
    }
    return 0;
}

//                                                                                                                                                                                           
//  *****************************************************************************************
                                                                                                                
//                    --- Hp3458.c::GetDintValue ---                                                                                                                                      
//                                                                                                                                                                                            
//!\brief   Converts binary DINT output to double and applies scale                                                                                                                                                            
//!\param   int - index                                                                                                                                                                              
//!\param   double - scale                                                                                                                                                                               
//!\retval  double                                                                                                                                                                               
//!\author  Seva                                                                                                                                                                              
//  *****************************************************************************************

double GetDintValue(int idx, double scale)
{
    DWORD dw = 0;
    char t[4];
    long res = 0;
    double retVal = 0.;
    idx*=4;
    t[0] = inBuf.in_buf[idx];
    t[1] = inBuf.in_buf[idx+1];
    t[2] = inBuf.in_buf[idx+2];
    t[3] = inBuf.in_buf[idx+3];
        
    //reverse order 
    inBuf.in_buf[idx]       =   t[3];
    inBuf.in_buf[idx+1]     =   t[2];
    inBuf.in_buf[idx+2]     =   t[1];
    inBuf.in_buf[idx+3]     =   t[0]; 
    dw = inBuf.dw[idx];
    if(dw&(0x80000000))
    {
        res = -((long)(0x80000000-(dw&0x7fffffff)));
    }
    else
    {
        res = (long)(dw&0xfffffff);
    } 
    retVal = ((double)(res)*scale);
    return  retVal;
}

//
//  *****************************************************************************************

//                    --- hp3458.c::hp3458_is_dev_open ---
//
//!\brief   Checks whether device is open using address
//!\param  int
//!\retval int
//!\author Seva
//  *****************************************************************************************
int hp3458_is_dev_open(int dev_address)
{
    int i = 0;
    for (i = 1; i <= HP3458_MAX_NUM_DEV; i++)
    {
        if (dev_addr[i] == dev_address)
        {
            return 1;
        }
    }
    return 0;
}

//
//  *****************************************************************************************
//                    --- hp3458.c::hp_3458_setBreak ---
//
//!\brief  Set break parameter
//!\param  int
//!\retval none
//!\author   Seva
//  *****************************************************************************************
 
void hp3458_setBreak(int set)
{
    m_break = set;
}

//
//  *****************************************************************************************

//                    --- hp3458.c::hp_3458_GetMaxSamplesNumber ---
//
//!\brief   Get maximum samples number
//!\param  none
//!\retval  int
//!\author   Seva
//  *****************************************************************************************
int hp_3458_GetMaxSamplesNumber(void)
{
    return HP3458_MAX_NUM_SAMPLES;
}
  
//
//  ***************************************************************************************** 
//                    --- hp3458.c::hp3458_set_voltmeter ---
//
//!\brief   Set range and voltage averaging time
//!\param  int
//!\param  double
//!\param  double
//!\retval void
//!\author Seva
//  ***************************************************************************************** 
void hp3458_set_voltmeter(int dev_ID, double v_range, double num_plc)
{
    int dev_open = -1;
    int err = 0;
    int spoll = 0;
    
    int i;
    char buf[HP3458_MAX_CMD];

    // Check input parameters 
    if (dev_ID < 1 || dev_ID > HP3458_MAX_NUM_DEV)
    {
        Fmt(buf, "%s<dev_ID = %i, out of range", dev_ID);
<<<<<<< HEAD
        printf("%s %s\n", module_name,buf);
=======
        stop_error_msg(module_name,buf);
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Exit if testing without hardware 
    #ifdef DUMMY_DEVICES
        return;
    #endif
    
    // Check to make sure the device is open 
    dev_open = hp3458_check_dev_open(dev_ID);
    if (dev_open != 1)
    {
<<<<<<< HEAD
        printf("%s Device not open\n", module_name);
=======
        stop_error_msg(module_name,"Device not open");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
        return;
    }
    
    // Preset the HP3458 
	//hp3458_out(dev_ID, "PRESET");
   
    // Turn off autorange 
	//hp3458_out(dev_ID, "ARANGE OFF");
    
    // Turn off the autozero 
	//hp3458_out(dev_ID, "AZERO OFF");
    
    // Turn off the display 
	//hp3458_out(dev_ID, "DISP OFF");
    
    //Turn off math functions 
	//hp3458_out(dev_ID, "MATH OFF");
    
    // Turn on the keyboard lock 
	//hp3458_out(dev_ID, "LOCK ON");
 
    // Set the output format 
    hp3458_out(dev_ID, "OFORMAT ASCII"); //?
     
    // Set the voltage range 
    Fmt(buf, "%s<DCV %f", v_range);
    hp3458_out(dev_ID, buf);
    
    // Set the A/D integration time 
    Fmt(buf, "%s<NPLC %f", num_plc);
    hp3458_out(dev_ID, buf);
    

    
    // Check for errors 
    err = hp3458_get_errors(dev_ID);
    if (err != 0)
    {
		hp3458_close_dev(dev_ID); 
<<<<<<< HEAD
        printf("%s The HP3458 has an error\n", module_name);
=======
        stop_error_msg(module_name,"The HP3458 has an error");
>>>>>>> 100d0612235e902a5c4921e08a36b6d0b064d94b
    }
}

