Example #1
0
void *gpibPort::portOpen(void)
{
    int ret;
    int gpibAddress;
    int sta;

    gpibAddress = atoi(deviceString);

    handle = ibdev( BDINDEX, gpibAddress, NO_SECONDARY_ADDR, T1s, 1, 0 );
    if ( ibsta & ERR )
    {
        GPIBCleanup( (char*)"Unable to open device" );
        exit( -1 );
    }

    sta = ibconfig( BDINDEX, IbcAUTOPOLL, 0 ); // enable auto serial polling
    if ( sta & ERR )
    {
        GPIBCleanup( (char*)"Problem setting Autopoll" );
        exit( -1 );
    }

    if (eosMode)
    {
        ibeos( handle, eosMode);
    }

    if (eotMode)
    {
        ibeot( handle, eotMode);
    }

    ibtmo( handle, timeout);

    portService();

    Dev = &handle;

    return Dev;
}
int main( int argc, char *argv[] )
{
	int board = 0;
	int eos_mode = 0;
	char *file_path;
	int status;
	int retval;
	FILE *filep;
	struct stat file_stats;
	uint8_t *buffer;
	unsigned long buffer_length;

	if( argc < 2 )
	{
		fprintf( stderr, "Must provide file path as arguement\n" );
		return -1;
	}

	file_path = argv[ 1 ];
	filep = fopen( file_path, "r" );
	if( filep == NULL )
	{
		perror( "fopen()");
		return -1;
	}

	retval = fstat( fileno( filep ), &file_stats );
	if( retval < 0 )
	{
		perror( "fstat()");
		return -1;
	}
	buffer_length = file_stats.st_size;

	buffer = malloc( buffer_length );
	if( buffer == NULL )
	{
		perror( "malloc()");
		return -1;
	}

	if( fread( buffer, 1, buffer_length, filep ) != buffer_length )
	{
		perror( "fread()" );
		return -1;
	}

	fclose( filep );

	status = ibeos( board, eos_mode );
	if( status & ERR )
	{
		fprintf( stderr, "ibeos() failed\n" );
		fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
		return -1;
	}

	status = ibtmo( board, TNONE );
	if( status & ERR )
	{
		fprintf( stderr, "ibtmo() failed\n" );
		fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
		return -1;
	}

	status = ibconfig( board, IbcTIMING, T1_DELAY_350ns );
	if( status & ERR )
	{
		fprintf( stderr, "ibconfig() failed\n" );
		fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
		return -1;
	}

	status = ibwait( board, TACS );
	if( ( status & TACS ) == 0 )
	{
		fprintf( stderr, "ibwait() for TACS failed\n" );
		fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
		return -1;
	}

	status = ibwrt( board, buffer, buffer_length );
	if( status & ERR )
	{
		fprintf( stderr, "ibwrt() failed\n" );
		fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
		return -1;
	}

	free( buffer );

	return 0;
}
Example #3
0
bool CGPIBDevice::ScanDevice(CArray<IO_ADDRESS> &addresses)
{
	IO_ADDRESS addrnode;
	addresses.RemoveAll();
	int res;

	if (m_hGPIBDLL == NULL && !LoadGPIBDriver())
	{
		return false;
	}

	short primary_addrs[MAX_GPIB_PRIM_ADDR];
	short addr[MAX_ADDRESSES];

	for (int nAddr = 0; nAddr < MAX_ADDRESSES; nAddr++)
	{
		addr[nAddr] = (short)0xFFFF;
	}

	for (int nAddr = 0; nAddr < (MAX_GPIB_PRIM_ADDR - 2); nAddr++)
	{
		primary_addrs[nAddr] = (short)(nAddr + 1);
	}

	primary_addrs[MAX_GPIB_PRIM_ADDR - 2] = (short)0xFFFF;

	Log(_T("GPIB device scanning start!"));

	for (int nBus = 0; nBus < MAX_GPIB_CARD; nBus++)
	{
		ibonl(nBus, 1); if (ibsta & ERR) continue;
		ibconfig(nBus, IbcSC, 1); if (ibsta & ERR) continue;
		ibsic(nBus); if (ibsta & ERR) continue;
		ibconfig(nBus, IbcSRE, 1); if (ibsta & ERR) continue;
		ibconfig(nBus, IbcTIMING, 1); if (ibsta & ERR) continue;
		ibask(nBus, IbaPAD, &res); if (ibsta & ERR) continue;
		ibask(nBus, IbaSAD, &res); if (ibsta & ERR) continue;

		FindLstn(nBus, primary_addrs, addr, MAX_ADDRESSES - 1);

		if (ibsta & ERR) continue;

		for (int i = 0; i < (MAX_ADDRESSES - 1); i++)
		{
			if (addr[i] == -1) break;

			memset(&addrnode, 0, sizeof(IO_ADDRESS));

			addrnode.nBus = nBus;
			addrnode.eType = DRV_GPIB;
			addrnode.nPrimAddr = (addr[i] & 0xFF);
			addrnode.nSecAddr = ((addr[i] & 0xFF00) >> 8);

			addrnode.bPrimary = (i == 0 || addresses[i - 1].nPrimAddr != addrnode.nPrimAddr);

			if (addrnode.bPrimary && !Open(nBus, addrnode.nPrimAddr, addrnode.nSecAddr)) continue;

			if (!addrnode.bPrimary)
				strcpy_s(addrnode.sDevIdentity, addresses[i - 1].sDevIdentity);
			else
			{
				int nReadLen = MAX_DEV_IDN_LEN;
				CString strIDN;

				if (!Query("*IDN?", nReadLen, addrnode.sDevIdentity, 1000))
				{
					Log(CCommon::FormatString(_T("Failed to query identity of  device at GPIB::%d::%d::%d"), nBus, addrnode.nPrimAddr, addrnode.nSecAddr));

					Close();
					continue;
				}

				Close();
			}

			addresses.Add(addrnode);
		}
	}

	if (addresses.GetCount() == 0)
	{
		Log(_T("Found no GPIB device!"));
		return false;
	}

	return true;
}