VISAInstrument运行时环境包
项目地址:
https://github.com/cnxy/VISAInstrument
附件是NIVISA1700运行时;
VISA Example in C
To compile and run this example in Microsoft Visual Studio 2008:
Open Visual Studio.
Create a new Visual C++, Win32, Win32 Console Application project.
In the Win32 Application Wizard, click Next >. Then, check Empty project, and click Finish.
Cut-and-paste the code that follows into a file named "example.c" in the project directory.
In Visual Studio 2008, right-click the Source Files folder, choose Add > Add Existing Item..., select the example.c file, and click Add.
Edit the program to use the VISA address of your oscilloscope.
Choose Project > Properties.... In the Property Pages dialog, update these project settings:
Under Configuration Properties, Linker, Input, add "visa32.lib" to the Additional Dependencies field.
Under Configuration Properties, C/C++, Code Generation, select Multi-threaded DLL for the Runtime Library field.
Click OK to close the Property Pages dialog.
Add the include files and library files search paths:
Choose Tools > Options....
In the Options dialog, under Projects and Solutions, select VC++ Directories.
Show directories for Include files, and add the include directory (for example, Program Files (x86)\IVI Foundation\VISA\WinNT\Include).
Show directories for Library files, and add the library files directory (for example, Program Files (x86)\IVI Foundation\VISA\WinNT\lib\msc).
Click OK to close the Options dialog.
Build and run the program.
/* * Keysight VISA Example in C * ------------------------------------------------------------------ * This program illustrates a few commonly-used programming * features of your Keysight Infiniium Series oscilloscope. */ #include <stdio.h> /* For printf(). */ #include <string.h> /* For strcpy(), strcat(). */ #include <time.h> /* For clock(). */ #include <visa.h> /* Keysight VISA routines. */ #define VISA_ADDRESS "TCPIP0::141.121.237.226::hislip0::INSTR" #define IEEEBLOCK_SPACE 5000000 /* Function prototypes */ void initialize(void); /* Initialize to known state. */ void capture(void); /* Capture the waveform. */ void analyze(void); /* Analyze the captured waveform. */ void do_command(char *command); /* Send command. */ int do_command_ieeeblock(char *command); /* Command w/IEEE block. */ void do_query_string(char *query); /* Query for string. */ void do_query_number(char *query); /* Query for number. */ void do_query_numbers(char *query); /* Query for numbers. */ int do_query_ieeeblock(char *query); /* Query for IEEE byte block. */ int do_query_ieeeblock_words(char *query); /* Query for word block. */ void check_instrument_errors(); /* Check for inst errors. */ void error_handler(); /* VISA error handler. */ /* Global variables */ ViSession defaultRM, vi; /* Device session ID. */ ViStatus err; /* VISA function return value. */ char str_result[256] = {0}; /* Result from do_query_string(). */ double num_result; /* Result from do_query_number(). */ unsigned char ieeeblock_data[IEEEBLOCK_SPACE]; /* Result from do_query_ieeeblock(). */ signed short ieeeblock_data_words[IEEEBLOCK_SPACE]; /* Result from do_query_ieeeblock_words(). */ double dbl_results[10]; /* Result from do_query_numbers(). */ /* Main Program * --------------------------------------------------------------- */ void main(void) { /* Open the default resource manager session. */ err = viOpenDefaultRM(&defaultRM); if (err != VI_SUCCESS) error_handler(); /* Open the session using the oscilloscope's VISA address. */ err = viOpen(defaultRM, VISA_ADDRESS, VI_NULL, VI_NULL, &vi); if (err != VI_SUCCESS) error_handler(); /* Set the I/O timeout to fifte-en seconds. */ err = viSetAttribute(vi, VI_ATTR_TMO_VALUE, 15000); if (err != VI_SUCCESS) error_handler(); /* Clear the interface. */ err = viClear(vi); if (err != VI_SUCCESS) error_handler(); /* Initialize - start from a known state. */ initialize(); /* Capture data. */ capture(); /* Analyze the captured waveform. */ analyze(); /* Close the vi session and the resource manager session. */ viClose(vi); viClose(defaultRM); } /* Initialize the oscilloscope to a known state. * --------------------------------------------------------------- */ void initialize (void) { /* Clear status. */ do_command("*CLS"); /* Get and display the device's *IDN? string. */ do_query_string("*IDN?"); printf("Oscilloscope *IDN? string: %s\n", str_result); /* Load the default setup. */ do_command("*RST"); } /* Capture the waveform. * --------------------------------------------------------------- */ void capture (void) { int num_values; FILE *fp; /* Set probe attenuation factor. */ do_command(":CHANnel1:PROBe 1.0"); do_query_string(":CHANnel1:PROBe?"); printf("Channel 1 probe attenuation factor: %s\n", str_result); /* Use auto-scale to automatically configure oscilloscope. */ do_command(":AUToscale"); /* Set trigger mode. */ do_command(":TRIGger:MODE EDGE"); do_query_string(":TRIGger:MODE?"); printf("Trigger mode: %s\n", str_result); /* Set EDGE trigger parameters. */ do_command(":TRIGger:EDGE:SOURCe CHANnel1"); do_query_string(":TRIGger:EDGE:SOURce?"); printf("Trigger edge source: %s\n", str_result); do_command(":TRIGger:LEVel CHANnel1,-2E-3"); do_query_string(":TRIGger:LEVel? CHANnel1"); printf("Trigger level, channel 1: %s\n", str_result); do_command(":TRIGger:EDGE:SLOPe POSitive"); do_query_string(":TRIGger:EDGE:SLOPe?"); printf("Trigger edge slope: %s\n", str_result); /* Save oscilloscope setup. */ /* Read system setup. */ num_values = do_query_ieeeblock(":SYSTem:SETup?"); printf("Read setup string query (%d bytes).\n", num_values); /* Write setup string to file. */ fp = fopen ("c:\\scope\\config\\setup.stp", "wb"); num_values = fwrite(ieeeblock_data, sizeof(unsigned char), num_values, fp); fclose (fp); printf("Wrote setup string (%d bytes) to ", num_values); printf("c:\\scope\\config\\setup.stp.\n"); /* Change settings with individual commands: /* Set vertical scale and offset. */ do_command(":CHANnel1:SCALe 0.1"); do_query_string(":CHANnel1:SCALe?"); printf("Channel 1 vertical scale: %s\n", str_result); do_command(":CHANnel1:OFFSet 0.0"); do_query_string(":CHANnel1:OFFSet?"); printf("Channel 1 offset: %s\n", str_result); /* Set horizontal scale and offset. */ do_command(":TIMebase:SCALe 0.0002"); do_query_string(":TIMebase:SCALe?"); printf("Timebase scale: %s\n", str_result); do_command(":TIMebase:POSition 0.0"); do_query_string(":TIMebase:POSition?"); printf("Timebase position: %s\n", str_result); /* Set the acquisition mode. */ do_command(":ACQuire:MODE RTIMe"); do_query_string(":ACQuire:MODE?"); printf("Acquire mode: %s\n", str_result); /* Or, set up by loading a previously saved setup. */ /* Read setup string from file. */ fp = fopen ("c:\\scope\\config\\setup.stp", "rb"); num_values = fread (ieeeblock_data, sizeof(unsigned char), IEEEBLOCK_SPACE, fp); fclose (fp); printf("Read setup string (%d bytes) from file ", num_values); printf("c:\\scope\\config\\setup.stp.\n"); /* Restore setup string. */ num_values = do_command_ieeeblock(":SYSTem:SETup", num_values); printf("Restored setup string (%d bytes).\n", num_values); /* Set the desired number of waveform points, * and capture an acquisition. */ do_command(":ACQuire:POINts 32000"); do_command(":DIGitize"); } /* Analyze the captured waveform. * --------------------------------------------------------------- */ void analyze (void) { double wav_format; double acq_type; double wav_points; double avg_count; double x_increment; double x_origin; double y_increment; double y_origin; FILE *fp; int num_values; /* Number of bytes returned from instrument. */ int i; /* Make measurements. * ------------------------------------------------------------- */ do_command(":MEASure:SOURce CHANnel1"); do_query_string(":MEASure:SOURce?"); printf("Measure source: %s\n", str_result); do_command(":MEASure:FREQuency"); do_query_number(":MEASure:FREQuency?"); printf("Frequency: %.4f kHz\n", num_result / 1000); do_command(":MEASure:VAMPlitude"); do_query_number(":MEASure:VAMPlitude?"); printf("Vertical amplitude: %.2f V\n", num_result); /* Download the screen image. * ------------------------------------------------------------- */ /* Read screen image. */ num_values = do_query_ieeeblock(":DISPlay:DATA? PNG"); printf("Screen image bytes: %d\n", num_values); /* Write screen image bytes to file. */ fp = fopen ("c:\\scope\\data\\screen.png", "wb"); num_values = fwrite(ieeeblock_data, sizeof(unsigned char), num_values, fp); fclose (fp); printf("Wrote screen image (%d bytes) to ", num_values); printf("c:\\scope\\data\\screen.bmp.\n"); /* Download waveform data. * ------------------------------------------------------------- */ /* Get the waveform type. */ do_query_string(":WAVeform:TYPE?"); printf("Waveform type: %s\n", str_result); /* Get the number of waveform points. */ do_query_string(":WAVeform:POINts?"); printf("Waveform points: %s\n", str_result); /* Set the waveform source. */ do_command(":WAVeform:SOURce CHANnel1"); do_query_string(":WAVeform:SOURce?"); printf("Waveform source: %s\n", str_result); /* Choose the format of the data returned: */ do_command(":WAVeform:FORMat WORD"); do_query_string(":WAVeform:FORMat?"); printf("Waveform format: %s\n", str_result); /* Display the waveform settings: */ do_query_number(":WAVeform:XINCrement?"); x_increment = num_result; printf("Waveform X increment: %e\n", x_increment); do_query_number(":WAVeform:XORigin?"); x_origin = num_result; printf("Waveform X origin: %e\n", x_origin); do_query_number(":WAVeform:YINCrement?"); y_increment = num_result; printf("Waveform Y increment: %e\n", y_increment); do_query_number(":WAVeform:YORigin?"); y_origin = num_result; printf("Waveform Y origin: %e\n", y_origin); /* Read waveform data. */ num_values = do_query_ieeeblock_words(":WAVeform:DATA?"); printf("Number of data values: %d\n", num_values); /* Open file for output. */ fp = fopen("c:\\scope\\data\\waveform_data.csv", "wb"); /* Output waveform data in CSV format. */ for (i = 0; i < num_values - 1; i++) { /* Write time value, voltage value. */ fprintf(fp, "%9f, %6f\n", x_origin + ((float)i * x_increment), ((float)ieeeblock_data_words[i] * y_increment) + y_origin); } /* Close output file. */ fclose(fp); printf("Waveform format WORD data written to "); printf("c:\\scope\\data\\waveform_data.csv.\n"); } /* Send a command to the instrument. * --------------------------------------------------------------- */ void do_command(command) char *command; { char message[80]; strcpy(message, command); strcat(message, "\n"); err = viPrintf(vi, message); if (err != VI_SUCCESS) error_handler(); check_instrument_errors(); } /* Command with IEEE definite-length block. * --------------------------------------------------------------- */ int do_command_ieeeblock(command, num_bytes) char *command; int num_bytes; { char message[80]; int data_length; strcpy(message, command); strcat(message, " #8%08d"); err = viPrintf(vi, message, num_bytes); if (err != VI_SUCCESS) error_handler(); err = viBufWrite(vi, ieeeblock_data, num_bytes, &data_length); if (err != VI_SUCCESS) error_handler(); check_instrument_errors(); return(data_length); } /* Query for a string result. * --------------------------------------------------------------- */ void do_query_string(query) char *query; { char message[80]; strcpy(message, query); strcat(message, "\n"); err = viPrintf(vi, message); if (err != VI_SUCCESS) error_handler(); err = viScanf(vi, "%t", str_result); if (err != VI_SUCCESS) error_handler(); check_instrument_errors(); } /* Query for a number result. * --------------------------------------------------------------- */ void do_query_number(query) char *query; { char message[80]; strcpy(message, query); strcat(message, "\n"); err = viPrintf(vi, message); if (err != VI_SUCCESS) error_handler(); err = viScanf(vi, "%lf", &num_result); if (err != VI_SUCCESS) error_handler(); check_instrument_errors(); } /* Query for numbers result. * --------------------------------------------------------------- */ void do_query_numbers(query) char *query; { char message[80]; strcpy(message, query); strcat(message, "\n"); err = viPrintf(vi, message); if (err != VI_SUCCESS) error_handler(); err = viScanf(vi, "%,10lf\n", dbl_results); if (err != VI_SUCCESS) error_handler(); check_instrument_errors(); } /* Query for an IEEE definite-length byte block result. * --------------------------------------------------------------- */ int do_query_ieeeblock(query) char *query; { char message[80]; int data_length; strcpy(message, query); strcat(message, "\n"); err = viPrintf(vi, message); if (err != VI_SUCCESS) error_handler(); data_length = IEEEBLOCK_SPACE; err = viScanf(vi, "%#b\n", &data_length, ieeeblock_data); if (err != VI_SUCCESS) error_handler(); if (data_length == IEEEBLOCK_SPACE ) { printf("IEEE block buffer full: "); printf("May not have received all data.\n"); } check_instrument_errors(); return(data_length); } /* Query for an IEEE definite-length word block result. * --------------------------------------------------------------- */ int do_query_ieeeblock_words(query) char *query; { char message[80]; int data_length; strcpy(message, query); strcat(message, "\n"); err = viPrintf(vi, message); if (err != VI_SUCCESS) error_handler(); data_length = IEEEBLOCK_SPACE; err = viScanf(vi, "%#hb\n", &data_length, ieeeblock_data_words); if (err != VI_SUCCESS) error_handler(); if (data_length == IEEEBLOCK_SPACE ) { printf("IEEE block buffer full: "); printf("May not have received all data.\n"); } check_instrument_errors(); return(data_length); } /* Check for instrument errors. * --------------------------------------------------------------- */ void check_instrument_errors() { char str_err_val[256] = {0}; char str_out[800] = ""; err = viQueryf(vi, ":SYSTem:ERRor? STRing\n", "%t", str_err_val); if (err != VI_SUCCESS) error_handler(); while(strncmp(str_err_val, "0,", 2) != 0 ) { strcat(str_out, ", "); strcat(str_out, str_err_val); err = viQueryf(vi, ":SYSTem:ERRor? STRing\n", "%t", str_err_val); if (err != VI_SUCCESS) error_handler(); } if (strcmp(str_out, "") != 0) { printf("INST Error%s\n", str_out); err = viFlush(vi, VI_READ_BUF); if (err != VI_SUCCESS) error_handler(); err = viFlush(vi, VI_WRITE_BUF); if (err != VI_SUCCESS) error_handler(); } } /* Handle VISA errors. * --------------------------------------------------------------- */ void error_handler() { char err_msg[1024] = {0}; viStatusDesc(vi, err, err_msg); printf("VISA Error: %s\n", err_msg); if (err < VI_SUCCESS) { exit(1); } }
- 点赞
- 收藏
- 关注作者
评论(0)