diff --git a/site_ansto/hardsup/chopper.c b/site_ansto/hardsup/chopper.c index 218b2b89..e7e41d60 100644 --- a/site_ansto/hardsup/chopper.c +++ b/site_ansto/hardsup/chopper.c @@ -1,9 +1,9 @@ /*-------------------------------------------------------------- - This is a driver for the newer Astrium == Dornier chopper + This is a driver for the newer Astrium == Dornier chopper systems which use a TCP/IP server for communication. This driver has to take care of some ugliness: - As of december 2005, the communication is in unicode! - To go from ASCII to unicode and back one has to + To go from ASCII to unicode and back one has to add a 0x00 before each character or to remove it. - The controller is slow in responding and the controller must be watched in the environment monitor. This is taken @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include /*======================================================================== Our private data structure @@ -62,7 +62,7 @@ Error codes: #define BADVALUE -8310 extern char *trim(char *str); -#define ABS(x) (x < 0 ? -(x) : (x)) +#define ABS(x) (x < 0 ? -(x) : (x)) #define SPEEDTOL 2 #define PHASETOL 0.2 @@ -76,15 +76,15 @@ static int asciiToUnicode(char *input, char **output){ len = strlen(input); result = (char *)malloc(2*len*sizeof(char)); if(result == NULL){ - return 0; + return 0; } memset(result,0,2*len*sizeof(char)); for(i = 0; i < len; i++){ - result[i*2] = input[i]; + result[i*2] = input[i]; } - *output = result; + *output = result; return 2*len; -} +} #endif /*-------------------------------------------------------------------*/ #if 0 @@ -96,11 +96,11 @@ static int unicodeToAscii(char *input, int len, char **output){ result = (char *)malloc((alen+1)*sizeof(char)); if(result == NULL){ - return 0; + return 0; } memset(result,0,(alen+1)*sizeof(char)); for(i = 0; i < alen; i++){ - result[i] = input[i*2]; + result[i] = input[i*2]; } *output = result; return alen; @@ -108,7 +108,7 @@ static int unicodeToAscii(char *input, int len, char **output){ #endif /*----------------------------------------------------------------*/ static int tcpDoChoSend(pTcpDoCho self, char *command, int choNum, - char *value){ + char *value){ char buffer[1024]; int sendlen, status; @@ -117,22 +117,22 @@ static int tcpDoChoSend(pTcpDoCho self, char *command, int choNum, */ self->lastError = 0; if(choNum < 0){ - snprintf(buffer,1023,"#SOS#%s \r\n",command); + snprintf(buffer,1023,"#SOS#%s \r\n",command); } else if(value != NULL){ - snprintf(buffer,1023,"#SOS#%s%1.1d: %s\r\n",command, choNum, - value); + snprintf(buffer,1023,"#SOS#%s%1.1d: %s\r\n",command, choNum, + value); } else { - snprintf(buffer,1023,"#SOS#%s%1.1d:\r\n",command, choNum); + snprintf(buffer,1023,"#SOS#%s%1.1d:\r\n",command, choNum); } sendlen = strlen(buffer); if(sendlen == 0){ - self->lastError = BADCONVERSION; - return 0; + self->lastError = BADCONVERSION; + return 0; } status = send(self->controller->pSock->sockid,buffer,sendlen,0); if(status < 0){ - self->lastError = BADWRITE; - return 0; + self->lastError = BADWRITE; + return 0; } return 1; } @@ -147,14 +147,14 @@ static int statusComplete(char *statusMessage){ */ pPtr = strstr(statusMessage,"TIME"); if(pPtr != NULL){ - pPtr = strstr(pPtr+6,"#"); - if(pPtr != NULL){ - return 1; - } else { - return 0; - } + pPtr = strstr(pPtr+6,"#"); + if(pPtr != NULL){ + return 1; + } else { + return 0; + } } else { - return 0; + return 0; } } /*---------------------------------------------------------------*/ @@ -164,7 +164,7 @@ static int readChopperNum(char *entry){ pPtr = strstr(entry,"CH"); if(pPtr == NULL){ - return -1; + return -1; } sscanf(pPtr+3,"%d",&num); return num; @@ -194,27 +194,27 @@ static void addEntry(pTcpDoCho self, int choNum, char *entry){ StringDictAddPair(self->parameters,name,trim(value)); } } -} +} /*----------------------------------------------------------------*/ static int parseStatus(pTcpDoCho self, char *statusMessage){ int choNum; char entry[80], *pPtr; - + pPtr = statusMessage; /* skip over SOS */ pPtr = stptok(pPtr,entry,79,"#"); pPtr = stptok(pPtr,entry,79,"#"); pPtr = stptok(pPtr,entry,79,"#"); - + choNum = readChopperNum(entry); if(choNum < 0){ - self->lastError = BADRESPONSE; - return 0; + self->lastError = BADRESPONSE; + return 0; } while((pPtr = stptok(pPtr,entry,79,"#")) != NULL){ - addEntry(self,choNum,entry); - memset(entry,0,80); + addEntry(self,choNum,entry); + memset(entry,0,80); } return 1; } @@ -229,51 +229,51 @@ static int tcpDoChoReceive(pTcpDoCho self){ bytesRead = 0; memset(buffer,0,1024*sizeof(char)); while(time(NULL) < endTime){ - if(availableRS232(self->controller)){ - bytesRead += recv(self->controller->pSock->sockid,buffer+bufferStart, - 1024 - bytesRead,0); - if(bytesRead < 0){ - self->lastError = BADREAD; - return 0; - } + if(availableRS232(self->controller)){ + bytesRead += recv(self->controller->pSock->sockid,buffer+bufferStart, + 1024 - bytesRead,0); + if(bytesRead < 0){ + self->lastError = BADREAD; + return 0; + } if(strstr(buffer,"State") != NULL){ - if(statusComplete(buffer) == 0) { - continue; - } else { - status = parseStatus(self,buffer); - return status; - } - } - if(strstr(buffer,"ACCEPT") != NULL){ - return 1; + if(statusComplete(buffer) == 0) { + continue; + } else { + status = parseStatus(self,buffer); + return status; + } + } + if(strstr(buffer,"ACCEPT") != NULL){ + return 1; } else if(strstr(buffer,"NCCEPT") != NULL){ - self->lastError = FAILEDCOMMAND; - return 0; + self->lastError = FAILEDCOMMAND; + return 0; } else { - self->lastError = BADRESPONSE; - return 0; - } + self->lastError = BADRESPONSE; + return 0; + } } else { - SicsWait(1); - } + SicsWait(1); + } } self->lastError = TIMEOUT; return 0; } /*-----------------------------------------------------------------*/ static int tcpDoChoCommand(pTcpDoCho self, char *command, int choNum, - char *value){ + char *value){ int status; - + status = tcpDoChoSend(self,command,choNum,value); if(status == 0){ - return 0; + return 0; } return tcpDoChoReceive(self); } /*---------------------------------------------------------------*/ /* TODO Check for response on authentication - * authentication succeeded: #SES#Hello + * authentication succeeded: #SES#Hello * authentication failed: #SES#You are not a valid user, try again! * Make sure that ch2,3,4 are set to MW on init */ @@ -283,8 +283,8 @@ static int TcpDoChoConnect(pTcpDoCho self){ status = initRS232(self->controller); if(status != 1){ - self->lastError = status; - return 0; + self->lastError = status; + return 0; } setRS232Timeout(self->controller,5); @@ -297,36 +297,36 @@ static int TcpDoChoConnect(pTcpDoCho self){ snprintf(buffer,255,"user:%s\r\n",self->user); sendLen = strlen(buffer); if(sendLen == 0){ - self->lastError = BADCONVERSION; + self->lastError = BADCONVERSION; return 0; } status = send(self->controller->pSock->sockid,buffer,sendLen,0); if(status < 0){ - self->lastError = BADSEND; - return 0; - } - readLen = 255; - readRS232(self->controller,(void *)buffer,&readLen); - - /* - password - */ - snprintf(buffer,255,"password:%s\r\n",self->pword); - sendLen = strlen(buffer); - if(sendLen == 0){ - self->lastError = BADCONVERSION; + self->lastError = BADSEND; return 0; } - status = send(self->controller->pSock->sockid,buffer,sendLen,0); - if(status < 0){ - self->lastError = BADSEND; - return 0; - } readLen = 255; readRS232(self->controller,(void *)buffer,&readLen); /* - TODO: responses should be checked to test for a valid login. + password + */ + snprintf(buffer,255,"password:%s\r\n",self->pword); + sendLen = strlen(buffer); + if(sendLen == 0){ + self->lastError = BADCONVERSION; + return 0; + } + status = send(self->controller->pSock->sockid,buffer,sendLen,0); + if(status < 0){ + self->lastError = BADSEND; + return 0; + } + readLen = 255; + readRS232(self->controller,(void *)buffer,&readLen); + + /* + TODO: responses should be checked to test for a valid login. I do not know at this time how the controller reacts upon a bad login. */ @@ -348,32 +348,32 @@ static int TcpChopperTask(void *pData){ assert(pPriv); if(pPriv->stop == 1){ - return 0; + return 0; } - + if(time(NULL) > pPriv->nextRefresh){ - if(pPriv->lastError != 0){ - self->GetError(self,&code,buffer,79); - snprintf(error,511,"WARNING: chopper tries to fix: %s",buffer); - WriteToCommandLog("Chopper-task:>>", error); + if(pPriv->lastError != 0){ + self->GetError(self,&code,buffer,79); + snprintf(error,511,"WARNING: chopper tries to fix: %s",buffer); + WriteToCommandLog("Chopper-task:>>", error); status = self->TryFixIt(self,code); - if(status == CHFAIL){ - pPriv->nextRefresh = time(NULL) + pPriv->iRefreshIntervall; - return 1; - } - } else { - pPriv->busy = 1; - for(i = 0; i < pPriv->numChoppers; i++){ - status = tcpDoChoCommand(pPriv,"STATE ", i+1,NULL); - if(status != 1){ - /* - force error correction - */ - return 1; - } + if(status == CHFAIL){ + pPriv->nextRefresh = time(NULL) + pPriv->iRefreshIntervall; + return 1; } - pPriv->nextRefresh = time(NULL) + pPriv->iRefreshIntervall; - pPriv->busy = 0; + } else { + pPriv->busy = 1; + for(i = 0; i < pPriv->numChoppers; i++){ + status = tcpDoChoCommand(pPriv,"STATE ", i+1,NULL); + if(status != 1){ + /* + force error correction + */ + return 1; + } + } + pPriv->nextRefresh = time(NULL) + pPriv->iRefreshIntervall; + pPriv->busy = 0; } } return 1; @@ -388,13 +388,13 @@ static int TcpDoChoKill(pCodri self){ return 1; if(pPriv->controller != NULL){ - KillRS232(pPriv->controller); + KillRS232(pPriv->controller); } if(pPriv->parameters != NULL){ - DeleteStringDict(pPriv->parameters); + DeleteStringDict(pPriv->parameters); } if(pPriv->config != NULL){ - free(pPriv->config); + free(pPriv->config); } free(pPriv); return 1; @@ -411,11 +411,11 @@ static int TcpDoChoConfigure(pCodri pDriv){ assert(self != NULL); if(self->config != NULL){ - pPtr = self->config; - while( (pPtr = stptok(pPtr,command,79,"\n")) != NULL){ - status = tcpDoChoCommand(self,command,-1,NULL); - if(status != 1){ - return 0; + pPtr = self->config; + while( (pPtr = stptok(pPtr,command,79,"\n")) != NULL){ + status = tcpDoChoCommand(self,command,-1,NULL); + if(status != 1){ + return 0; } } } @@ -429,32 +429,32 @@ static int TcpDoChoInit(pCodri pDriv){ assert(pDriv != NULL); self = (pTcpDoCho)pDriv->pPrivate; assert(self != NULL); - + self->lastError = 0; self->stop = 0; self->nextRefresh = 0; - + status = TcpDoChoConnect(self); if(status != 1){ - return 0; + return 0; } status = TcpDoChoConfigure(pDriv); if(status != 1){ - return 0; + return 0; } #if 0 /* start the update task */ if(self->lTask == 0){ - self->lTask = TaskRegister(pServ->pTasker, - TcpChopperTask, - NULL, - NULL, - pDriv, - 1); + self->lTask = TaskRegister(pServ->pTasker, + TcpChopperTask, + NULL, + NULL, + pDriv, + 1); } #endif - return 1; + return 1; } /*-------------------------------------------------------------------*/ static void waitForBusy(pTcpDoCho self){ @@ -462,11 +462,11 @@ static void waitForBusy(pTcpDoCho self){ endTime = time(NULL) + 10 *60; /* max 10 min */ while(time(NULL) < endTime){ - if(self->busy == 1){ - SicsWait(3); + if(self->busy == 1){ + SicsWait(3); } else { - return; - } + return; + } } WriteToCommandLog("Chopper-task>> ","WARNING: timeout on busy flag, flag forced"); self->busy = 0; @@ -481,8 +481,8 @@ static int TcpDoChoClose(pCodri pDriv){ self->stop = 1; if(self->controller != NULL){ - KillRS232(self->controller); - self->controller = NULL; + KillRS232(self->controller); + self->controller = NULL; } return 1; } @@ -492,20 +492,20 @@ static int dissectName(char *name, char par[80], int *num){ pPtr = strrchr(name,(int)'_'); if(pPtr == NULL){ - return 0; + return 0; } memset(par,0,80*sizeof(char)); strncpy(par,name,pPtr - name); if(sscanf(pPtr+1,"%d",num) != 1){ - return 0; + return 0; } return 1; } /* Use this to coerce speed and phase for choppers 2 and 3 - * into the same coordinate convention as chopper 1. + * into the same coordinate convention as chopper 1. * errno = 0 if successful * on failure errorcode is set in lastError - */ + */ #if 0 static int fixDirection(pTcpDoCho self, int choNum, char *value, int value_len) { double dValue; @@ -523,52 +523,52 @@ static int fixDirection(pTcpDoCho self, int choNum, char *value, int value_len) #endif /*----------------------------------------------------------------------*/ static int TcpDoChoSetPar2(pCodri pDriv, char *parname, char *value){ - + pTcpDoCho self = NULL; int status, choNum; - char par[80], buffer[80], state[80]; + char par[80], buffer[80], state[80]; assert(pDriv != NULL); self = (pTcpDoCho)pDriv->pPrivate; assert(self != NULL); - + /* force status requests right after setting something in order to make the stored status represent the new target values */ if(dissectName(parname,par,&choNum)){ - /* - check for emergency stop - */ - snprintf(buffer,79,"State_%1.1d",choNum); - memset(state,0,80*sizeof(char)); - StringDictGet(self->parameters,buffer,state,79); - if(strstr(state,"E-Stop") != NULL){ - self->lastError = ESTOP; - return 0; + /* + check for emergency stop + */ + snprintf(buffer,79,"State_%1.1d",choNum); + memset(state,0,80*sizeof(char)); + StringDictGet(self->parameters,buffer,state,79); + if(strstr(state,"E-Stop") != NULL){ + self->lastError = ESTOP; + return 0; } - if(strcmp(par,"speed") == 0){ - waitForBusy(self); - status = tcpDoChoCommand(self,"SPEED ",choNum,trim(value)); - tcpDoChoCommand(self,"STATE ",choNum,NULL); - if(status != 1){ - return 0; - } else { - return 1; - } - } else if(strcmp(par,"phase") == 0){ - waitForBusy(self); + if(strcmp(par,"speed") == 0){ + waitForBusy(self); + status = tcpDoChoCommand(self,"SPEED ",choNum,trim(value)); + tcpDoChoCommand(self,"STATE ",choNum,NULL); + if(status != 1){ + return 0; + } else { + return 1; + } + } else if(strcmp(par,"phase") == 0){ + waitForBusy(self); if (errno != 0) return 0; - status = tcpDoChoCommand(self,"PHASE ",choNum,trim(value)); - tcpDoChoCommand(self,"STATE ",choNum,NULL); - if(status != 1){ - return 0; - } else { - return 1; - } + status = tcpDoChoCommand(self,"PHASE ",choNum,trim(value)); + tcpDoChoCommand(self,"STATE ",choNum,NULL); + if(status != 1){ + return 0; + } else { + return 1; + } } } self->lastError = UNDRIVABLE; @@ -576,7 +576,7 @@ static int TcpDoChoSetPar2(pCodri pDriv, char *parname, char *value){ } /*-----------------------------------------------------------------------*/ static int TcpDoChoHalt(pCodri pDriv){ - + pTcpDoCho self = NULL; assert(pDriv != NULL); @@ -589,7 +589,7 @@ static int TcpDoChoHalt(pCodri pDriv){ } /*-----------------------------------------------------------------------*/ static int TcpDoChoSetPar(pCodri pDriv, char *parname, float fValue){ - + pTcpDoCho self = NULL; int choNum; char value[80]; @@ -601,26 +601,26 @@ static int TcpDoChoSetPar(pCodri pDriv, char *parname, float fValue){ assert(self != NULL); if(dissectName(parname,par,&choNum)){ - if(strcmp(par,"speed") == 0){ - snprintf(value,79,"%5.1f", fValue); - return TcpDoChoSetPar2(pDriv,parname,value); - } else if(strcmp(par,"phase") == 0){ - snprintf(value,79,"%6.2f",fValue); - return TcpDoChoSetPar2(pDriv,parname,value); + if(strcmp(par,"speed") == 0){ + snprintf(value,79,"%5.1f", fValue); + return TcpDoChoSetPar2(pDriv,parname,value); + } else if(strcmp(par,"phase") == 0){ + snprintf(value,79,"%6.2f",fValue); + return TcpDoChoSetPar2(pDriv,parname,value); } - } + } if(strcmp(parname,"updateintervall") == 0){ sprintf(value,"%d",(int)fValue); StringDictUpdate(self->parameters,"updateintervall",value); self->iRefreshIntervall = (int)fValue; return 1; } else { - snprintf(value,79,"%f",fValue); - return TcpDoChoSetPar2(pDriv,parname,value); + snprintf(value,79,"%f",fValue); + return TcpDoChoSetPar2(pDriv,parname,value); } } /*---------------------------------------------------------------------*/ -static int TcpDoChoGetPar(pCodri pDriv, char *parname, +static int TcpDoChoGetPar(pCodri pDriv, char *parname, char *pBuffer, int iBuflen){ pTcpDoCho self = NULL; int status = 0, choNum, i, j, ready; @@ -744,12 +744,12 @@ static int TcpDoChoCheckPar(pCodri pDriv, char *parname){ check for flags first */ if(self->busy){ - return HWBusy; + return HWBusy; } if(self->lastError != 0) { - return HWFault; + return HWFault; } - + /* updateintervall is always Idle */ @@ -764,38 +764,38 @@ static int TcpDoChoCheckPar(pCodri pDriv, char *parname){ memset(state,0,80*sizeof(char)); StringDictGet(self->parameters,buffer,state,79); if(strstr(state,"E-Stop") != NULL){ - self->lastError = HWFault; - return 0; + self->lastError = HWFault; + return 0; } - + memset(par,0,80); dissectName(parname,par,&choNum); if(strcmp(par,"speed") == 0){ snprintf(buffer,79,"RSPEED_%1.1d", choNum); - StringDictGet(self->parameters,buffer,csoll,79); - sscanf(csoll,"%f",&soll); + StringDictGet(self->parameters,buffer,csoll,79); + sscanf(csoll,"%f",&soll); snprintf(buffer,79,"ASPEED_%1.1d", choNum); - StringDictGet(self->parameters,buffer,value,79); - sscanf(value,"%f",&val); - delta = ABS(soll - val); - if(delta > SPEEDTOL){ - return HWBusy; - } else { - return HWIdle; - } + StringDictGet(self->parameters,buffer,value,79); + sscanf(value,"%f",&val); + delta = ABS(soll - val); + if(delta > SPEEDTOL){ + return HWBusy; + } else { + return HWIdle; + } } else if(strcmp(par,"phase") == 0){ snprintf(buffer,79,"RPHASE_%1.1d", choNum); - StringDictGet(self->parameters,buffer,csoll,79); - sscanf(value,"%f",&soll); + StringDictGet(self->parameters,buffer,csoll,79); + sscanf(value,"%f",&soll); snprintf(buffer,79,"APHASE_%1.1d", choNum); - StringDictGet(self->parameters,buffer,value,79); - sscanf(value,"%f",&val); - delta = ABS(soll - val); - if(delta > PHASETOL){ - return HWBusy; - } else { - return HWIdle; - } + StringDictGet(self->parameters,buffer,value,79); + sscanf(value,"%f",&val); + delta = ABS(soll - val); + if(delta > PHASETOL){ + return HWBusy; + } else { + return HWIdle; + } } self->lastError = BADPAR; return HWFault; @@ -810,36 +810,36 @@ static int TcpDoChoError(pCodri pDriv, int *iCode, char *pError, int iLen){ *iCode = self->lastError; switch(self->lastError){ - case WRONGMODE: - strncpy(pError,"Chopper in wrong mode",iLen); - break; + case WRONGMODE: + strncpy(pError,"Chopper in wrong mode",iLen); + break; case BADCONVERSION: - strncpy(pError,"Bad ASCII to unicode conversion",iLen); + strncpy(pError,"Bad ASCII to unicode conversion",iLen); break; case FAILEDCOMMAND: - strncpy(pError,"Command not accepted",iLen); + strncpy(pError,"Command not accepted",iLen); break; case BADWRITE: - strncpy(pError,"Failed to write to chopper controller",iLen); + strncpy(pError,"Failed to write to chopper controller",iLen); break; case BADRESPONSE: - strncpy(pError,"Chopper controller send invalid command",iLen); + strncpy(pError,"Chopper controller send invalid command",iLen); break; case UNDRIVABLE: - strncpy(pError,"Parameter cannot be changed",iLen); + strncpy(pError,"Parameter cannot be changed",iLen); break; case BADPAR: - strncpy(pError,"No such parameter",iLen); + strncpy(pError,"No such parameter",iLen); break; case BADVALUE: strncpy(pError,"Invalid parameter value",iLen); break; - case ESTOP: - strncpy(pError,"Emergency stop is engaged",iLen); + case ESTOP: + strncpy(pError,"Emergency stop is engaged",iLen); + break; + default: + getRS232Error(self->lastError,pError,iLen); break; - default: - getRS232Error(self->lastError,pError,iLen); - break; } return 1; } @@ -847,8 +847,8 @@ static int TcpDoChoError(pCodri pDriv, int *iCode, char *pError, int iLen){ static int TcpDoChoFix(pCodri pDriv, int iCode){ pTcpDoCho self = NULL; int status = 0; - - + + assert(pDriv != NULL); self = (pTcpDoCho)pDriv->pPrivate; @@ -856,26 +856,26 @@ static int TcpDoChoFix(pCodri pDriv, int iCode){ self->lastError = 0; switch(iCode){ - case BADCONVERSION: + case BADCONVERSION: case BADRESPONSE: - return CHREDO; - break; + return CHREDO; + break; case WRONGMODE: case FAILEDCOMMAND: case UNDRIVABLE: case BADPAR: case ESTOP: return CHFAIL; - break; + break; default: - closeRS232(self->controller); - status = TcpDoChoConnect(self); - if(status == 1){ - return CHREDO; + closeRS232(self->controller); + status = TcpDoChoConnect(self); + if(status == 1){ + return CHREDO; } else { - return CHFAIL; - } - break; + return CHFAIL; + } + break; } return CHFAIL; } @@ -888,21 +888,21 @@ pCodri MakeTcpDoChoDriver(char *tclArray, SConnection *pCon){ int port, i, count; Tcl_DString pars; char *parnames[] = {"State", - "ASPEED", - "RSPEED", - "APHASE", - "RPHASE", - "AVETO", - "DIR", - "MONIT", - "FLOWR", - "WTEMP", - "MTEMP", - "MVIBR", - "MVACU", - "speed", - "phase", - NULL, + "ASPEED", + "RSPEED", + "APHASE", + "RPHASE", + "AVETO", + "DIR", + "MONIT", + "FLOWR", + "WTEMP", + "MTEMP", + "MVIBR", + "MVACU", + "speed", + "phase", + NULL, }; /* @@ -915,23 +915,23 @@ pCodri MakeTcpDoChoDriver(char *tclArray, SConnection *pCon){ } memset(pNew,0,sizeof(Codri)); memset(self,0,sizeof(TcpDoCho)); - + /* port and host name */ pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"port",TCL_GLOBAL_ONLY); if(!pPtr){ - SCWrite(pCon,"ERROR: port not found in configuration array for TCP Dornier Chopper", - eError); + SCWrite(pCon,"ERROR: port not found in configuration array for TCP Dornier Chopper", + eError); free(pNew); - free(self); + free(self); return NULL; } sscanf(pPtr,"%d",&port); pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"host",TCL_GLOBAL_ONLY); if(!pPtr){ - SCWrite(pCon,"ERROR: host not found in configuration array for TCP Dornier Chopper", - eError); + SCWrite(pCon,"ERROR: host not found in configuration array for TCP Dornier Chopper", + eError); free(pNew); - free(self); + free(self); return NULL; } memset(buffer,0,132); @@ -941,27 +941,27 @@ pCodri MakeTcpDoChoDriver(char *tclArray, SConnection *pCon){ /* number of choppers */ pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"nchopper",TCL_GLOBAL_ONLY); if(!pPtr){ - SCWrite(pCon,"ERROR: nchopper not found in configuration array for TCP Dornier Chopper", - eError); + SCWrite(pCon,"ERROR: nchopper not found in configuration array for TCP Dornier Chopper", + eError); free(pNew); - free(self); + free(self); return NULL; } sscanf(pPtr,"%d",&port); if(port < 0 || port > 8){ - SCWrite(pCon,"ERROR: number of choppers not in range 1 - 8",eError); + SCWrite(pCon,"ERROR: number of choppers not in range 1 - 8",eError); free(pNew); - free(self); + free(self); } self->numChoppers = port; /* timeout */ pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"timeout",TCL_GLOBAL_ONLY); if(!pPtr){ - SCWrite(pCon,"ERROR: timeout not found in configuration array for TCP Dornier Chopper", - eError); + SCWrite(pCon,"ERROR: timeout not found in configuration array for TCP Dornier Chopper", + eError); free(pNew); - free(self); + free(self); return NULL; } sscanf(pPtr,"%d",&port); @@ -970,19 +970,19 @@ pCodri MakeTcpDoChoDriver(char *tclArray, SConnection *pCon){ /* username and password */ pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"user",TCL_GLOBAL_ONLY); if(!pPtr){ - SCWrite(pCon,"ERROR: user not found in configuration array for TCP Dornier Chopper", - eError); + SCWrite(pCon,"ERROR: user not found in configuration array for TCP Dornier Chopper", + eError); free(pNew); - free(self); + free(self); return NULL; } strncpy(self->user,pPtr, 131); pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"password",TCL_GLOBAL_ONLY); if(!pPtr){ - SCWrite(pCon,"ERROR: password not found in configuration array for TCP Dornier Chopper", - eError); + SCWrite(pCon,"ERROR: password not found in configuration array for TCP Dornier Chopper", + eError); free(pNew); - free(self); + free(self); return NULL; } strncpy(self->pword,pPtr, 131); @@ -992,16 +992,16 @@ pCodri MakeTcpDoChoDriver(char *tclArray, SConnection *pCon){ */ pPtr = Tcl_GetVar2(pServ->pSics->pTcl,tclArray,"config",TCL_GLOBAL_ONLY); if(pPtr != NULL){ - self->config = strdup(pPtr); + self->config = strdup(pPtr); } /* initialize some more */ self->parameters = CreateStringDict(); if(self->parameters == NULL || self->controller == NULL){ - SCWrite(pCon,"ERROR: out of memory in MakeTcpDoCho",eError); + SCWrite(pCon,"ERROR: out of memory in MakeTcpDoCho",eError); free(pNew); - free(self); + free(self); return NULL; } self->iRefreshIntervall = 60; @@ -1025,11 +1025,11 @@ pCodri MakeTcpDoChoDriver(char *tclArray, SConnection *pCon){ count = 0; Tcl_DStringAppend(&pars,"updateintervall",15); while(parnames[count] != NULL){ - for(i = 0; i < self->numChoppers; i++){ - snprintf(buffer,131,",%s_%1.1d", parnames[count], i + 1); - Tcl_DStringAppend(&pars,buffer,strlen(buffer)); + for(i = 0; i < self->numChoppers; i++){ + snprintf(buffer,131,",%s_%1.1d", parnames[count], i + 1); + Tcl_DStringAppend(&pars,buffer,strlen(buffer)); } - count++; + count++; } pNew->pParList = strdup(Tcl_DStringValue(&pars)); Tcl_DStringFree(&pars); diff --git a/site_ansto/lakeshore340driv.c b/site_ansto/lakeshore340driv.c index 287b67ef..77883580 100644 --- a/site_ansto/lakeshore340driv.c +++ b/site_ansto/lakeshore340driv.c @@ -1,10 +1,10 @@ /*-------------------------------------------------------------------------- - L A K E S H O R E 3 4 0 D R I V + 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) @@ -50,7 +50,7 @@ typedef struct __EVDriver *pEVDriver; -#include +#include /* Do we need these ? #include #include @@ -65,8 +65,8 @@ pEVDriver CreateLAKESHORE340Driver(int argc, char *argv[]); int ConfigLAKESHORE340(pEVDriver self); - - + + /*-----------------------------------------------------------------------*/ typedef struct { pLAKESHORE340 pData; @@ -85,12 +85,12 @@ { pLAKESHORE340Driv pMe = NULL; int iRet; - + assert(self); pMe = (pLAKESHORE340Driv)self->pPrivate; assert(pMe); - - iRet = LAKESHORE340_Read(&pMe->pData,fPos); + + iRet = LAKESHORE340_Read(&pMe->pData,fPos); if(iRet <= 0 ) { pMe->iLastError = iRet; @@ -103,13 +103,13 @@ return 0; } return 1; - } + } /*----------------------------------------------------------------------------*/ static int LAKESHORE340Run(pEVDriver self, float fVal) { pLAKESHORE340Driv pMe = NULL; int iRet; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -126,7 +126,7 @@ static int LAKESHORE340Error(pEVDriver self, int *iCode, char *error, int iErrLen) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv)self->pPrivate; assert(pMe); @@ -138,7 +138,7 @@ } else { - LAKESHORE340_ErrorTxt(&pMe->pData,pMe->iLastError,error,iErrLen); + LAKESHORE340_ErrorTxt(&pMe->pData,pMe->iLastError,error,iErrLen); } return 1; } @@ -147,7 +147,7 @@ { pLAKESHORE340Driv pMe = NULL; int iRet; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -160,13 +160,13 @@ } return 1; - } + } /*--------------------------------------------------------------------------*/ static int LAKESHORE340Init(pEVDriver self) { pLAKESHORE340Driv pMe = NULL; int iRet; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -183,28 +183,28 @@ { 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); @@ -230,11 +230,11 @@ if(iRet) { return DEVREDO; - } + } else { return DEVFAULT; - } + } break; /* handable protocoll errors */ case EL734__BAD_TMO: @@ -243,19 +243,19 @@ case -501: /* Bad_COM */ return DEVREDO; case -504: /* Badly formatted */ - return DEVREDO; + return DEVREDO; default: return DEVFAULT; - break; + break; } return DEVFAULT; } - + /*--------------------------------------------------------------------------*/ /* static int LAKESHORE340Halt(pEVDriver *self) { assert(self); - + return 1; } */ @@ -263,28 +263,28 @@ 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)); @@ -294,23 +294,23 @@ } 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; @@ -319,20 +319,20 @@ pNew->TryFixIt = LAKESHORE340Fix; pNew->Init = LAKESHORE340Init; pNew->Close = LAKESHORE340Close; - - return pNew; - } + + 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, + iRet = LAKESHORE340_Config(&pMe->pData, pMe->iTmo, pMe->iRead, pMe->iControl,pMe->fDiv,pMe->fMult); if(iRet < 0) { @@ -345,17 +345,17 @@ 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; @@ -364,17 +364,17 @@ 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. */ + /* 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; @@ -383,7 +383,7 @@ int SetTMOLAKESHORE340(pEVDriver self, int iSensor) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -391,7 +391,7 @@ if(iSensor < 10) { return 0; - } + } pMe->iTmo = iSensor; return 1; } @@ -399,7 +399,7 @@ int GetControlLAKESHORE340(pEVDriver self) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -410,7 +410,7 @@ int GetSensorLAKESHORE340(pEVDriver self) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -421,7 +421,7 @@ int GetTMOLAKESHORE340(pEVDriver self) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -432,7 +432,7 @@ float GetDivisorLAKESHORE340(pEVDriver self) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -443,12 +443,12 @@ 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 */ + /* The LAKESHORE340 doesn't need divisor, force to 1.0 */ pMe->fDiv = 1.0; /* fDiv */; return 1; } @@ -456,7 +456,7 @@ float GetMultLAKESHORE340(pEVDriver self) { pLAKESHORE340Driv pMe = NULL; - + assert(self); pMe = (pLAKESHORE340Driv )self->pPrivate; assert(pMe); @@ -467,13 +467,13 @@ 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 */ + + /* The LAKESHORE340 doesn't need multiplier, force to 1.0 */ pMe->fMult = 1.0; /* fDiv */; return 1; } - +