Newer
Older
// ============================================================================
//
// = CONTEXT
// TANGO Project - SCPI KeithleySCPIProtocol Support Library
//
// = FILENAME
// KeithleySCPIProtocol.cpp
//
// = AUTHOR
// X. Elattaoui
//
// ============================================================================
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <iostream>
#include <sstream>
#include <string>
//#include "TangoGpibLink.h"
// ============================================================================
// KeithleySCPIProtocol::KeithleySCPIProtocol
// ============================================================================
KeithleySCPIProtocol::KeithleySCPIProtocol (std::string& gpib_device_name)
: ElectrometerProtocol(),
{
std::cout << "KeithleySCPIProtocol::KeithleySCPIProtocol <-" << std::endl;
std::cout << "KeithleySCPIProtocol::KeithleySCPIProtocol ->" << std::endl;
}
// ============================================================================
// KeithleySCPIProtocol::~KeithleySCPIProtocol
// ============================================================================
KeithleySCPIProtocol::~KeithleySCPIProtocol (void)
{
std::cout << "KeithleySCPIProtocol::~KeithleySCPIProtocol <-" << std::endl;
if(_communication_link)
{
delete _communication_link;
_communication_link = 0;
}
std::cout << "KeithleySCPIProtocol::~KeithleySCPIProtocol ->" << std::endl;
// ============================================================================
// KeithleySCPIProtocol::build_communicationLink
// ============================================================================
bool KeithleySCPIProtocol::build_communicationLink()
{
if (_commDevName.empty())
return false;
try
{
_communication_link = new TangoGpibLink (_commDevName);
}
catch(...)
{
if( _communication_link )
{
delete _communication_link;
_communication_link = 0;
}
return false;
}
try
{
//- configure tthe SCPI Keithley device
std::string cmd_to_send("");
//- Select reading only
cmd_to_send = "FORM:ELEM READ";
_communication_link->write(cmd_to_send);
//- Select control source : IMMediate
cmd_to_send = "ARM:SOUR IMM";
_communication_link->write(cmd_to_send);
//- Set measure count
cmd_to_send = "ARM:COUNT 1";
_communication_link->write(cmd_to_send);
}
catch(...)
{
if( _communication_link )
{
delete _communication_link;
_communication_link = 0;
}
return false;
}
// ============================================================================
// KeithleySCPIProtocol::set_range
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_range()");
}
//- get electrometer mode
tmpMode = get_mode( );
//- erase bad caracters
tmpMode.erase(tmpMode.find("\n") );
//- send command to Keithley device
cmd_to_send = tmpMode + ":RANGe " + value ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::zero_check_on
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::zero_check_on()");
}
//- send command
cmd_to_send = "SYST:ZCH ON" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::zero_check_off
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::zero_check_off()");
}
//- send command
cmd_to_send = "SYST:ZCH OFF";
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::zero_correct_on
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::zero_correct_on()");
}
//- send command
cmd_to_send = "SYST:ZCOR ON" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::zero_correct_off
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::zero_correct_off()");
}
//- send command
cmd_to_send = "SYST:ZCOR OFF" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::zero_correct_state_on
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::zero_correct_state_on()");
}
//- send command
cmd_to_send = "SYST:ZCOR:STAT ON" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::zero_correct_state_off
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::zero_correct_state_off()");
}
//- send command
cmd_to_send = "SYST:ZCOR:STAT OFF" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::autoRange_ON
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::autoRange_ON()");
}
//- get electrometer mode
tmpMode = get_mode( );
//- erase bad caracters
tmpMode.erase(tmpMode.find("\n") );
cmd_to_send = tmpMode + ":RANGe:AUTO ON" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::autoRange_OFF
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::autoRange_off()");
}
//- get electrometer mode
tmpMode = get_mode( );
//- erase bad caracters
tmpMode.erase(tmpMode.find("\n") );
//- send command
cmd_to_send = tmpMode + ":RANGe:AUTO OFF";
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::auto_zero_on
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::auto_zero_on()");
}
//- send command
cmd_to_send = "SYST:AZER ON" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::auto_zero_off
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::auto_zero_off()");
}
//- send command
cmd_to_send = "SYST:AZER OFF" ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::MODE -> Ampere, Volt, Ohm, Coulomb and V on I meters
// ============================================================================
// ============================================================================
// KeithleySCPIProtocol::setAmperMeterMode
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setAmperMeterMode()");
}
_communication_link->write(cmd_to_send.str());
}
// ============================================================================
// KeithleySCPIProtocol::setVoltMeterMode
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setVoltMeterMode()");
}
_communication_link->write(cmd_to_send.str());
}
// ============================================================================
// KeithleySCPIProtocol::setOhmMeterMode
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setOhmMeterMode()");
}
_communication_link->write(cmd_to_send.str());
}
// ============================================================================
// KeithleySCPIProtocol::setCoulombMeterMode
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setCoulombMeterMode()");
}
// ============================================================================
// KeithleySCPIProtocol::setVSourceOutputON
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setVSourceOutputON()");
}
}
// ============================================================================
// KeithleySCPIProtocol::setVSourceOutputOFF
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setVSourceOutputOFF()");
}
// ============================================================================
// KeithleySCPIProtocol::setVSourceValue
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::setVSourceValue()");
}
std::stringstream cmd_to_send;
//- send command
cmd_to_send << "SOUR:VOLT " << voltsValue << std::endl;
_communication_link->write(cmd_to_send.str());
}
// ============================================================================
// KeithleySCPIProtocol::getVSourceValue
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::getVSourceValue()");
}
std::string cmd_to_send("SOUR:VOLT?");
//- send command
return _communication_link->write_read(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_mode
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_mode()");
}
//- get electrometer mode
if(isDiffSuportedMode)
{
//- send command
cmd_to_send = "FUNC?" ;
_mode = _communication_link->write_read(cmd_to_send);
//- _mode returned is as this "RES" so, erase " caracters
_mode.erase(_mode.find("\""), 1);
_mode.erase(_mode.find("\""), 1);
}
else
_mode = "CURR\n";
return _mode;
}
// ============================================================================
// KeithleySCPIProtocol::get_range
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_range()");
}
//- send command
cmd_to_send = tmpMode + ":RANGe?" ;
std::string _rangeStr = _communication_link->write_read(cmd_to_send);
return _rangeStr;
}
// ============================================================================
// KeithleySCPIProtocol::get_value
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_value()");
}
////- This command performs a CONFIGURE and a READ?
//cmd_to_send = "MEAS?" ;
tmp = _communication_link->write_read(cmd_to_send);
//- extract all data
return tmp;
}
// ============================================================================
// KeithleySCPIProtocol::get_integratedValue
// ============================================================================
std::vector<double> KeithleySCPIProtocol::get_integratedValue (void)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_integratedValue()");
}
//- This command performs an INIT and then query the bufferised values
cmd_to_send = "TRAC:DATA?" ;
tmp = _communication_link->write_read(cmd_to_send);
//- extract all data
return buildDataList(tmp);
}
// ============================================================================
// KeithleySCPIProtocol::get_fetchValue
// ============================================================================
std::vector<double> KeithleySCPIProtocol::get_fetchValue (void)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_fetchValue()");
}
tmp = _communication_link->write_read(cmd_to_send);
//- extract all data
return buildDataList(tmp);
}
// ============================================================================
// KeithleySCPIProtocol::buildDataList
//
// This method extract and convert data from the received string
// ============================================================================
std::vector<double> KeithleySCPIProtocol::buildDataList(std::string datalist)
{
std::string dataStr("");
std::vector<double> argout;
std::string::size_type posBeg;
std::string::size_type posEnd;
std::cout << "\t*****::buildDataList -> argin :\n$" << datalist << "$" << std::endl;
if(posEnd == std::string::npos)
{
argout.push_back( XString<double>::convertFromString(datalist) );
std::cout << "\t*****::buildDataList -> JUST ONE DATA :$" << datalist << "$" << std::endl;
}
else
{
int i = 0;
//std::string tmp = get_triggercount();
short numberOfData = XString<short>::convertFromString(get_triggercount());
for(i=0; i < numberOfData; i++)
{
dataStr = datalist.substr(posBeg,posEnd);
argout.push_back( XString<double>::convertFromString(dataStr) );
std::cout << "\t*****::buildDataList -> FULL OF DATA : " << i << " -> $" << argout[i] << "$" << std::endl;
posBeg = posEnd+1;
posEnd = datalist.find(',', posBeg);
//- end of string reached so latest data is here
if(posEnd == std::string::npos)
{
dataStr = datalist.substr(posBeg);
argout.push_back( XString<double>::convertFromString(dataStr) );
break;
}
}
}
return argout;
}
// ============================================================================
// KeithleySCPIProtocol::init_keithley to perform reading(s) data
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::init_keithley()");
}
#ifdef WIN32
Sleep( 10 ); //- sleep just a few
#else
usleep(10000);
#endif
//- send command : INIT to trigg readings !
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::abort -> cancel all operations started by INIT command
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::abort()");
}
std::string cmd_to_send("ABORt");
//- send command : ABORt to cancel all operations
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::set_knplc : specify the integration rate
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_knplc()");
}
//- send command
cmd_to_send = tmpMode + ":NPLC " + nbNPLC ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_knplc : get the integration rate
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_knplc()");
}
//- send command
cmd_to_send = tmpMode + ":NPLCycles?" ;
return _communication_link->write_read(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::set_triggercount
// ============================================================================
void KeithleySCPIProtocol::set_triggercount (std::string nbTrigCount)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_triggercount()");
}
std::string cmd_to_send("");
//- send command
cmd_to_send = "TRIG:COUN " + nbTrigCount ;
//- for internal use ( = keithley device buffer size)
trigCountStr = nbTrigCount;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_triggercount
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_triggercount()");
}
std::string cmd_to_send("TRIG:COUNt?");
std::string response = _communication_link->write_read(cmd_to_send);
return response;
}
// ============================================================================
// KeithleySCPIProtocol::set_triggerdelay
// ============================================================================
void KeithleySCPIProtocol::set_triggerdelay (std::string trigDelay)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_triggerdelay()");
}
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_triggerdelay
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_triggerdelay()");
}
return _communication_link->write_read(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::set_triggerdelayAuto
// ============================================================================
void KeithleySCPIProtocol::set_triggerdelayAuto (std::string trigDelayAuto)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_triggerdelayAuto()");
}
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_triggerdelayAuto
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_triggerdelayAuto()");
}
return _communication_link->write_read(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::set_averagecount
// ============================================================================
void KeithleySCPIProtocol::set_averagecount (std::string nbAverageCount)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_averagecount()");
}
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::set_averagecount
// ============================================================================
void KeithleySCPIProtocol::set_averagecount_K6517 (std::string nbAverageCount)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::set_averagecount()");
}
std::string cmd_to_send = tmpMode + ":AVER:COUN " + nbAverageCount ;
_communication_link->write(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_averagecount
// ============================================================================
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_averagecount()");
}
return _communication_link->write_read(cmd_to_send);
}
// ============================================================================
// KeithleySCPIProtocol::get_averagecount
// ============================================================================
std::string KeithleySCPIProtocol::get_averagecount_K6517 (void)
if( !this->_communication_link )
{
throw electrometer::ElectrometerException(
"No communication protocol available.",
"KeithleySCPIProtocol::get_averagecount()");
}