diff --git a/site_ansto/ansto_sctdriveadapter.c b/site_ansto/ansto_sctdriveadapter.c index 6393af38..aa55c8ab 100644 --- a/site_ansto/ansto_sctdriveadapter.c +++ b/site_ansto/ansto_sctdriveadapter.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "scriptcontext.h" /*---------------------------------------------------------------*/ typedef struct { @@ -29,6 +30,7 @@ typedef struct { pHdb write_node; pHdb read_node; SctController *c; + char* controller_name; }SctDrive, *pSctDrive; /*---------------------------------------------------------------*/ static void *SCTDRIVGetInterface(void *data, int iD){ @@ -105,8 +107,19 @@ static long SCTDRIVSetValue(void *data, SConnection *pCon, float val){ hdbValue v; self = (pSctDrive)data; - v.dataType = HIPFLOAT; - v.v.doubleValue = (double)val; + if (self->write_node->value.dataType == HIPINT) { + v.dataType = HIPINT; + v.v.intValue = round(val); + } + else if (self->write_node->value.dataType == HIPFLOAT) { + v.dataType = HIPFLOAT; + v.v.doubleValue = (double)val; + } + else { + SCPrintf(pCon, eError, "Node %s type (%d) is not INT nor FLOAT", + self->read_node->name, self->read_node->value.dataType); + return HWFault; + } SetHdbProperty(self->write_node,"writestatus", "start"); status = SetHipadabaPar(self->write_node, v, pCon); if(status == 1){ @@ -199,7 +212,13 @@ static float SCTDRIVGetValue(void *data, SConnection *pCon){ SCWrite(pCon,error, eError); return val; } - return (float)self->read_node->value.v.doubleValue; + if (self->read_node->value.dataType == HIPINT) + return (float) self->read_node->value.v.intValue; + if (self->read_node->value.dataType == HIPFLOAT) + return (float)self->read_node->value.v.doubleValue; + SCPrintf(pCon, eError, "Node %s type (%d) is not INT nor FLOAT", + self->read_node->name, self->read_node->value.dataType); + return val; } /*---------------------------------------------------------------- returns NULL on failure, a new datastructure else @@ -235,10 +254,20 @@ static int SctDriveCommand(SConnection *pCon, SicsInterp *sics, void *object, assert(self != NULL); - if (self->write_node == NULL) { + if (self->write_node == NULL || self->read_node == NULL) { SCWrite(pCon, "ERROR: defunct object", eError); return 0; } + if (argc > 1) { + SCPrintf(pCon, eLog, "SctDriveCommand for %s: %s", argv[0], argv[1]); + if (strcasecmp(argv[1], "list") == 0) { + SCPrintf(pCon, eValue, "%s.write_node = %s", argv[0], GetHipadabaPath(self->write_node)); + SCPrintf(pCon, eValue, "%s.read_node = %s", argv[0], GetHipadabaPath(self->read_node)); + SCPrintf(pCon, eValue, "%s.controller = %s", argv[0], self->controller_name); + return 1; + } + return 0; + } /* * only action: print value */ @@ -256,6 +285,9 @@ static void SctDriveKill(void *data){ if(self->pDriv != NULL){ free(self->pDriv); } + if(self->controller_name != NULL){ + free(self->controller_name); + } if(self->pDes != NULL){ DeleteDescriptor(self->pDes); } @@ -276,7 +308,7 @@ int ANSTO_SctMakeDriveAdapter(SConnection *pCon, SicsInterp *pSics, void *object int argc, char *argv[]) { pSctDrive pNew = NULL; pSICSOBJ obj = NULL; - hdbCallback *cb; + hdbCallback *cb; if(argc < 4){ SCWrite(pCon,"ERROR: not enough arguments for ANSTO_SctMakeDriveAdapter", eError); @@ -290,11 +322,22 @@ int ANSTO_SctMakeDriveAdapter(SConnection *pCon, SicsInterp *pSics, void *object return 0; } - pNew->write_node = FindHdbNode(NULL,argv[2], pCon); - pNew->read_node = FindHdbNode(NULL,argv[3], pCon); + pNew->write_node = FindHdbNode(NULL, argv[2], pCon); + if (pNew->write_node == NULL) + SCWrite(pCon, "ERROR: write-node not found", eError); + if (pNew->write_node->value.dataType != HIPFLOAT && pNew->write_node->value.dataType != HIPINT) { + SCWrite(pCon, "ERROR: write-node not INT or FLOAT", eError); + pNew->write_node = NULL; + } + pNew->read_node = FindHdbNode(NULL, argv[3], pCon); + if (pNew->read_node->value.dataType != HIPFLOAT && pNew->read_node->value.dataType != HIPINT) { + SCWrite(pCon, "ERROR: read-node not INT or FLOAT", eError); + pNew->write_node = NULL; + } obj = FindCommandData(pSics,argv[4], "SctController"); - if(pNew->write_node == NULL || obj == NULL){ - SCWrite(pCon,"ERROR: node or controller not found", eError); + if (obj == NULL) + SCWrite(pCon, "ERROR: controller not found", eError); + if (pNew->write_node == NULL || pNew->read_node == NULL || obj == NULL) { SctDriveKill(pNew); return 0; } @@ -304,9 +347,10 @@ int ANSTO_SctMakeDriveAdapter(SConnection *pCon, SicsInterp *pSics, void *object /* make object dynamic by defining a descriptor command */ SetDescriptorKey(pNew->pDes, "creationCommand", "0"); } - AddCommand(pSics, argv[1], SctDriveCommand, SctDriveKill, pNew); - SetHdbProperty(pNew->write_node,"sicsdev",argv[1]); + AddCommand(pSics, argv[1], SctDriveCommand, SctDriveKill, pNew); + SetHdbProperty(pNew->write_node,"sicsdev",argv[1]); + pNew->controller_name = strdup(argv[4]); cb = MakeHipadabaCallback(SctDummyCallback, pNew, SctDriveDeleteNode); assert(cb); AppendHipadabaCallback(pNew->write_node, cb);