/*-------------------------------------------------------------------------- S T A N D A R D S C A N This is a library of scan functions for the SICS standard scan. copyright: see copyright.h Extracted from scan.c: Mark Koennecke, November 2004 ----------------------------------------------------------------------------*/ #include "sics.h" #include #include #include #include #include "fortify.h" #include "sdynar.h" #include "dynstring.h" #include "stringdict.h" #include "status.h" #include "sicsvar.h" #include "counter.h" #include "scan.h" #include "scan.i" #include "splitter.h" #include "danu.h" #include "userscan.h" #include "motor.h" #include "nxscript.h" #include "nxutil.h" #include "site.h" #include "lld.h" /*-----------------------------------------------------------------------*/ static char *fixExtension(char *filename) { if(strstr(filename,".hdf") != NULL) { changeExtension(filename,"dat"); } return filename; } /*------------------------------------------------------------------------*/ char *ScanMakeFileName(SicsInterp *pSics, SConnection *pCon) { pSicsVariable pPath = NULL, pPref = NULL, pEnd = NULL; char *pRes = NULL; int iLen, iNum, iYear; char pNumText[10]; CommandList *pCom = NULL; char simName[255]; /* make a simulated filename if in simulation mode */ if(pServ->simMode){ snprintf(simName,255,"%s/tmp/sim001001901.sim",getenv("HOME")); return strdup(simName); } pRes = makeFilename(pSics,pCon); if(pRes == NULL) { pRes = strdup("emergency.scn"); } return fixExtension(pRes); } /*-------------------------------------------------------------------------*/ void WriteTemplate(FILE *fd, FILE *temp, char *filename, pScanData pScan, SConnection *pCon, SicsInterp *pSics) { char pBuffer[512], pError[512], *pPtr, *pName; CommandList *pCom = NULL; pSicsVariable pVar = NULL; pDummy pDum = NULL; pIDrivable pDriv = NULL; pMotor pMot = NULL; pVarEntry pScanVar = NULL; void *pVoid = NULL; float fVal; int iRet; /* loop through description file and act along the way */ while(fgets(pBuffer,511,temp) != NULL) { pPtr = strstr(pBuffer,"!!VAR("); if(pPtr) /* handle a Sics variable */ { /* extract the name */ pName = pPtr + 6; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; /* find the variable */ pCom = FindCommand(pSics,pName); if(!pCom) { sprintf(pError,"ERROR: variable %s NOT found",pName); SCWrite(pCon,pError,eError); continue; } pVar = (pSicsVariable)pCom->pData; if(!pVar) { sprintf(pError,"ERROR: variable %s NOT found",pName); SCWrite(pCon,pError,eError); continue; } switch(pVar->eType) { case veFloat: sprintf(pError,"%f",pVar->fVal); break; case veInt: sprintf(pError,"%d",pVar->iVal); break; case veText: sprintf(pError,"%s",pVar->text); break; } /* finally write */ fprintf(fd,"%s %s\n",pBuffer,pError); continue; }/* end variable */ /*------- Drivable */ pPtr = strstr(pBuffer,"!!DRIV("); if(pPtr) { /* extract the name */ pName = pPtr + 7; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; /* find the variable */ pCom = FindCommand(pSics,pName); if(!pCom) { sprintf(pError,"ERROR: variable %s NOT found",pName); SCWrite(pCon,pError,eError); continue; } pDum = (pDummy)pCom->pData; if(!pDum) { sprintf(pError,"ERROR: variable %s is NOT drivable",pName); SCWrite(pCon,pError,eError); continue; } pDriv = (pIDrivable)pDum->pDescriptor->GetInterface(pDum,DRIVEID); if(!pDriv) { sprintf(pError,"ERROR: variable %s is NOT drivable",pName); SCWrite(pCon,pError,eError); continue; } fVal = pDriv->GetValue(pDum,pCon); fprintf(fd,"%s %f\n",pBuffer,fVal); continue; } /* end drive */ /*------- zero point */ pPtr = strstr(pBuffer,"!!ZERO("); if(pPtr) { /* extract the name */ pName = pPtr + 7; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; /* find the motor */ pMot = FindMotor(pSics,pName); if(!pMot) { sprintf(pError,"ERROR: motor %s NOT found",pName); SCWrite(pCon,pError,eError); continue; } iRet = MotorGetPar(pMot,"softzero",&fVal); if(!iRet) { SCWrite(pCon,"ERROR: failed to read zero point",eError); continue; } fprintf(fd,"%s %f\n",pBuffer,fVal); continue; } /* end zero point */ /*---------- scripted info */ pPtr = strstr(pBuffer,"!!SCRIPT("); if(pPtr) { /* extract the name */ pName = pPtr + 9; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; if(Tcl_Eval(InterpGetTcl(pSics),pName) == TCL_OK) { fprintf(fd,"%s\n",Tcl_GetStringResult(InterpGetTcl(pSics))); } else { SCWrite(pCon,"ERROR: failed to execute Tcl command",eError); strncpy(pBuffer, Tcl_GetStringResult(InterpGetTcl(pSics)), 511); SCWrite(pCon,pBuffer,eError); continue; } } /* ------- date */ pPtr = strstr(pBuffer,"!!DATE!!"); if(pPtr) { *pPtr = '\0'; SNXFormatTime(pError,511); fprintf(fd,"%s %s\n",pBuffer,pError); continue; } /*-------- filename */ pPtr = strstr(pBuffer,"!!FILE!!"); if(pPtr) { *pPtr = '\0'; fprintf(fd,"%s %s\n",pBuffer,filename); continue; } /*------------ scanzero */ pPtr = strstr(pBuffer,"!!SCANZERO!!"); if(pPtr && pScan != NULL) { *pPtr = '\0'; /* write zero point of first scan variable if motor */ DynarGet(pScan->pScanVar,0,&pVoid); pScanVar = (pVarEntry)pVoid; if(pScanVar) { pMot = NULL; pMot = FindMotor(pSics,ScanVarName(pScanVar)); if(pMot != NULL) { MotorGetPar(pMot,"softzero",&fVal); fprintf(fd,"%s zero = %8.3f\n",ScanVarName(pScanVar), fVal); } } } /* --------- plain text */ fprintf(fd,"%s",pBuffer); } /* end while */ } /*---------------------------------------------------------------------------*/ int WriteHeader(pScanData self) { int i, iRet; FILE *fd; assert(self->pSics); assert(self->pCon); /* open data file */ self->fd = fopen(self->pFile,"w"); if(!self->fd) { SCWrite(self->pCon,"ERROR: cannot write data file",eError); return 0; } /* open header description file */ fd = fopen(self->pHeaderFile,"r"); if(!fd) { SCWrite(self->pCon,"ERROR: cannot open header description file",eError); return 0; } WriteTemplate(self->fd,fd,self->pFile, self, self->pCon, self->pSics); /* remember position for seeking to it for writing data */ self->lPos = ftell(self->fd); fclose(fd); fclose(self->fd); self->fd = NULL; return 1; } /*---------------------------------------------------------------------------*/ int WriteHeaderOld(pScanData self) { int i, iRet; FILE *fd; char pBuffer[512], pError[512], *pPtr, *pName; CommandList *pCom = NULL; pSicsVariable pVar = NULL; pDummy pDum = NULL; pIDrivable pDriv = NULL; float fVal; pMotor pMot = NULL; pVarEntry pScanVar = NULL; void *pVoid = NULL; assert(self->pSics); assert(self->pCon); /* open data file */ self->fd = fopen(self->pFile,"w"); if(!self->fd) { SCWrite(self->pCon,"ERROR: cannot write data file",eError); return 0; } /* open header description file */ fd = fopen(self->pHeaderFile,"r"); if(!fd) { SCWrite(self->pCon,"ERROR: cannot open header description file",eError); return 0; } /* loop through description file and act along the way */ while(fgets(pBuffer,511,fd) != NULL) { pPtr = strstr(pBuffer,"!!VAR("); if(pPtr) /* handle a Sics variable */ { /* extract the name */ pName = pPtr + 6; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; /* find the variable */ pCom = FindCommand(self->pSics,pName); if(!pCom) { sprintf(pError,"ERROR: variable %s NOT found",pName); SCWrite(self->pCon,pError,eError); continue; } pVar = (pSicsVariable)pCom->pData; if(!pVar) { sprintf(pError,"ERROR: variable %s NOT found",pName); SCWrite(self->pCon,pError,eError); continue; } switch(pVar->eType) { case veFloat: sprintf(pError,"%f",pVar->fVal); break; case veInt: sprintf(pError,"%d",pVar->iVal); break; case veText: sprintf(pError,"%s",pVar->text); break; } /* finally write */ fprintf(self->fd,"%s %s\n",pBuffer,pError); continue; }/* end variable */ /*------- Drivable */ pPtr = strstr(pBuffer,"!!DRIV("); if(pPtr) { /* extract the name */ pName = pPtr + 7; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; /* find the variable */ pCom = FindCommand(self->pSics,pName); if(!pCom) { sprintf(pError,"ERROR: variable %s NOT found",pName); SCWrite(self->pCon,pError,eError); continue; } pDum = (pDummy)pCom->pData; if(!pDum) { sprintf(pError,"ERROR: variable %s is NOT drivable",pName); SCWrite(self->pCon,pError,eError); continue; } pDriv = (pIDrivable)pDum->pDescriptor->GetInterface(pDum,DRIVEID); if(!pDriv) { sprintf(pError,"ERROR: variable %s is NOT drivable",pName); SCWrite(self->pCon,pError,eError); continue; } fVal = pDriv->GetValue(pDum,self->pCon); fprintf(self->fd,"%s %f\n",pBuffer,fVal); continue; } /* end drive */ /*------- zero point */ pPtr = strstr(pBuffer,"!!ZERO("); if(pPtr) { /* extract the name */ pName = pPtr + 7; /* first char of name */ *pPtr = '\0'; /* this is the starter of the line */ pPtr = pName; while( (*pPtr != '\0') && (*pPtr != ')') ) { pPtr++; } *pPtr = '\0'; /* find the motor */ pMot = FindMotor(self->pSics,pName); if(!pMot) { sprintf(pError,"ERROR: motor %s NOT found",pName); SCWrite(self->pCon,pError,eError); continue; } iRet = MotorGetPar(pMot,"softzero",&fVal); if(!iRet) { SCWrite(self->pCon,"ERROR: failed to read zero point",eError); continue; } fprintf(self->fd,"%s %f\n",pBuffer,fVal); continue; } /* end zero point */ /* ------- date */ pPtr = strstr(pBuffer,"!!DATE!!"); if(pPtr) { *pPtr = '\0'; SNXFormatTime(pError,511); fprintf(self->fd,"%s %s\n",pBuffer,pError); continue; } /*-------- filename */ pPtr = strstr(pBuffer,"!!FILE!!"); if(pPtr) { *pPtr = '\0'; fprintf(self->fd,"%s %s\n",pBuffer,self->pFile); continue; } /*------------ scanzero */ pPtr = strstr(pBuffer,"!!SCANZERO!!"); if(pPtr) { *pPtr = '\0'; /* write zero point of first scan variable if motor */ DynarGet(self->pScanVar,0,&pVoid); pScanVar = (pVarEntry)pVoid; if(pScanVar) { pMot = NULL; pMot = FindMotor(self->pSics,ScanVarName(pScanVar)); if(pMot != NULL) { MotorGetPar(pMot,"softzero",&fVal); fprintf(self->fd,"%s zero = %8.3f\n",ScanVarName(pScanVar), fVal); } } } /* --------- plain text */ fprintf(self->fd,"%s",pBuffer); } /* end while */ /* remember position for seeking to it for writing data */ self->lPos = ftell(self->fd); fclose(fd); fclose(self->fd); self->fd = NULL; return 1; } /*--------------------------------------------------------------------------*/ int WriteScanPoints(pScanData self, int iPoint) { int i, i2; char pLine[512], pItem[30], pInfo[1024], pSteps[256]; pVarEntry pVar = NULL; pCountEntry pData = NULL; void *pPtr = NULL; assert(self->pCon); assert(self->pSics); /* reopen file */ self->fd = fopen(self->pFile,"r+"); if(!self->fd) { SCWrite(self->pCon, "ERROR: Failed to reopen scan file, aborting scan", eError); return 0; } /* jump to end of header */ fseek(self->fd,self->lPos, SEEK_SET); if(self->iChannel != 0) { fprintf(self->fd,"WARNING: Scanning monitor %d\n",self->iChannel); } /* make the data header */ sprintf(pLine,"%-4s ","NP"); strcpy(pInfo,"Scanning Variables: "); strcpy(pSteps,"Steps: "); for(i = 0; i < self->iScanVar;i++) { DynarGet(self->pScanVar,i,&pPtr); pVar = (pVarEntry)pPtr; if(pVar) { sprintf(pItem,"%-9.9s ",ScanVarName(pVar)); strcat(pLine,pItem); sprintf(pItem,"%s, ",ScanVarName(pVar)); strcat(pInfo,pItem); sprintf(pItem,"%f ",ScanVarStep(pVar)); strcat(pSteps,pItem); } } strcat(pLine," Counts "); strcat(pLine,"Monitor1 "); strcat(pLine,"Monitor2 "); strcat(pLine,"Monitor3 "); strcat(pLine,"Time "); strcat(pInfo,pSteps); sprintf(pItem,"\n%d Points,",self->iNP); strcat(pInfo,pItem); if(self->iMode == eTimer) { strcat(pInfo," Mode: Timer,"); } else { strcat(pInfo," Mode: Monitor,"); } sprintf(pItem," Preset %f",self->fPreset); strcat(pInfo,pItem); fprintf(self->fd,"%s\n",pInfo); fprintf(self->fd,"%s\n",pLine); /* now the scan points */ for(i = 0; i < self->iCounts; i++) { sprintf(pLine,"%-4d ",i); /* print vars */ for(i2 = 0; i2 < self->iScanVar; i2++) { DynarGet(self->pScanVar,i2,&pPtr); pVar = (pVarEntry)pPtr; if(pVar) { sprintf(pItem,"%-9.3f ",GetScanVarPos(pVar,i)); strcat(pLine,pItem); } } /* print Counts & Monitor */ DynarGet(self->pCounts,i,&pPtr); pData = (pCountEntry)pPtr; if(pData) { sprintf(pItem," %-11ld ",pData->lCount); strcat(pLine,pItem); sprintf(pItem,"%-11ld ",pData->Monitors[0]); strcat(pLine,pItem); sprintf(pItem,"%-11ld ",pData->Monitors[1]); strcat(pLine,pItem); sprintf(pItem,"%-11ld ",pData->Monitors[2]); strcat(pLine,pItem); sprintf(pItem,"%-5.1f ",pData->fTime); strcat(pLine,pItem); } fprintf(self->fd,"%s\n",pLine); } /* done */ fprintf(self->fd,"END-OF-DATA\n"); fclose(self->fd); self->fd = NULL; return 1; } /*------------------------------------------------------------------------*/ int prepareDataFile(pScanData self){ char *pPtr = NULL; char pBueffel[512]; /* allocate a new data file */ pPtr = ScanMakeFileName(self->pSics,self->pCon); if(!pPtr) { SCWrite(self->pCon, "ERROR: cannot allocate new data filename, Scan aborted", eError); self->pCon = NULL; self->pSics = NULL; return 0; } snprintf(pBueffel,511,"Writing data file: %s ...",pPtr); SCWrite(self->pCon,pBueffel,eWarning); strcpy(self->pFile,pPtr); free(pPtr); return 1; } /*--------------------------------------------------------------------------*/ int PrepareScan(pScanData self) { pVarEntry pVar = NULL; void *pDings; int i, iRet; float fVal; char pBueffel[512]; char pMessage[1024]; assert(self); assert(self->iNP > 0); assert(self->pCon); /* check boundaries of scan variables and allocate storage */ for(i = 0; i < self->iScanVar; i++) { DynarGet(self->pScanVar,i,&pDings); pVar = (pVarEntry)pDings; if(pVar) { iRet = CheckScanVar(pVar,self->pCon,self->iNP - 1); if(!iRet) { return 0; } InitScanVar(pVar); } else { SCWrite(self->pCon, "WARNING: Internal error, no scan variable, I try to continue", eWarning); } pVar = NULL; } /* end for */ /* configure counter */ SetCounterMode((pCounter)self->pCounterData,self->iMode); SetCounterPreset((pCounter)self->pCounterData, self->fPreset); self->iCounts = 0; if(!prepareDataFile(self)){ return 0; } return 1; } /*--------------------------------------------------------------------------*/ int SilentPrepare(pScanData self) { pVarEntry pVar = NULL; void *pDings; int i, iRet; float fVal; char pBueffel[512]; char pMessage[1024]; assert(self); assert(self->iNP > 0); assert(self->pCon); /* check boundaries of scan variables and allocate storage */ for(i = 0; i < self->iScanVar; i++) { DynarGet(self->pScanVar,i,&pDings); pVar = (pVarEntry)pDings; if(pVar) { iRet = CheckScanVar(pVar,self->pCon,self->iNP); if(!iRet) { return 0; } InitScanVar(pVar); } else { SCWrite(self->pCon, "WARNING: Internal error, no scan variable, I try to continue", eWarning); } pVar = NULL; } /* end for */ /* configure counter */ SetCounterMode((pCounter)self->pCounterData,self->iMode); SetCounterPreset((pCounter)self->pCounterData, self->fPreset); self->iCounts = 0; return 1; } /*--------------------------------------------------------------------------*/ int NonCheckPrepare(pScanData self) { pVarEntry pVar = NULL; void *pDings; int i, iRet; char pBueffel[512]; char pMessage[1024]; assert(self); assert(self->iNP > 0); assert(self->pCon); /* allocate storage for scan variables */ for(i = 0; i < self->iScanVar; i++) { DynarGet(self->pScanVar,i,&pDings); pVar = (pVarEntry)pDings; if(pVar) { InitScanVar(pVar); } else { SCWrite(self->pCon, "WARNING: Internal error, no scan variable, I try to continue", eWarning); } pVar = NULL; } /* end for */ /* configure counter */ SetCounterMode((pCounter)self->pCounterData,self->iMode); SetCounterPreset((pCounter)self->pCounterData, self->fPreset); self->iCounts = 0; if(!prepareDataFile(self)){ return 0; } return 1; } /*------------------------------------------------------------------------*/ static int CollectScanDataIntern(pScanData self, int iPoint, int jochenFlag) { pVarEntry pVar = NULL; void *pDings; int i, iRet, status; char pStatus[512], pItem[20]; char pHead[512]; float fVal; CountEntry sCount; assert(self); assert(self->pCon); InitCountEntry(&sCount); /* prepare output header */ sprintf(pHead,"%-4.4s ","NP"); sprintf(pStatus,"%-4d ",iPoint); /* loop over all scan variables */ status = 1; for(i = 0; i < self->iScanVar; i++) { DynarGet(self->pScanVar,i,&pDings); pVar = (pVarEntry)pDings; if(pVar) { if(jochenFlag == 1 && strcmp(pVar->pObject->pDescriptor->name, "Motor") == 0) { MotorGetSoftPosition((pMotor)pVar->pObject,self->pCon,&fVal); } else { fVal = pVar->pInter->GetValue(pVar->pObject,self->pCon); } AppendScanVar(pVar,fVal); sprintf(pItem,"%-9.9s ",ScanVarName(pVar)); strcat(pHead,pItem); sprintf(pItem,"%-9.3f ",fVal); strcat(pStatus,pItem); } } /* store counter data */ sCount = CollectCounterData(self); /* format header */ strcat(pHead,"Counts "); sprintf(pItem,"%-14ld ",sCount.lCount); strcat(pStatus,pItem); strcat(pHead,"Monitor1 "); sprintf(pItem,"%-11ld ",sCount.Monitors[0]); strcat(pStatus,pItem); strcat(pHead,"Monitor2 "); sprintf(pItem,"%-11ld ",sCount.Monitors[1]); strcat(pStatus,pItem); strcat(pHead,"Monitor3 "); sprintf(pItem,"%-11ld ",sCount.Monitors[2]); strcat(pStatus,pItem); strcat(pHead,"Time "); sprintf(pItem,"%-6.1f",sCount.fTime); strcat(pStatus,pItem); /* write progress */ strcat(pHead,"\n"); strcat(pStatus,"\n"); SCWrite(self->pCon,pHead,eWarning); SCWrite(self->pCon,pStatus,eWarning); return 1; } /*---------------------------------------------------------------------------*/ int CollectScanData(pScanData self, int iPoint) { return CollectScanDataIntern(self,iPoint,0); } /*--------------------------------------------------------------------------*/ int CollectScanDataJochen(pScanData self, int iPoint) { return CollectScanDataIntern(self,iPoint,1); } /*------------------------------------------------------------------------*/ int CollectSilent(pScanData self, int iPoint) { pVarEntry pVar = NULL; void *pDings; int i, iRet, status, jochenFlag = 1; float fVal; CountEntry sCount; assert(self); assert(self->pCon); InitCountEntry(&sCount); /* loop over all scan variables */ status = 1; for(i = 0; i < self->iScanVar; i++) { DynarGet(self->pScanVar,i,&pDings); pVar = (pVarEntry)pDings; if(pVar) { if(jochenFlag == 1 && strcmp(pVar->pObject->pDescriptor->name, "Motor") == 0) { MotorGetSoftPosition((pMotor)pVar->pObject,self->pCon,&fVal); } else { fVal = pVar->pInter->GetValue(pVar->pObject,self->pCon); } AppendScanVar(pVar,fVal); } } /* store counter data */ sCount = CollectCounterData(self); return 1; } /*--------------------------------------------------------------------------*/ static int StartToDrive(pScanData self, int iPoint) { pVarEntry pVar = NULL; void *pDings; int i, iRet, status; assert(self); assert(self->pCon); /* loop over all scan variables */ status = 1; for(i = 0; i < self->iScanVar; i++) { DynarGet(self->pScanVar,i,&pDings); pVar = (pVarEntry)pDings; if(pVar) { iRet = StartScanVar(pVar,self->pCon,iPoint); if(!iRet) { status = 0; break; } } } return status; } /*------------------------------------------------------------------------*/ int ScanDrive(pScanData self, int iPoint) { int iRet; long lTask; int status; iRet = StartToDrive(self,iPoint); if(!iRet) { SCWrite(self->pCon,"ERROR: Cannot Drive, Scan aborted",eError); status = 0; } else { status = 1; } /* wait for finish */ lTask = GetDevexecID(pServ->pExecutor); if(lTask > 0) { TaskWait(pServ->pTasker,lTask); } return status; } /*------------------------------------------------------------------------*/ int ScanFastDrive(pScanData self, int iPoint) { int iRet; long lTask; int status; /* * drive the first point normally, otherwise there is to much intensity * in the first data point from the long time driving to the start * position. */ if(iPoint == 0) { return ScanDrive(self,iPoint); } iRet = StartToDrive(self,iPoint); if(!iRet) { SCWrite(self->pCon,"ERROR: Cannot Drive, Scan aborted",eError); status = 0; } else { status = 1; } return status; } /*--------------------------------------------------------------------------*/ int ScanCount(pScanData self, int iPoint) { pDummy pDum; int iRet; long lTask; pDum = (pDummy)self->pCounterData; iRet = StartDevice(pServ->pExecutor, "ScanCounter", pDum->pDescriptor, self->pCounterData, self->pCon, self->fPreset); if(!iRet) { SCWrite(self->pCon,"ERROR: Cannot Count, Scan aborted",eError); return 0; } SetStatus(eCounting); /* wait for finish */ lTask = GetDevexecID(pServ->pExecutor); if(lTask > 0); { TaskWait(pServ->pTasker,lTask); } return 1; } /*====================== script invocation functions ====================*/ static pDynString GetStandardInvocation(pScanData self, char *function){ pDynString result = NULL; char value[132]; result = CreateDynString(80,80); if(result == NULL){ SCWrite(self->pCon,"ERROR: out of memory in scan invocation",eError); return NULL; } if(StringDictGet(self->scanFunctions,function,value,131) != 1){ snprintf(value,131,"ERROR: scan function %s not found",function); SCWrite(self->pCon,value,eError); DeleteDynString(result); return NULL; } DynStringCopy(result,value); DynStringConcatChar(result,' '); DynStringConcat(result, self->objectName); DynStringConcatChar(result,' '); value[0] = '\0'; StringDictGet(self->scanFunctions,"userdata",value,131); DynStringConcat(result,value); DynStringConcatChar(result,' '); return result; } /*-----------------------------------------------------------------------*/ static int StandardScriptInvoke(pScanData self, char *function){ pDynString command = NULL; int status; command = GetStandardInvocation(self,function); if(command == NULL){ return 0; } status = InterpExecute(self->pSics, self->pCon, GetCharArray(command)); DeleteDynString(command); if(status != 1) { return 0; } return status; } /*-----------------------------------------------------------------------*/ static int StandardScriptInvokeWithPoint(pScanData self, char *function, int iPoint){ pDynString command = NULL; int status; char pNumber[50]; command = GetStandardInvocation(self,function); if(command == NULL){ return 0; } snprintf(pNumber,49,"%d",iPoint); DynStringConcat(command,pNumber); status = InterpExecute(self->pSics, self->pCon, GetCharArray(command)); DeleteDynString(command); if(status != 1) { return 0; } return status; } /*------------------------------------------------------------------------*/ int ScriptWriteHeader(pScanData self){ return StandardScriptInvoke(self,"writeheader"); } /*---------------------------------------------------------------------*/ int ScriptPrepareScan(pScanData self){ /* configure counter */ SetCounterMode((pCounter)self->pCounterData,self->iMode); SetCounterPreset((pCounter)self->pCounterData, self->fPreset); self->iCounts = 0; return StandardScriptInvoke(self,"prepare"); } /*-----------------------------------------------------------------------*/ int ScriptScanDrive(pScanData self, int iPoint){ return StandardScriptInvokeWithPoint(self,"drive",iPoint); } /*------------------------------------------------------------------------*/ int ScriptScanCount(pScanData self, int iPoint){ pDynString command = NULL; int status; char pNumber[50]; command = GetStandardInvocation(self,"count"); if(command == NULL){ return 0; } snprintf(pNumber,49,"%d",iPoint); DynStringConcat(command,pNumber); if(self->iMode == eTimer){ DynStringConcat(command," timer "); } else { DynStringConcat(command," monitor "); } snprintf(pNumber,49," %f ", self->fPreset); DynStringConcat(command,pNumber); status = InterpExecute(self->pSics, self->pCon, GetCharArray(command)); DeleteDynString(command); if(status != 1) { return 0; } return status; } /*-------------------------------------------------------------------------*/ int ScriptScanCollect(pScanData self, int iPoint){ return StandardScriptInvokeWithPoint(self,"collect",iPoint); } /*----------------------------------------------------------------------*/ int ScriptWriteScanPoints(pScanData self, int iPoint){ return StandardScriptInvokeWithPoint(self,"writepoint",iPoint); } /*--------------------------------------------------------------------*/ int ScriptScanFinish(pScanData self){ return StandardScriptInvoke(self,"finish"); } /*---------------------------------------------------------------------*/ void ConfigureScript(pScanData self){ assert(self); self->PrepareScan = ScriptPrepareScan; self->WriteHeader = ScriptWriteHeader; self->WriteScanPoints = ScriptWriteScanPoints; self->ScanDrive = ScriptScanDrive; self->ScanCount = ScriptScanCount; self->CollectScanData = ScriptScanCollect; } /*======================================================================*/ int StandardScanWrapper(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]){ pScanData self = NULL; int iPoint; char pError[132]; if(argc < 4) { SCWrite(pCon,"ERROR: not enough arguments to stdscan",eError); return 0; } strtolower(argv[1]); self = (pScanData)FindCommandData(pSics,argv[2],"ScanObject"); assert(self); if(strcmp(argv[1],"writeheader") == 0){ return WriteHeader(self); } else if(strcmp(argv[1],"prepare") == 0){ return PrepareScan(self); } else if(strcmp(argv[1],"finish") == 0) { return 1; } /* from here on we need a scan point */ if(argc < 5) { SCWrite(pCon,"ERROR: not enough arguments to stdscan",eError); return 0; } iPoint = atoi(argv[4]); if(strcmp(argv[1],"drive") == 0){ return ScanDrive(self,iPoint); } else if(strcmp(argv[1],"count") == 0){ return ScanCount(self,iPoint); } else if(strcmp(argv[1],"collect") == 0){ return CollectScanData(self,iPoint); } else if(strcmp(argv[1],"silentcollect") == 0){ return CollectSilent(self,iPoint); } else if(strcmp(argv[1],"writepoint") == 0){ return WriteScanPoints(self,iPoint); } else { snprintf(pError,131,"ERROR: subcommand %s to stdscan not found", argv[1]); SCWrite(pCon,pError,eError); return 0; } return 0; }