Skip to content
Snippets Groups Projects
TangoGpibLink.cpp 9.76 KiB
Newer Older
LE's avatar
LE committed
// ============================================================================
//
// = CONTEXT
//    TANGO Project - Keithley Electrometer Support Library
//
// = FILENAME
//    TangoGpibLink.cpp
//
// = AUTHOR
//    X. Elattaoui
//
// ============================================================================

// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <string>
#include <iostream>
#include "TangoGpibLink.h"


// ============================================================================
// TangoGpibLink::TangoGpibLink
// ============================================================================
TangoGpibLink::TangoGpibLink (std::string& gpib_device_name) 
:	CommunicationLink(gpib_device_name),
ELATTAOUI's avatar
ELATTAOUI committed
_gpib_proxy (0)
LE's avatar
LE committed
{
	std::cout << "TangoGpibLink::TangoGpibLink <-" << std::endl;
	
	response.erase();
	
	std::cout << "TangoGpibLink::TangoGpibLink ->" << std::endl;
}

// ============================================================================
// TangoGpibLink::~TangoGpibLink
// ============================================================================
TangoGpibLink::~TangoGpibLink (void)
{
	std::cout << "TangoGpibLink::~TangoGpibLink <-" << std::endl;
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(_gpib_proxy)
LE's avatar
LE committed
	{
		delete _gpib_proxy;
		_gpib_proxy = 0;
	}
	
	std::cout << "TangoGpibLink::~TangoGpibLink ->" << std::endl;
}


// ============================================================================
// TangoGpibLink::create_gpib_proxy
// ============================================================================
void TangoGpibLink::create_gpib_proxy (void)  throw (Tango::DevFailed)
{
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
LE's avatar
LE committed
	
	try
	{
		//- try
		this->_gpib_proxy = new Tango::DeviceProxy(_communication_Device_name.c_str());
LE's avatar
LE committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable to create proxy on : " + _communication_Device_name + ". Check GPIB device is up !";
LE's avatar
LE committed
		
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
LE's avatar
LE committed
			(const char*)"TangoGpibLink::create_gpib_proxy");
ELATTAOUI's avatar
ELATTAOUI committed
	}
	catch(...)
	{
		description = "Unable to create proxy on : " + _communication_Device_name + ". Check GPIB device is up !";
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::create_gpib_proxy");
	}
LE's avatar
LE committed
}


// ============================================================================
// TangoGpibLink::write
// ============================================================================
void TangoGpibLink::write (std::string command_to_send)  throw (Tango::DevFailed)
{
  Tango::DeviceData dd_in;
  Tango::DevString cmd = 0;
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
LE's avatar
LE committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
LE's avatar
LE committed
		create_gpib_proxy();
	try
	{
		cmd = new char [command_to_send.size() + 1];
		strcpy(cmd, command_to_send.c_str());
	}
	catch(Tango::DevFailed& df )
	{
		if ( cmd )
			delete [] cmd;
		description = "Failed to allocate memory for the command to write";
		Tango::Except::re_throw_exception (df,
			(const char*)"MEMORY_ALLOCATION",
			description.c_str(),
			(const char*)"TangoGpibLink::write");
	}
	catch(...)
	{
		if ( cmd )
			delete [] cmd;
		Tango::Except::throw_exception (
			(const char*)"MEMORY_ALLOCATION",
			(const char*)"Not enough memory.",
			(const char*)"TangoGpibLink::write");
	}

LE's avatar
LE committed
	try
	{
		//- try
		dd_in << cmd;
		this->_gpib_proxy->command_inout("Write",  dd_in);
		if ( cmd )
			delete [] cmd;
LE's avatar
LE committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable to write command : " + command_to_send + ". Caught DevFailed." ;
LE's avatar
LE committed
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
LE's avatar
LE committed
			(const char*)"TangoGpibLink::write");
		
	}
ELATTAOUI's avatar
ELATTAOUI committed
	catch(...)
	{
		if ( cmd )
			delete [] cmd;
		description = "Unable to write command : " + command_to_send + ". Caught [...]." ;
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::write");
	}
LE's avatar
LE committed
}


// ============================================================================
// TangoGpibLink::read
// ============================================================================
std::string TangoGpibLink::read (void) throw (Tango::DevFailed)
{
LE's avatar
LE committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
LE's avatar
LE committed
		create_gpib_proxy();
	
	try
	{
		//- try
		dd_out = this->_gpib_proxy->command_inout("Read");
		dd_out >> this->response;
LE's avatar
LE committed
	}
	catch(Tango::DevFailed& df )
	{
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
			(const char*)"Unable to perform a read operation",
			(const char*)"TangoGpibLink::read");
	}
ELATTAOUI's avatar
ELATTAOUI committed
	catch(...)
	{
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			(const char*)"Unable to perform a read operation, caught [...].",
			(const char*)"TangoGpibLink::read");
	}
LE's avatar
LE committed
	
LE's avatar
LE committed
}

// ============================================================================
// TangoGpibLink::write_read
// ============================================================================
std::string TangoGpibLink::write_read (std::string command_to_send) throw (Tango::DevFailed)
{
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
LE's avatar
LE committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
LE's avatar
LE committed
		create_gpib_proxy();
	Tango::DeviceData dd_in, dd_out;
	Tango::DevString argin = 0;
LE's avatar
LE committed
	
	try
	{
		//- try
		argin = new char [command_to_send.size() + 1];
		strcpy(argin, command_to_send.c_str());
		dd_in << argin;
		dd_out = this->_gpib_proxy->command_inout("WriteRead",  dd_in);
		dd_out >> this->response;
		if ( argin )
			delete [] argin;
LE's avatar
LE committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable to write/read command : " + command_to_send + " and read device response.";
LE's avatar
LE committed
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
ELATTAOUI's avatar
ELATTAOUI committed
			(const char*)"TangoGpibLink::write_read");		
LE's avatar
LE committed
	}
