|
Sample for GPIB-1, PCIGPIB-1, USB-GPIB-1
|
Sample Source for Visual-C/C++
| |
// gpib.cpp : Sample project for the GPIB-1, PCIGPIB and USB GPIB Modules // // Author: Michael Reimer, QUANCOM Informationssysteme GmbH, Germany // // Website: http://www.quancom.de // Product: // GPIB PCI Controller http://www.quancom.de/qprod01/eng/pb/pcigpib_1.htm // GPIB ISA Controller http://www.quancom.de/qprod01/eng/pb/GPIB_1.htm // GPIB USB Controller http://www.quancom.de/qprod01/eng/pb/usb_gpib_1.htm // Information: // // To use the QLIB Commands in your source, do the following: // // (1) Add statement #include "qlib.h" to your source file. // (2) Add in the Dialog Menu->Project->Settings->C/C++->Preprocessor // "$(QLIB_INC)" to the additional include directories entry. // (3) Add in the Dialog Menu->Project->Settings->Linker->General // "$(QLIB_LIB)\qlib32.lib" to the additional library and object // modules directories entry.
#include "stdafx.h" #include "windows.h" #include "qlib.h"
int main(int argc, char* argv[]) { ULONG result; ULONG listener; ULONG talker;
char buffer[1024];
// The following sequence tries to find the // Bus Type ( PCI, ISA or USB ) of the // installed GPIB Controller ULONG handle = QAPIExtOpenCard(PCIGPIB,0);
if ( handle == NULL ) { handle = QAPIExtOpenCard(USBGPIB,0); if ( handle == NULL ) { handle = QAPIExtOpenCard(GPIB,0); } }
// The handle is != NULL if there is a GPIB Controller installed if ( handle == NULL ) { MessageBox(NULL, "No QUANCOM GPIB Controller found!", "Error", MB_OK); return FALSE; }
// Ok, we found a QUANCOM GPIB Controller Card
// Now we can send a string to the listner with address 3 // Change the address to the appropriate address for your // device ( normally set by DIP-Switches on the back side )
// --------------------------------------------------------------------------- // PART 1a: Writing a string to the DMM ( The DMM is a the listener ) // // Listener: A device capable of receiving data over the interface // when addressed to Listen by the active controller. Examples of such // devices are printers, programmable power supplies, or any other // programmable instrument. There can be up to 14 Listeners on the GPIB Bus // at one time. // ---------------------------------------------------------------------------
// Select the listener address, which is set by a DIP-Switch on the // back side of your instrument.
listener = 3;
// Send "z" to the listner 3, which resets the DMM to the initial settings.
char s1[] = "z";
if ( QAPIExtWriteString(handle, listener, (char*)&s1, strlen(s1),0)) { printf("Writing to device %u was successful\n", listener); } else { printf("Writing to device %u failed\n", listener); }
// --------------------------------------------------------------------------- // PART 1b: Reading a DMM ( The DMM is a talker ) // // Talker: A device capable of transmitting data over the interface when // addressed to talk by the active controller. Examples of such devices // are voltmeters, data-acquisition systems, or any other programmable // instrument. There can be only one addressed talker on the GPIB Bus at one // time. // --------------------------------------------------------------------------- // Select the talker address, which is set by a DIP-Switch on the // back side of your instrument.
talker = 3;
// Read value from DMM with talker address 3
result = QAPIExtReadString(handle, talker, (char*)&buffer, sizeof(buffer), 0); if (result) { printf("Reading from device %u was: %s\n",talker, buffer); } else { printf("Reading from device %u failed\n", talker); }
// --------------------------------------------------------------------------- // PART2: Checking whether a device has requested service ( SRQ Service Request) // // A device can interrupt the active controller by asserting the SRQ line. The // SRQ is a single line, and if there are multiple devices on the GPIB Bus that // have been configured to assert an SRQ, the active controller will have // to "poll" the devices to figure out which one actually asserted the SRQ. // More than one device could in principle assert an SRQ at the same time. The // active controller can poll the devices in one of two ways: serial poll // or parallel poll. // ---------------------------------------------------------------------------
result = QAPIExtSpecial(handle, JOB_READSRQ, 0, 0); if (result) { printf("SRQ was asserted\n"); } else { printf("SRQ was not asserted\n"); }
// --------------------------------------------------------------------------- // PART3: Reading Serial Poll Status from DMM // // In a serial poll, the active controller asks each device in turn to // send back a status byte that indicates whether the device has asserted // the SRQ. Bit 6 of this byte (where the bits are numbered 0 through 7) is set // if the device is requesting service. The definition of the other bits // is device dependent (under 488.1 at least; 488.2 provides a much more // concise definition of the status byte). // // The program has to perform this same sequence with every device // it needs to poll. // ---------------------------------------------------------------------------
// We poll all devices from 1 to 15 here. This is normally not nescessary and // very time consuming. Poll only valid devices on the GPIB Bus.
ULONG serial_poll_byte = 0;
for ( ULONG device=1;device<15; device++ ) { result = QAPIExtSpecial(handle, JOB_SERIALPOLL, device, (ULONG)&serial_poll_byte);
if ( result ) { if (serial_poll_byte & 0x40) // check for bit 6 = device requested service { printf("Device %u requested service and returned status byte %u\n", device, serial_poll_byte); } else { printf("Device %u no SRQ requested. Status is %u \n", device, serial_poll_byte); } } else { printf("No answer from device %u\n", device); }
}
// --------------------------------------------------------------------------- // PART4: Send a command to DMM // // The following commands are accepted by all devices on the GPIB Bus // simultaneously. The address part will be ignored. // // - JOB_DCL (Device Clear): The DCL command causes all devices to return to a device // dependent initial state. // // - JOB_LLO (Local Lockout): The LLO command disables the return-to-local front // panel key on the device. The user can no longer change the device settings // from its front panel. // // The following commands need an address and are only accepted by addressed // devices. Whether the devices are the listeners or the talkers depends on the // command. The three commands are as follows: // // - JOB_GET (Group Execute Trigger): The GET command tells all the addressed // listeners to perform some device-dependent function, like take a measurement. // GET allows for synchronizing a measurement function between multiple devices. // This is only used in specialized cases. // // - JOB_SDC (Selected Device Clear): The SDC command resets the addressed listeners // to a device-dependent state. It performs the same function as DCL, // but only resets the addressed listeners, not all the devices. // // - JOB_GTL (Go To Local): The GTL command sets the addressed listeners back to // local mode. // ---------------------------------------------------------------------------
// Send DCL to all devices
result = QAPIExtSpecial(handle, JOB_DCL, 1, NULL);
if ( !result ) { printf("Command DCL failed\n"); }
// Send LLO to all devices
result = QAPIExtSpecial(handle, JOB_LLO, 1, NULL);
if ( !result ) { printf("Command LLO failed\n"); }
// Send GET to device 3
result = QAPIExtSpecial(handle, JOB_GET, 3, NULL);
if ( !result ) { printf("Command GET failed\n"); }
// Send SDC to device 3
result = QAPIExtSpecial(handle, JOB_SDC, 3, NULL);
if ( !result ) { printf("Command SDC failed\n"); }
// Send GTL to device 3
result = QAPIExtSpecial(handle, JOB_GTL, 3, NULL);
if ( !result ) { printf("Command GTL failed\n"); }
QAPIExtCloseCard(handle);
return 0; }
|
|
|