//*******************************************************************************
//* Copyright (c) 2008-2014 Synchrotron SOLEIL
//* All rights reserved. This program and the accompanying materials
//* are made available under the terms of the GNU Lesser Public License v3
//* which accompanies this distribution, and is available at
//* http://www.gnu.org/licenses/lgpl.html
//******************************************************************************
static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/Instrumentation/AcquireWaveformLecroy/src/AcquireWaveformLecroy.cpp,v 1.15 2012-03-09 10:22:12 xavela Exp $";
//+=============================================================================
//
// file :         AcquireWaveformLecroy.cpp
//
// description :  C++ source for the AcquireWaveformLecroy and its commands.
//                The class is derived from Device. It represents the
//                CORBA servant object which will be accessed from the
//                network. All commands which can be executed on the
//                AcquireWaveformLecroy are implemented in this file.
//
// project :      TANGO Device Server
//
// $Author: xavela $
//
// $Revision: 1.15 $
//
// $Log: not supported by cvs2svn $
// Revision 1.14  2012/02/28 13:28:25  xavela
// WaveForm.cpp : ptr which receives data, allocated statically !
//
// Revision 1.13  2010/03/26 09:11:31  vince_soleil
// "Migration_Tango7_Part2"
//
// Revision 1.12  2008/04/24 14:59:23  xavela
// xavier :
// - SocketLecroy file splitted for Win32 and Linux like platforms
// - Bugs fixed in SocketLecroy
//
// Revision 1.11  2008/04/24 13:08:44  syldup
// no message
//
// Revision 1.10  2008/02/29 15:00:12  xavela
// xavier:
// sleep added after a write
//
// Revision 1.9  2008/02/28 09:02:05  xavela
// xavier :
// StateMachine file added !! (oups)
//
// Revision 1.7  2004/10/22 13:24:47  buteau
// Second code reread. Minor modifications
//
// Revision 1.6  2004/10/18 13:17:49  xavela
// xavier : add an attr waveArray2.
// Sorts attr in expert and operator view with Pogo.
// Inconsistencies removed.
//
// Revision 1.5  2004/10/18 09:01:40  buteau
// First code reread
//
// Revision 1.4  2004/10/07 09:21:18  xavela
// xavier : no cout now.
//
// Revision 1.3  2004/10/05 13:11:11  xavela
// Xavier : previous commit failed.
//
// Revision 1.2  2004/10/05 13:09:10  xavela
// Xavier : memory allocation in read_attr_hardware() method.
// calculation of offset of the begining of the struct, which contains the context of the waveform acquisition, in the received block data.
//
// Revision 1.1.1.1  2004/09/29 15:42:14  syldup
// Initial import.
//
//
// copyleft :   Synchrotron SOLEIL
//      L'Orme des Merisiers
//      Saint-Aubin - BP 48

//
//-=============================================================================
//
//      This file is generated by POGO
//  (Program Obviously used to Generate tango Object)
//
//         (c) - Software Engineering Group - ESRF
//=============================================================================



//===================================================================
//
//	The following table gives the correspondence
//	between commands and method name.
//
//  Command name|  Method name
//	----------------------------------------
//  State      |  dev_state()
//  Status     |  dev_status()
//  WriteRead  |  write_read()
//
//===================================================================


#include <AcquireWaveformLecroy.h>
#include <AcquireWaveformLecroyClass.h>
#include <yat/time/Timer.h>

static const int MAX_RESPONSE_LENGTH = 150000;
static const int MAX_STRING_LENGTH   = 256;

namespace AcquireWaveformLecroy_ns
{

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::AcquireWaveformLecroy(string &s)
//
// description :  constructor for simulated AcquireWaveformLecroy
//
// in : - cl : Pointer to the DeviceClass object
//      - s : Device name
//
//-----------------------------------------------------------------------------
AcquireWaveformLecroy::AcquireWaveformLecroy(Tango::DeviceClass *cl,string &s)
  :Tango::Device_4Impl(cl,s.c_str())
{
  init_device();
}

AcquireWaveformLecroy::AcquireWaveformLecroy(Tango::DeviceClass *cl,const char *s)
  :Tango::Device_4Impl(cl,s)
{
  init_device();
}

AcquireWaveformLecroy::AcquireWaveformLecroy(Tango::DeviceClass *cl,const char *s,const char *d)
  :Tango::Device_4Impl(cl,s,d)
{
  init_device();
}
//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::delete_device()
//
// description :  will be called at device destruction or at init command.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::delete_device()
{
  //- Delete device's allocated object
  if (attr_rawWaveformData_read) {
   delete [] attr_rawWaveformData_read;
   attr_rawWaveformData_read = 0;
  }
  if (attr_verticalScaledData_read) {
   delete [] attr_verticalScaledData_read;
   attr_verticalScaledData_read = 0;
  }
  if (attr_waveArray1_read) {
    delete attr_waveArray1_read;
    attr_waveArray1_read = 0;
  }
  if (attr_waveArray2_read) {
    delete attr_waveArray2_read;
    attr_waveArray2_read = 0;
  }
  if (attr_waveArrayCount_read) {
    delete attr_waveArrayCount_read;
    attr_waveArrayCount_read = 0;
  }
  if (attr_nominalBits_read) {
    delete attr_nominalBits_read;
    attr_nominalBits_read = 0;
  }
  if (attr_horizontalInterval_read) {
    delete attr_horizontalInterval_read;
    attr_horizontalInterval_read = 0;
  }
  if (attr_horizontalOffset_read) {
    delete attr_horizontalOffset_read;
    attr_horizontalOffset_read = 0;
  }
  if (attr_verticalGain_read) {
    delete attr_verticalGain_read;
    attr_verticalGain_read = 0;
  }
  if (attr_verticalOffset_read) {
    delete attr_verticalOffset_read;
    attr_verticalOffset_read = 0;
  }
  if (attr_triggerTime_read) {
    delete attr_triggerTime_read;
    attr_triggerTime_read = 0;
  }

  //- close the socket
  ptr_com->TCP_Disconnect();

  //- delete the SocketLecroy obj
  SocketLecroy::delete_instance(ptr_com);

  //- delete the Waveform_Data obj
  if(waveform_ptr)
  {
    delete waveform_ptr;
    waveform_ptr = 0;
  }
  if(_deviceResponse)
  {
    delete [] _deviceResponse;
    _deviceResponse = 0;
  }

  DEBUG_STREAM << "Remove the InnerAppender." << endl;
  yat4tango::InnerAppender::release(this);

  DEBUG_STREAM << "Remove the YatLogAdapter." << endl;
  yat4tango::YatLogAdapter::release();
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::init_device()
//
// description :  will be called at device initialization.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::init_device()
{
  INFO_STREAM << "AcquireWaveformLecroy::AcquireWaveformLecroy() create device " << device_name << endl;

  //- create read attributes
  //--------------------------------------------

  //- The memory allocation is done in read_attr_hardware( ) method,
  //-   because the length of the received data block is not known here
  attr_rawWaveformData_read = 0;

  //- The memory allocation is done in read_attr_hardware( ) method,
  //-   because the length of the received data block is not known here
  attr_verticalScaledData_read = 0;

  attr_waveArray1_read = new Tango::DevLong;
  *attr_waveArray1_read = 0;

  attr_waveArray2_read = new Tango::DevLong;
  *attr_waveArray2_read = 0;

  attr_waveArrayCount_read = new Tango::DevLong;
  *attr_waveArrayCount_read = 0;

  attr_nominalBits_read = new Tango::DevShort;
  *attr_nominalBits_read = 0;

  attr_horizontalInterval_read = new Tango::DevDouble;
  *attr_horizontalInterval_read = 0.;

  attr_horizontalOffset_read = new Tango::DevDouble;
  *attr_horizontalOffset_read = 0.;

  attr_verticalGain_read = new Tango::DevDouble;
  *attr_verticalGain_read = 0.;

  attr_verticalOffset_read = new Tango::DevDouble;
  *attr_verticalOffset_read = 0.;

  attr_triggerTime_read = new Tango::DevString;
  *attr_triggerTime_read = new char[MAX_STRING_LENGTH];
  ::memset(*attr_triggerTime_read, 0, MAX_STRING_LENGTH * sizeof(char));


  //- Initialise variables to default values
  //--------------------------------------------
  ptr_com     = 0;
  waveform_ptr= 0;
  _deviceResponse = new char[MAX_RESPONSE_LENGTH];
  data_value  = 0;
  data_length = 0;
  _is_communication_opened = false;

  get_device_property();

   //---------------------------------------------------------
  //- instanciate the log_adapter & inner_appender in order to manage logs
  //---------------------------------------------------------
  try
  {
      DEBUG_STREAM << "Create the YatLogAdapter in order to use logs from third party library." << endl;
      yat4tango::YatLogAdapter::initialize(this);

      DEBUG_STREAM << "Create the InnerAppender in order to manage logs." << endl;
      yat4tango::InnerAppender::initialize(this, 512);
  }
  catch (Tango::DevFailed& df)
  {
      ERROR_STREAM << df << endl;
      set_state(Tango::FAULT);
      set_status("OUT OF MEMORY : Failed to create log manager !");
      return;
  }

 //- create the com obj
  ptr_com = SocketLecroy::get_instance();

  if ( !ptr_com )
  {
    set_state(Tango::FAULT);
    set_status("OUT OF MEMORY : communication link cannot be created !");
    return;
  }

  //- create the waveform obj
  waveform_ptr = new WaveForm_data(channelName);
  if ( !waveform_ptr )
  {
    set_state(Tango::FAULT);
    set_status("OUT OF MEMORY : objects cannot be created !");
  }
  else
  {
    set_state(Tango::ALARM);
    set_status("Communication is not yet opened.");
  }
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::get_device_property()
//
// description :  Read the device properties from database.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::get_device_property()
{
  //- Initialize your default values here.
  //------------------------------------------
  iPaddress   = "172.16.35.1";
  channelName = "C2";

  //- Read device properties from database.(Automatic code generation)
  //-------------------------------------------------------------
	Tango::DbData	dev_prop;
	dev_prop.push_back(Tango::DbDatum("IPaddress"));
	dev_prop.push_back(Tango::DbDatum("ChannelName"));

	//	Call database and extract values
	//--------------------------------------------
	if (Tango::Util::instance()->_UseDb==true)
		get_db_device()->get_property(dev_prop);
	Tango::DbDatum	def_prop, cl_prop;
	AcquireWaveformLecroyClass	*ds_class =
		(static_cast<AcquireWaveformLecroyClass *>(get_device_class()));
	int	i = -1;

	//	Try to initialize IPaddress from class property
	cl_prop = ds_class->get_class_property(dev_prop[++i].name);
	if (cl_prop.is_empty()==false)	cl_prop  >>  iPaddress;
	else {
		//	Try to initialize IPaddress from default device value
		def_prop = ds_class->get_default_device_property(dev_prop[i].name);
		if (def_prop.is_empty()==false)	def_prop  >>  iPaddress;
	}
	//	And try to extract IPaddress value from database
	if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  iPaddress;

	//	Try to initialize ChannelName from class property
	cl_prop = ds_class->get_class_property(dev_prop[++i].name);
	if (cl_prop.is_empty()==false)	cl_prop  >>  channelName;
	else {
		//	Try to initialize ChannelName from default device value
		def_prop = ds_class->get_default_device_property(dev_prop[i].name);
		if (def_prop.is_empty()==false)	def_prop  >>  channelName;
	}
	//	And try to extract ChannelName value from database
	if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  channelName;

  //- End of Automatic code generation
  //-------------------------------------------------------------
  Tango::DbData data_put;
  if (dev_prop[0].is_empty())
  {
    Tango::DbDatum  propert("IPaddress");
    propert <<  iPaddress;
    data_put.push_back(propert);
  }
  if (dev_prop[1].is_empty())
  {
    Tango::DbDatum  propert("ChannelName");
    propert <<  channelName;
    data_put.push_back(propert);
  }

  //  End of Automatic code generation
  //------------------------------------------------------------------
  if( !data_put.empty() )
    get_db_device()->put_property(data_put);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::always_executed_hook()
//
// description :  method always executed before any command is executed
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::always_executed_hook()
{
  //- init communication
  if ( !_is_communication_opened && ptr_com )
  {
    try
    {
      ptr_com->TCP_Connect( (char*)iPaddress.c_str() );
      _is_communication_opened = true;
      set_state(Tango::OPEN);
      set_status("The communication is OK.");
    }
    catch(const lecroy::SocketException & se)
    {
      _is_communication_opened = false;
      set_state(Tango::FAULT);
      set_status("The communication is not well opened.");
      Tango::DevFailed df = lecroy_to_tango_exception(se);
      FATAL_STREAM << df << std::endl;
      Tango::Except::re_throw_exception(df,
                                         "TCP_CONNECTION_FAILED",
                                         "Cannot built a socket connection.",
                                         "AcquireWaveformLecroy::init_device()",
                                        Tango::PANIC
                                       );
    }
  }
}

//+----------------------------------------------------------------------------
//
// method :   AcquireWaveformLecroy::read_attr_hardware()
//
// description :  Hardware acquisition for attributes.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_attr_hardware(vector<long> &attr_list)
{
  //- DEBUG_STREAM << "In read_attr_hardware for " << attr_list.size();
  //- DEBUG_STREAM << " attribute(s)" << endl;
yat::Timer t;
    DEBUG_STREAM << "\nREAD attr HW ENTERING ..." << std::endl;

  //- Get the waveform data and description( ADC resolution, horizontal and vertical infos, data length ...)
  try
  {
    // First do a real read on the hardware
    //
    waveform_ptr->get_waveform_data();
    set_state(Tango::ON);
    set_status("Waveform data acquired.");
    INFO_STREAM << "\nTimes infos to get waveform data :\n" << waveform_ptr->get_times_to_get_waveformData() << std::endl;
  }
  catch(const lecroy::WaveformException &we)
  {
    set_state(Tango::ALARM);
    set_status("Failed to acquire waveform.");
    Tango::DevFailed df = lecroy_to_tango_exception(we);
    FATAL_STREAM << df << std::endl;
    Tango::Except::re_throw_exception(df,
                                       "COMMUNICATION_BROKEN",
                                       "Cannot call get_waveform_data.",
                                       "AcquireWaveformLecroy::always_executed_hook()",
                                      Tango::ERR
                                     );
  }

  //- get the waveform length
  try
  {
    long previous_lgth = data_length;
    //- TODO : retirer les copies dans les attr !!!!
    data_length = waveform_ptr->get_wavedesc_descriptor()->wave_array_count;
    //- check data does not exceed the MAX defined :
    if ( data_length > MAX_WAVEFORM_DATA_LENGTH )
        data_length = MAX_WAVEFORM_DATA_LENGTH;
    if (previous_lgth <= data_length)
    {
      reallocate_spectrums(data_length);
    }
    //- get waveform data
    attr_rawWaveformData_read    = waveform_ptr->get_raw_waveform_data();
    attr_verticalScaledData_read = waveform_ptr->get_vertical_scaled_waveform_data();
  }
  catch(const lecroy::WaveformException &we)
  {
    set_state(Tango::ALARM);
    set_status("Cannot acquire the waveform data.");
    Tango::DevFailed df = lecroy_to_tango_exception(we);

    Tango::Except::re_throw_exception(df,
                                       "GET_FAILED",
                                       "Cannot acquire the total length of the waveform data.",
                                       "AcquireWaveformLecroy::read_attr_hardware()",
                                      Tango::ERR
                                     );
  }

  *attr_waveArray1_read = waveform_ptr->get_wavedesc_descriptor()->wave_array_1;

  *attr_waveArray2_read = waveform_ptr->get_wavedesc_descriptor()->wave_array_2;

  *attr_waveArrayCount_read = data_length;

  *attr_nominalBits_read = waveform_ptr->get_wavedesc_descriptor()->nominal_bits;

  *attr_horizontalInterval_read = (double) (waveform_ptr->get_wavedesc_descriptor()->horizontal_interval);

  *attr_horizontalOffset_read = waveform_ptr->get_wavedesc_descriptor()->horizontal_offset;

  *attr_verticalGain_read = (double) (waveform_ptr->get_wavedesc_descriptor()->vertical_gain);

  *attr_verticalOffset_read = waveform_ptr->get_wavedesc_descriptor()->vertical_offset;

  std::string response = waveform_ptr->get_trigger_time_value();
  strcpy(*attr_triggerTime_read, response.c_str());
  
  INFO_STREAM << "READ_HW : all data ar now updated. DONE in " << t.elapsed_msec() << " milliseconds.\n" << std::endl;
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_verticalScaledData
//
// description :  Extract real attribute values for verticalScaledData acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_verticalScaledData(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_verticalScaledData(Tango::Attribute &attr) entering... "<< endl;
  //  Add your own code here
  attr.set_value(attr_verticalScaledData_read, data_length);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_triggerTime
//
// description :  Extract real attribute values for triggerTime acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_triggerTime(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_triggerTime(Tango::Attribute &attr) entering... "<< endl;
  //  Add your own code here
  attr.set_value(attr_triggerTime_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_verticalOffset
//
// description :  Extract real attribute values for verticalOffset acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_verticalOffset(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_verticalOffset(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_verticalOffset_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_verticalGain
//
// description :  Extract real attribute values for verticalGain acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_verticalGain(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_verticalGain(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_verticalGain_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_horizontalOffset
//
// description :  Extract real attribute values for horizontalOffset acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_horizontalOffset(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_horizontalOffset(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_horizontalOffset_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_horizontalInterval
//
// description :  Extract real attribute values for horizontalInterval acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_horizontalInterval(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_horizontalInterval(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_horizontalInterval_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_nominalBits
//
// description :  Extract real attribute values for nominalBits acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_nominalBits(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_nominalBits(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_nominalBits_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_waveArrayCount
//
// description :  Extract real attribute values for waveArrayCount acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_waveArrayCount(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_waveArrayCount(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_waveArrayCount_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_waveArray2
//
// description :  Extract real attribute values for waveArray2 acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_waveArray2(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_waveArray2(Tango::Attribute &attr) entering... "<< endl;
  //  Add your own code here
  attr.set_value(attr_waveArray2_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_waveArray1
//
// description :  Extract real attribute values for waveArray1 acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_waveArray1(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_waveArray1(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_waveArray1_read);
}

//+----------------------------------------------------------------------------
//
// method :     AcquireWaveformLecroy::read_rawWaveformData
//
// description :  Extract real attribute values for rawWaveformData acquisition result.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroy::read_rawWaveformData(Tango::Attribute &attr)
{
  //- DEBUG_STREAM << "AcquireWaveformLecroy::read_rawWaveformData(Tango::Attribute &attr) entering... "<< endl;
  //- Add your own code here
  attr.set_value(attr_rawWaveformData_read, data_length);
}

//+----------------------------------------------------------------------------
//
// method : AcquireWaveformLecroy::lecroy_to_tango_exception()
//
// description :   Extract real attribute values from
//        hardware acquisition result.
//
//-----------------------------------------------------------------------------
Tango::DevFailed AcquireWaveformLecroy::lecroy_to_tango_exception(const lecroy::LecroyException& de)
{
  Tango::DevErrorList error_list(de.errors.size());
  error_list.length(de.errors.size());

  for(size_t i = 0; i < de.errors.size(); i++)
  {
    error_list[i].reason = CORBA::string_dup(de.errors[i].reason.c_str());
    error_list[i].desc   = CORBA::string_dup(de.errors[i].desc.c_str());
    error_list[i].origin = CORBA::string_dup(de.errors[i].origin.c_str());

    switch(de.errors[i].severity)
    {
    case lecroy::WARN:
      error_list[i].severity = Tango::WARN;
      break;
    case lecroy::PANIC:
      error_list[i].severity = Tango::PANIC;
      break;
    case lecroy::ERR:
    default:
      error_list[i].severity = Tango::ERR;
      break;
    }
  }

  return Tango::DevFailed(error_list);
}

//+------------------------------------------------------------------
/**
 *	method:	AcquireWaveformLecroy::write_read
 *
 *	description:	method to execute "WriteRead"
 *	Command to send a specific command to the Lecroy device
 *
 * @param	argin	command to send
 * @return	device response (if any)
 *
 */
//+------------------------------------------------------------------
Tango::DevString AcquireWaveformLecroy::write_read(Tango::DevString argin)
{
  //  POGO has generated a method core with argout allocation.
  //  If you would like to use a static reference without copying,
  //  See "TANGO Device Server Programmer's Manual"
  //    (chapter : Writing a TANGO DS / Exchanging data)
  //------------------------------------------------------------
  //- DEBUG_STREAM << "AcquireWaveformLecroy::write_read(): entering... !" << endl;

  //  Add your own code to control device here
  std::string cmd_to_send(argin);
  int bytes_received = -1;

  ptr_com->TCP_WriteDevice(argin, cmd_to_send.size(), true);

  //- check if a response is expected (must found ? character)
  if( cmd_to_send.find('?') != std::string::npos )
  {
#ifdef WIN32
    Sleep(100); //- milliseconds
#else
    usleep(100000);
#endif
    ptr_com->TCP_ReadDevice(_deviceResponse, MAX_RESPONSE_LENGTH, &bytes_received);
  }
  else
  {
    strcpy(_deviceResponse, "No response");
  }

  return _deviceResponse;
}

//- method to manage memory
void AcquireWaveformLecroy::reallocate_spectrums(long new_data_size)
{
  if (new_data_size)
  {
    //- delete previous memory
    if (attr_rawWaveformData_read)
    {
      delete[] attr_rawWaveformData_read;
      attr_rawWaveformData_read = 0;
    }
    if (attr_verticalScaledData_read)
    {
      delete[] attr_verticalScaledData_read;
      attr_verticalScaledData_read = 0;
    }
    //- allocate memory
    attr_rawWaveformData_read   = new Tango::DevShort[new_data_size];
    attr_verticalScaledData_read= new Tango::DevDouble[new_data_size];
  }
}

}	//	namespace