Files
sics/site_ansto/lh45driv.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

479 lines
13 KiB
C

/*---------------------------------------------------------------------
L H 4 5 D R I V
This file contains the implementation of a driver for the Julabo
LH45 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/lh45util.h"
#include "hardsup/el734_def.h"
#include "hardsup/el734fix.h"
#define SHITTYVALUE -777
/*------------------------- The Driver ------------------------------------*/
pEVDriver CreateLH45Driver(int argc, char *argv[]);
int ConfigLH45(pEVDriver self);
/*-----------------------------------------------------------------------*/
typedef struct {
pLH45 pData;
char *pHost;
int iPort;
int iChannel;
int iControl; /* LH45 control */
float fDiv;
float fMult;
int iRead; /* LH45 sensor */
int iTmo;
int iLastError;
} LH45Driv, *pLH45Driv;
/*----------------------------------------------------------------------------*/
static int GetLH45Pos(pEVDriver self, float *fPos)
{
pLH45Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLH45Driv)self->pPrivate;
assert(pMe);
iRet = LH45_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 LH45Run(pEVDriver self, float fVal)
{
pLH45Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
iRet = LH45_Set(&pMe->pData,fVal);
if(iRet != 1)
{
pMe->iLastError = iRet;
return 0;
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LH45Error(pEVDriver self, int *iCode, char *error, int iErrLen)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv)self->pPrivate;
assert(pMe);
*iCode = pMe->iLastError;
if(pMe->iLastError == SHITTYVALUE)
{
strncpy(error,"Invalid temperature returned form LH45, check sensor",iErrLen);
}
else
{
LH45_ErrorTxt(&pMe->pData,pMe->iLastError,error,iErrLen);
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LH45Send(pEVDriver self, char *pCommand, char *pReply, int iLen)
{
pLH45Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
iRet = LH45_Send(&pMe->pData,pCommand, pReply,iLen);
if(iRet <= 0)
{
pMe->iLastError = iRet;
return 0;
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LH45Init(pEVDriver self)
{
pLH45Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
pMe->pData = NULL;
iRet = LH45_Open(&pMe->pData, pMe->pHost, pMe->iRead, pMe->iControl,0);
if(iRet != 1)
{
if(iRet == LH45__NOLH45)
{
return -1;
}
else
{
pMe->iLastError = iRet;
return 0;
}
}
return 1;
}
/*--------------------------------------------------------------------------*/
static int LH45Close(pEVDriver self)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
LH45_Close(&pMe->pData);
return 1;
}
/*---------------------------------------------------------------------------*/
static int LH45Fix(pEVDriver self, int iError)
{
pLH45Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLH45Driv )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:
LH45Close(self);
iRet = LH45Init(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 LH45Halt(pEVDriver *self)
// {
// assert(self);
//
// return 1;
// }
/*------------------------------------------------------------------------*/
void KillLH45(void *pData)
{
pLH45Driv pMe = NULL;
pMe = (pLH45Driv)pData;
assert(pMe);
if(pMe->pHost)
{
free(pMe->pHost);
}
free(pMe);
}
/*------------------------------------------------------------------------*/
pEVDriver CreateLH45Driver(int argc, char *argv[])
{
pEVDriver pNew = NULL;
pLH45Driv pSim = NULL;
/* check for arguments */
if(argc < 3)
{
return NULL;
}
pNew = CreateEVDriver(argc,argv);
pSim = (pLH45Driv)malloc(sizeof(LH45Driv));
memset(pSim,0,sizeof(LH45Driv));
if(!pNew || !pSim)
{
return NULL;
}
pNew->pPrivate = pSim;
pNew->KillPrivate = KillLH45;
/* initalise pLH45Driver */
pSim->iControl = atoi(argv[2]);
pSim->iRead = atoi(argv[1]);
pSim->iLastError = 0;
pSim->iTmo = 10;
/* The LH45 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 = LH45Run;
pNew->GetValue = GetLH45Pos;
pNew->Send = LH45Send;
pNew->GetError = LH45Error;
pNew->TryFixIt = LH45Fix;
pNew->Init = LH45Init;
pNew->Close = LH45Close;
return pNew;
}
/*--------------------------------------------------------------------------*/
int ConfigLH45(pEVDriver self)
{
pLH45Driv pMe = NULL;
int iRet;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
iRet = LH45_Config(&pMe->pData, pMe->iTmo, pMe->iRead,
pMe->iControl,pMe->fDiv,pMe->fMult);
if(iRet < 0)
{
pMe->iLastError = iRet;
return 0;
}
return 1;
}
/*-------------------------------------------------------------------------*/
int SetSensorLH45(pEVDriver self, int iSensor)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
/* The LH45 allows bath temp, external temp and tank temp to be read
so allow iRead=1 to 3 for each of these respectively */
if( (iSensor < 1) || (iSensor > 3) )
{
return 0;
}
pMe->iRead = iSensor;
pMe->pData->iRead = iSensor;
return 1;
}
/*-------------------------------------------------------------------------*/
int SetControlLH45(pEVDriver self, int iSensor)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
/* For the LH45 allow selection of internal or external control
where iControl==1 or 2 respectively */
if( (iSensor < 1) || (iSensor > 2) )
{
return 0;
}
pMe->iControl = iSensor;
pMe->pData->iControl = iSensor;
return 1;
}
/*-------------------------------------------------------------------------*/
int SetTMOLH45(pEVDriver self, int iSensor)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
if(iSensor < 10)
{
return 0;
}
pMe->iTmo = iSensor;
return 1;
}
/*-------------------------------------------------------------------------*/
int GetControlLH45(pEVDriver self)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
return pMe->iControl;
}
/*-------------------------------------------------------------------------*/
int GetSensorLH45(pEVDriver self)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
return pMe->iRead;
}
/*-------------------------------------------------------------------------*/
int GetTMOLH45(pEVDriver self)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
return pMe->iTmo;
}
/*-------------------------------------------------------------------------*/
float GetDivisorLH45(pEVDriver self)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
return pMe->fDiv; /* but forced to 1.0 for LH45, not used */
}
/*--------------------------------------------------------------------------*/
int SetDivisorLH45(pEVDriver self, float fDiv)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
/* The LH45 doesn't need divisor, force to 1.0 */
pMe->fDiv = 1.0; /* fDiv */;
return 1;
}
/*-------------------------------------------------------------------------*/
float GetMultLH45(pEVDriver self)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
return pMe->fMult; /* but forced to 1.0 for LH45, not used */
}
/*--------------------------------------------------------------------------*/
int SetMultLH45(pEVDriver self, float fDiv)
{
pLH45Driv pMe = NULL;
assert(self);
pMe = (pLH45Driv )self->pPrivate;
assert(pMe);
/* The LH45 doesn't need multiplier, force to 1.0 */
pMe->fMult = 1.0; /* fDiv */;
return 1;
}