/* This example demonstrates the use of some * NI-VXI Word Serial functions. Word Serial functions * provide a method for communicating with message based * VXI instruments. * The function Word Serial Write - WSwrt() - allows * the user to send a message to an instrument. The function * Word Serial Read - WSrd() - allows the user to read a * response back from the instrument. Refer to Chapter * Four entitled Commander Word Serial Protocol Functions * of the NI-VXI Function Reference Manual for a detailed * description of these functions. */ #include "nivxi.h" #include #include main() { INT16 logical_address = 0; /* the logical address of the device*/ char send_buffer[6] = "*idn?"; /* the command we will send */ UINT32 send_count = 5; /* max number of bytes to send*/ UINT16 mode = 3; /* mode of transfer */ /* * mode is a value ranging from 0 to three. It is * a bit vector consisting of two bits. * When bit 0 == 0 abort the transfer if the device is not DIR * == 1 poll until the device is DIR. * * When bit 1 == 0 set the END bit at the end of transfer * == 1 clear END bit at end of transfer * * In this example, we'll use mode == 3: keep polling until * the device is ready, and set the END bit when we're done. * */ UINT32 ret_count; /* the number of bytes actually sent */ INT16 ret; /* return status vector (error codes) */ UINT8 rec_buffer[1024]; /* buffer for data returned from instrument */ printf("\nYou can run this example with VICtext. If it is not already running,"); printf("\nyou can quit this program, launch VICtext, then run this program again."); printf("\nBefore sending *idn?, run the ws.log script file in VICtext by typing"); printf("\n'$ \\ws.log' at the prompt. This will allow VICtext to act like"); printf("\na message based device that we can communicate with. VICtext"); printf("\nwill then wait for the wswrt command, which you can send from the"); printf("this example program by pressing any key when prompted."); ret = InitVXIlibrary(); /* Always open the VXI library * at the beginning*/ if (ret < 0) /* check for errors */ { /* If we get here, there was an error opening the library. */ return -1; } printf("\nPress a Key to send the string '*idn?'"); while (!kbhit()); _getch(); /* Send the command '*idn?' to logical address 0. */ ret = WSwrt(logical_address, send_buffer, send_count, mode, &ret_count); if (ret & 0x8000) /* check bit 15 to see if an error occured */ { /* If we get here, WSwrt encountered an error. * Close and exit the program */ printf("\nWord Serial Write did not succeed!"); CloseVXIlibrary(); return -1; } printf("\nPress a Key to read response"); while (!kbhit()); _getch(); /* Clear input buffer */ /* Read the response back from logical address 0 */ send_count = 1024; /* read up to 1024 bytes */ mode = 1; /* With Word Serial Read, the definition of mode changes somewhat. * mode is 15 bit bit vector now. * Refer to the software reference manual for the function of each bit * position. In this example, we'll use mode == 1, which tells * us to poll the target instrument until it is ready to send * data, and terminate the transfer when the end bit is set. */ printf("\nSending %s to the instrument\n", send_buffer); ret = WSrd(logical_address, rec_buffer, send_count, mode, &ret_count); if (ret & 0x8000) /* check bit 15 to see if an error occured */ { /* If we get here, WSrd encountered an error. * Close and exit the program */ CloseVXIlibrary(); return -1; } printf("Response from instrument:\n %s\n", rec_buffer); ret = CloseVXIlibrary(); printf("\nPress any key to exit..."); while (!kbhit()); }