ELATTAOUI's avatar
ELATTAOUI committed
	catch(...)
	{
		if ( argin )
			delete [] argin;
		description = "Unable to write/read command : " + command_to_send + " and read device response.";
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::write_read");
	}

ELATTAOUI's avatar
ELATTAOUI committed
	return this->response;
LE's avatar
LE committed
}

ELATTAOUI's avatar
ELATTAOUI committed
// ============================================================================
ELATTAOUI's avatar
ELATTAOUI committed
// TangoGpibLink::SRQLineState
ELATTAOUI's avatar
ELATTAOUI committed
// ============================================================================
ELATTAOUI's avatar
ELATTAOUI committed
bool TangoGpibLink::SRQLineState (void) throw (Tango::DevFailed)
ELATTAOUI's avatar
ELATTAOUI committed
{
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
ELATTAOUI's avatar
ELATTAOUI committed
  bool result = false;
ELATTAOUI's avatar
ELATTAOUI committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
ELATTAOUI's avatar
ELATTAOUI committed
		create_gpib_proxy();
	
	try
	{
		//- try
		dd_out = this->_gpib_proxy->command_inout("IsSRQLineUP");
		dd_out >> result;
ELATTAOUI's avatar
ELATTAOUI committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable to get Gpib SRQ line state.";
ELATTAOUI's avatar
ELATTAOUI committed
		
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
ELATTAOUI's avatar
ELATTAOUI committed
			(const char*)"TangoGpibLink::SRQLineState");
		
	}
ELATTAOUI's avatar
ELATTAOUI committed
	catch(...)
	{
		description = "Unable to get Gpib SRQ line state.";
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::SRQLineState");
	}
ELATTAOUI's avatar
ELATTAOUI committed

	return result;
ELATTAOUI's avatar
ELATTAOUI committed
}

// ============================================================================
// TangoGpibLink::readStatusByteRegister
// ============================================================================
short TangoGpibLink::readStatusByteRegister (void) throw (Tango::DevFailed)
{
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
ELATTAOUI's avatar
ELATTAOUI committed
  short result = -1;
ELATTAOUI's avatar
ELATTAOUI committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
ELATTAOUI's avatar
ELATTAOUI committed
		create_gpib_proxy();
	
	try
	{
		//- try
		da_out = this->_gpib_proxy->read_attribute("statusByteRegister");
		da_out >> result;
ELATTAOUI's avatar
ELATTAOUI committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable to get device status byte register.";
ELATTAOUI's avatar
ELATTAOUI committed
		
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
ELATTAOUI's avatar
ELATTAOUI committed
			(const char*)"TangoGpibLink::readStatusByteRegister");
ELATTAOUI's avatar
ELATTAOUI committed
		
ELATTAOUI's avatar
ELATTAOUI committed
	}	
ELATTAOUI's avatar
ELATTAOUI committed
	catch(...)
	{
		description = "Unable to get device status byte register.";
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::readStatusByteRegister");
	}

ELATTAOUI's avatar
ELATTAOUI committed
	return result;
ELATTAOUI's avatar
ELATTAOUI committed
// ============================================================================
// TangoGpibLink::clear
// ============================================================================
void TangoGpibLink::clear (void) throw (Tango::DevFailed)
{
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
ELATTAOUI's avatar
ELATTAOUI committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
ELATTAOUI's avatar
ELATTAOUI committed
		create_gpib_proxy();
	
	try
	{
		//- try
ELATTAOUI's avatar
ELATTAOUI committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable send Clear Device command.";
ELATTAOUI's avatar
ELATTAOUI committed
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
ELATTAOUI's avatar
ELATTAOUI committed
			(const char*)"TangoGpibLink::clear");		
	}	
ELATTAOUI's avatar
ELATTAOUI committed
	catch(...)
	{
		description = "Unable send Clear Device command.";
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::clear");
	}
ELATTAOUI's avatar
ELATTAOUI committed
}

// ============================================================================
// TangoGpibLink::trigger
// ============================================================================
void TangoGpibLink::trigger (void) throw (Tango::DevFailed)
{
ELATTAOUI's avatar
ELATTAOUI committed
  std::string description("");
ELATTAOUI's avatar
ELATTAOUI committed
	
ELATTAOUI's avatar
ELATTAOUI committed
	if(!_gpib_proxy)
ELATTAOUI's avatar
ELATTAOUI committed
		create_gpib_proxy();
	
	try
	{
		//- try
		this->_gpib_proxy->command_inout("Trigger");
ELATTAOUI's avatar
ELATTAOUI committed
	}
	catch(Tango::DevFailed& df )
	{
ELATTAOUI's avatar
ELATTAOUI committed
		description = "Unable send Trigger command.";
ELATTAOUI's avatar
ELATTAOUI committed
		Tango::Except::re_throw_exception (df,
			(const char*)"COMMUNICATION_ERROR",
ELATTAOUI's avatar
ELATTAOUI committed
			description.c_str(),
ELATTAOUI's avatar
ELATTAOUI committed
			(const char*)"TangoGpibLink::trigger");
	}
	catch(...)
	{
		description = "Unable send Trigger command.";
		Tango::Except::throw_exception (
			(const char*)"COMMUNICATION_ERROR",
			description.c_str(),
			(const char*)"TangoGpibLink::trigger");
ELATTAOUI's avatar
ELATTAOUI committed
	}
}