Commit 2cb75ffc authored by Graziano Scalamera's avatar Graziano Scalamera
Browse files

Implement communication with the device

parent 6cb08d42
......@@ -118,6 +118,7 @@ void SFE::delete_device()
/*----- PROTECTED REGION ID(SFE::delete_device) ENABLED START -----*/
// Delete device allocated objects
delete dp;
/*----- PROTECTED REGION END -----*/ // SFE::delete_device
delete[] attr_PMTvoltage_read;
......@@ -148,6 +149,28 @@ void SFE::init_device()
/*----- PROTECTED REGION ID(SFE::init_device) ENABLED START -----*/
// Initialize device
dp = new Tango::DeviceProxy(device);
dp_state=Tango::FAULT;
try
{
Tango::DeviceAttribute dattrin = dp->read_attribute("State");
dattrin >> dp_state;
if(dp_state == Tango::ON)
{
set_state(Tango::ON);
set_status("On");
}
else
{
set_state(Tango::FAULT);
set_status("Communication Error");
}
}
catch(Tango::DevFailed &e)
{
set_state(Tango::FAULT);
set_status("Communication Error");
}
/*----- PROTECTED REGION END -----*/ // SFE::init_device
}
......@@ -216,6 +239,46 @@ void SFE::always_executed_hook()
/*----- PROTECTED REGION ID(SFE::always_executed_hook) ENABLED START -----*/
// code always executed before all requests
Tango::DevState old_dp_state = dp_state;
try
{
Tango::DeviceAttribute dattrin = dp->read_attribute("State");
dattrin >> dp_state;
if(dp_state == Tango::ON)
{
set_state(Tango::ON);
set_status("On");
}
else
{
set_state(Tango::FAULT);
set_status("Communication Error");
}
}
catch(Tango::DevFailed &e)
{
set_state(Tango::FAULT);
set_status("Communication Error");
dp_state = Tango::FAULT;
}
if(old_dp_state == Tango::FAULT && dp_state == Tango::ON)
{
try
{
Tango::MultiAttribute *attr_list = get_device_attr();
Tango::WAttribute &attr = attr_list->get_w_attr_by_name ("PMTVoltage");
Tango::DevDouble w_val;
attr.get_write_value(w_val);
stringstream cmd;
cmd << "SV:" << w_val;
string resp;
SendReceive(cmd.str(), resp);
}
catch(Tango::DevFailed &e)
{
INFO_STREAM << __func__<<": error initializing PMTVoltage after reconnection: " << e.errors[0].desc;
}
}
/*----- PROTECTED REGION END -----*/ // SFE::always_executed_hook
}
......@@ -264,6 +327,8 @@ void SFE::read_PMTvoltage(Tango::Attribute &attr)
{
DEBUG_STREAM << "SFE::read_PMTvoltage(Tango::Attribute &attr) entering... " << endl;
/*----- PROTECTED REGION ID(SFE::read_PMTvoltage) ENABLED START -----*/
string cmd("RV?");
SendReceive(cmd, (double *)attr_PMTvoltage_read);
// Set the attribute value
attr.set_value(attr_PMTvoltage_read);
......@@ -285,8 +350,22 @@ void SFE::write_PMTvoltage(Tango::WAttribute &attr)
Tango::DevDouble w_val;
attr.get_write_value(w_val);
/*----- PROTECTED REGION ID(SFE::write_PMTvoltage) ENABLED START -----*/
stringstream cmd;
cmd << "SV:" << w_val;
string resp;
SendReceive(cmd.str(), resp);
if(resp != "OK")
{
stringstream err;
err << "Error setting command '" << cmd.str() << "': '" << resp << "'";
INFO_STREAM << __func__ << ": " << err.str();
Tango::Except::throw_exception(
(const char *) "Setting Error",
err.str(),
(const char *)__FUNCTION__,Tango::ERR);
}
/*----- PROTECTED REGION END -----*/ // SFE::write_PMTvoltage
}
//--------------------------------------------------------
......@@ -302,6 +381,8 @@ void SFE::read_Temperature(Tango::Attribute &attr)
{
DEBUG_STREAM << "SFE::read_Temperature(Tango::Attribute &attr) entering... " << endl;
/*----- PROTECTED REGION ID(SFE::read_Temperature) ENABLED START -----*/
string cmd("RT?");
SendReceive(cmd, attr_Temperature_read);
// Set the attribute value
attr.set_value(attr_Temperature_read);
......@@ -343,6 +424,105 @@ void SFE::add_dynamic_commands()
/*----- PROTECTED REGION ID(SFE::namespace_ending) ENABLED START -----*/
// Additional Methods
void SFE::SendReceive(const string & command, string & response)
{
if(dp_state == Tango::FAULT)
{
Tango::Except::throw_exception(
(const char *) "Communication Error",
(const char *) "Communication Error",
(const char *)__FUNCTION__,Tango::ERR);
}
char cmd[64];
memset(cmd,0,64);
sprintf(cmd, "%s",command.c_str());
Tango::DevLong input_length=0;
try
{
Tango::DevVarCharArray dvca;
dvca.length(strlen(cmd)+1); //for \r
for (unsigned int i=0; i<strlen(cmd); ++i)
dvca[i] = cmd[i];
dvca[strlen(cmd)] = '\r';
Tango::DeviceData din;
din << dvca;
dp->command_inout("Write", din);
DEBUG_STREAM << __func__<<": sent command: " << cmd;
string resp;
do
{
Tango::DeviceData din2, dout;
const Tango::DevVarCharArray *dtr;
Tango::DevVarCharArray *delim = new Tango::DevVarCharArray();
delim->length(1);
(*delim)[0] = '\n';
din2 << delim;
dout = dp->command_inout("ReadUntil", din2);
dout >> dtr;
stringstream tmp;
for (unsigned int i=0; i<dtr->length(); ++i)
{
resp += (*dtr)[i];
tmp << hex<<setw(2)<<setfill('0')<< (int)(*dtr)[i];
if(i != dtr->length()-1)
tmp << " ";
}
tmp << dec;
resp.erase(resp.find_last_not_of("\n\r")+1);
DEBUG_STREAM << __func__ << ": received response='" << resp << "'";
DEBUG_STREAM << __func__ << ": -> '" << tmp.str() << "'" << endl;
Tango::DeviceAttribute dattrin = dp->read_attribute("InputLength");
dattrin >> input_length;
if(input_length > 0)
DEBUG_STREAM << __func__<<": still " << input_length << " bytes in buffer";
} while (input_length > 0);
response = resp;
}
catch(Tango::DevFailed &e)
{
stringstream err;
err << "Error sending command '" << command << "': '" << e.errors[0].desc << "'";
INFO_STREAM << __func__ << ": " << err.str();
response = string("");
set_state(Tango::FAULT);
set_status("Communication Error");
Tango::Except::re_throw_exception(e,
(const char *) "Communication Error",
err.str(),
(const char *)__FUNCTION__,Tango::ERR);
}
catch(...)
{
INFO_STREAM << __func__<<": NON Tango Exception sending command";
}
set_state(Tango::ON);
set_status("On");
}
void SFE::SendReceive(const string & command, Tango::DevLong * val)
{
string response;
SendReceive(command, response);
stringstream tmp;
tmp << response;
tmp >> *val;
}
void SFE::SendReceive(const string & command, Tango::DevDouble * val)
{
string response;
SendReceive(command, response);
stringstream tmp;
tmp << response;
tmp >> *val;
}
/*----- PROTECTED REGION END -----*/ // SFE::namespace_ending
} // namespace
......@@ -60,6 +60,8 @@ class SFE : public TANGO_BASE_CLASS
/*----- PROTECTED REGION ID(SFE::Data Members) ENABLED START -----*/
// Add your own data members
Tango::DeviceProxy *dp;
Tango::DevState dp_state;
/*----- PROTECTED REGION END -----*/ // SFE::Data Members
......@@ -187,6 +189,9 @@ public:
/*----- PROTECTED REGION ID(SFE::Additional Method prototypes) ENABLED START -----*/
// Additional Method prototypes
void SendReceive(const string & command, string & response);
void SendReceive(const string & command, Tango::DevLong * val);
void SendReceive(const string & command, Tango::DevDouble * val);
/*----- PROTECTED REGION END -----*/ // SFE::Additional Method prototypes
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment