Files
sics/site_ansto/lakeshore340driv.c
Douglas Clowes 149d825a3a remove unused variables
r2422 | dcl | 2008-04-14 09:23:13 +1000 (Mon, 14 Apr 2008) | 2 lines
2012-11-15 13:34:40 +11:00

480 lines
14 KiB
C

/*--------------------------------------------------------------------------
L A K E S H O R E 3 4 0 D R I V
This file contains the implementation of a driver for the
Lakeshore 340 Temperature controller.
Mark Koennecke, Juli 1997
Mark Lesha, January 2006 (based on ITC4 code)
Copyright:
Labor fuer Neutronenstreuung
Paul Scherrer Institut
CH-5423 Villigen-PSI
The authors hereby grant permission to use, copy, modify, distribute,
and license this software and its documentation for any purpose, provided
that existing copyright notices are retained in all copies and that this
notice is included verbatim in any distributions. No written agreement,
license, or royalty fee is required for any of the authorized uses.
Modifications to this software may be copyrighted by their authors
and need not follow the licensing terms described here, provided that
the new terms are clearly indicated on the first page of each file where
they apply.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
----------------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <fortify.h>
#include <conman.h>
#include <servlog.h>
#include <fortify.h>
typedef struct __EVDriver *pEVDriver;
#include <evdriver.i>
/* Do we need these ?
#include <sics.h>
#include <modriv.h>
*/
#include <rs232controller.h>
#include "hardsup/lakeshore340util.h"
#include "hardsup/el734_def.h"
#include "hardsup/el734fix.h"
#define SHITTYVALUE -777
/*------------------------- The Driver ------------------------------------*/
pEVDriver CreateLAKESHORE340Driver(int argc, char *argv[]);
int ConfigLAKESHORE340(pEVDriver self);
/*-----------------------------------------------------------------------*/
typedef struct {
pLAKESHORE340 pData;
char *pHost;
int iPort;
int iChannel;
int iControl; /* LAKESHORE340 control */
float fDiv;
float fMult;
int iRead; /* LAKESHORE340 sensor */
int iTmo;
int iLastError;
} LAKESHORE340Driv, *pLAKESHORE340Driv;
/*----------------------------------------------------------------------------*/
static int GetLAKESHORE340Pos(pEVDriver self, float *fPos)
{
pLAKESHORE340Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLAKESHORE340Driv)self->pPrivate;
assert(pMe);
iRet = LAKESHORE340_Read(&pMe->pData,fPos);
if(iRet <= 0 )
{
pMe->iLastError = iRet;
return 0;
}
if( (*fPos < 0) || (*fPos > 10000) )
{
*fPos = -999.;
pMe->iLastError = SHITTYVALUE;
return 0;
}
return 1;
}
/*----------------------------------------------------------------------------*/
static int LAKESHORE340Run(pEVDriver self, float fVal)
{
pLAKESHORE340Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
iRet = LAKESHORE340_Set(&pMe->pData,fVal);
if(iRet != 1)
{
pMe->iLastError = iRet;
return 0;
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LAKESHORE340Error(pEVDriver self, int *iCode, char *error, int iErrLen)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv)self->pPrivate;
assert(pMe);
*iCode = pMe->iLastError;
if(pMe->iLastError == SHITTYVALUE)
{
strncpy(error,"Invalid temperature returned form LAKESHORE340, check sensor",iErrLen);
}
else
{
LAKESHORE340_ErrorTxt(&pMe->pData,pMe->iLastError,error,iErrLen);
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LAKESHORE340Send(pEVDriver self, char *pCommand, char *pReply, int iLen)
{
pLAKESHORE340Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
iRet = LAKESHORE340_Send(&pMe->pData,pCommand, pReply,iLen);
if(iRet <= 0)
{
pMe->iLastError = iRet;
return 0;
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LAKESHORE340Init(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
pMe->pData = NULL;
iRet = LAKESHORE340_Open(&pMe->pData, pMe->pHost, pMe->iRead, pMe->iControl,0);
if(iRet != 1)
{
if(iRet == LAKESHORE340__NOLAKESHORE340)
{
return -1;
}
else
{
pMe->iLastError = iRet;
return 0;
}
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LAKESHORE340Close(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
LAKESHORE340_Close(&pMe->pData);
return 1;
}
/*---------------------------------------------------------------------------*/
static int LAKESHORE340Fix(pEVDriver self, int iError)
{
pLAKESHORE340Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
switch(iError)
{
/* network errors */
case EL734__BAD_FLUSH:
case EL734__BAD_RECV:
case EL734__BAD_RECV_NET:
case EL734__BAD_RECV_UNKN:
case EL734__BAD_RECVLEN:
case EL734__BAD_RECV1:
case EL734__BAD_RECV1_PIPE:
case EL734__BAD_RNG:
case EL734__BAD_SEND:
case EL734__BAD_SEND_PIPE:
case EL734__BAD_SEND_NET:
case EL734__BAD_SEND_UNKN:
case EL734__BAD_SENDLEN:
LAKESHORE340Close(self);
iRet = LAKESHORE340Init(self);
if(iRet)
{
return DEVREDO;
}
else
{
return DEVFAULT;
}
break;
/* handable protocoll errors */
case EL734__BAD_TMO:
return DEVREDO;
break;
case -501: /* Bad_COM */
return DEVREDO;
case -504: /* Badly formatted */
return DEVREDO;
default:
return DEVFAULT;
break;
}
return DEVFAULT;
}
/*--------------------------------------------------------------------------*/
/* static int LAKESHORE340Halt(pEVDriver *self)
{
assert(self);
return 1;
}
*/
/*------------------------------------------------------------------------*/
void KillLAKESHORE340(void *pData)
{
pLAKESHORE340Driv pMe = NULL;
pMe = (pLAKESHORE340Driv)pData;
assert(pMe);
if(pMe->pHost)
{
free(pMe->pHost);
}
free(pMe);
}
/*------------------------------------------------------------------------*/
pEVDriver CreateLAKESHORE340Driver(int argc, char *argv[])
{
pEVDriver pNew = NULL;
pLAKESHORE340Driv pSim = NULL;
/* check for arguments */
if(argc < 3)
{
return NULL;
}
pNew = CreateEVDriver(argc,argv);
pSim = (pLAKESHORE340Driv)malloc(sizeof(LAKESHORE340Driv));
memset(pSim,0,sizeof(LAKESHORE340Driv));
if(!pNew || !pSim)
{
return NULL;
}
pNew->pPrivate = pSim;
pNew->KillPrivate = KillLAKESHORE340;
/* initalise pLAKESHORE340Driver */
pSim->iControl = atoi(argv[2]);
pSim->iRead = atoi(argv[1]);
pSim->iLastError = 0;
pSim->iTmo = 10;
/* The LAKESHORE340 doesn't require divisors or multipliers
and they are always forced to 1.0 */
pSim->fDiv = 1.0;
pSim->fMult = 1.0;
pSim->pHost = strdup(argv[0]);
pSim->iPort = 0;
pSim->iChannel = 0;
/* initialise function pointers */
pNew->SetValue = LAKESHORE340Run;
pNew->GetValue = GetLAKESHORE340Pos;
pNew->Send = LAKESHORE340Send;
pNew->GetError = LAKESHORE340Error;
pNew->TryFixIt = LAKESHORE340Fix;
pNew->Init = LAKESHORE340Init;
pNew->Close = LAKESHORE340Close;
return pNew;
}
/*--------------------------------------------------------------------------*/
int ConfigLAKESHORE340(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
iRet = LAKESHORE340_Config(&pMe->pData, pMe->iTmo, pMe->iRead,
pMe->iControl,pMe->fDiv,pMe->fMult);
if(iRet < 0)
{
pMe->iLastError = iRet;
return 0;
}
return 1;
}
/*-------------------------------------------------------------------------*/
int SetSensorLAKESHORE340(pEVDriver self, int iSensor)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
/* The LAKESHORE340 allows bath temp, external temp and tank temp to be read
so allow iRead=1 to 4 for A,B,C,D respectively. */
if( (iSensor < 1) || (iSensor > 4) )
{
return 0;
}
pMe->iRead = iSensor;
pMe->pData->iRead = iSensor;
return 1;
}
/*-------------------------------------------------------------------------*/
int SetControlLAKESHORE340(pEVDriver self, int iSensor)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
/* For the LAKESHORE340 allow selection of internal or external control
where iControl==1 to 4 for A,B,C,D respectively. */
if( (iSensor < 1) || (iSensor > 4) )
{
return 0;
}
pMe->iControl = iSensor;
pMe->pData->iControl = iSensor;
return 1;
}
/*-------------------------------------------------------------------------*/
int SetTMOLAKESHORE340(pEVDriver self, int iSensor)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
if(iSensor < 10)
{
return 0;
}
pMe->iTmo = iSensor;
return 1;
}
/*-------------------------------------------------------------------------*/
int GetControlLAKESHORE340(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
return pMe->iControl;
}
/*-------------------------------------------------------------------------*/
int GetSensorLAKESHORE340(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
return pMe->iRead;
}
/*-------------------------------------------------------------------------*/
int GetTMOLAKESHORE340(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
return pMe->iTmo;
}
/*-------------------------------------------------------------------------*/
float GetDivisorLAKESHORE340(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
return pMe->fDiv; /* but forced to 1.0 for LAKESHORE340, not used */
}
/*--------------------------------------------------------------------------*/
int SetDivisorLAKESHORE340(pEVDriver self, float fDiv)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
/* The LAKESHORE340 doesn't need divisor, force to 1.0 */
pMe->fDiv = 1.0; /* fDiv */;
return 1;
}
/*-------------------------------------------------------------------------*/
float GetMultLAKESHORE340(pEVDriver self)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
return pMe->fMult; /* but forced to 1.0 for LAKESHORE340, not used */
}
/*--------------------------------------------------------------------------*/
int SetMultLAKESHORE340(pEVDriver self, float fDiv)
{
pLAKESHORE340Driv pMe = NULL;
assert(self);
pMe = (pLAKESHORE340Driv )self->pPrivate;
assert(pMe);
/* The LAKESHORE340 doesn't need multiplier, force to 1.0 */
pMe->fMult = 1.0; /* fDiv */;
return 1;
}