// Add appropriate include files.  These are appropriate for
// LabWindows/CVI
#include <utility.h>
#include <gpib.h>
#include <stdio.h>

// Set Major and Minor Primary Addresses
#define PA1 0x01
#define PA2 0x04

void main ()
{
	int board;
	char buffer[100];
	char byte;
	int address;
	
	board = ibfind ("GPIB0");
	ibrsc (board, 0);
	
	// Determine Address of GPIB board
	// ibdiag will read some information from the table in memory
	// bits 27 and 26 combine to make up the base address of I/O
	// mapped boards (ie, not PCI boards which are memory mapped)
	ibdiag(board, buffer, 100);  
	address = (((buffer[27] << 8) & 0xFF00) | (buffer[26] & 0x00FF));

	outp (address + 0x8, 0x31);	   		// ADR: Set Dual Addressing
	outp (address + 0xC, PA1);			// ARD1: Primary PA2
	outp (address + 0xC, PA2 | 0x80);	// ADR2: Secondary PA3
	
	while (1)	  // Loop Foreve
	{
		ibwait(board, 0);		 // Read Status
		if ((ibsta & 0x04) && !(ibsta & 0x10))  // LACS + !ATN
		{
			byte = inp (address + 0x08);		// Read Major/Minor #
			if (byte & 0x1)						// If bit 0 is set, it is the Minor #
				printf("Primary Address %d is a Listener\n", PA2);
			else
				printf("Primary Address %d is a Listener\n", PA1);
		}
		if ((ibsta & 0x08) && !(ibsta & 0x10))	// TACS + !ATN
		{
			byte = inp (address + 0x08);		// Read Major/Minor #
			if (byte & 0x1)
				printf("Primary Address %d is a Talker\n", PA2);
			else
				printf("Primary Address %d is a Talker\n", PA1);
		}
		
			
	}
}
