This commit is contained in:
cvs
2000-04-07 07:09:42 +00:00
parent ff5e8cf0b2
commit b713ae4a93
15 changed files with 310 additions and 145 deletions

View File

@@ -5,7 +5,6 @@
#include <sys/timeb.h>
#include <string.h>
#include <ctype.h>
#include <fortify.h>
#include "err_handling.h"
#include "coc_server.h"
#include "coc_logfile.h"
@@ -13,8 +12,9 @@
#include "tecs_lsc.h"
#include "tecs_dlog.h"
#define TABLE_FILE "lsc.tab"
#define TABLE_FILE "lsci.tab"
#define NO_CODE 999
#define Progress(I) if (configuring) { configuring+=I; }
static SerChannel *ser=NULL;
static char *serverId=NULL;
@@ -33,13 +33,14 @@ typedef struct {
int codDefined; /* code is not yet confirmed */
char ch1[2], ch2[2]; /* channels for high/low T */
char device[16]; /* device name */
} Part;
char tname[16];
} Testpoint;
Part /* C standard guarantees initialization to zero */
Testpoint /* C standard guarantees initialization to zero */
cryo, /* data for main sensors (on heat exchanger, or the only sensors) */
samp, /* data for extra sensors of sample stick */
*parts[2]={&cryo, &samp},
*part=&cryo;
*testpoints[2]={&cryo, &samp},
*testpoint=&cryo;
static float
tempC, /* set T */
@@ -68,7 +69,9 @@ static int
fld, /* field number */
key, /* key status */
serialNo,
resist,
configuring=1,
resist, /* heater resistance */
readTemp, /* client requested readTemp */
cod1, cod2, out1, out2, /* codes read from digital input/output */
iRange, iAmp, /* max. range and max. current code */
per; /* effective period */
@@ -81,12 +84,12 @@ static time_t
static int decod[8]={21,20,17,16,5,4,1,0}; /* for code conversion */
static char
status[132], /* status buffer */
device[32], /* concatenated device names */
buf1[256], buf2[256], buf3[256], buf4[256], /* buffers for temporary use */
head[64], /* curve header */
intype[64], /* input configuration */
chan[2], /* actual channel */
xxxx;
chan[2]; /* actual channel */
static char
*table=NULL, /* environment devices table */
@@ -216,6 +219,7 @@ int instCurve(char *nam, char *channel) {
logfileOut(LOG_MAIN, "download curve %d\n", num);
/* select sensor type first to display sensor units */
ERR_P(LscCmd(ser, "INTYPE [chan]:[intype];DISPFLD [fld],[chan],3;DISPLAY:[maxfld]"));
Progress(1);
n=3;
do {
@@ -227,11 +231,15 @@ int instCurve(char *nam, char *channel) {
if (i!=0) ERR_MSG("can not delete curve");
sprintf(lbuf, "CRVPT %d", num);
i=0;
do { /* download curve */
t=str_split(nbuf, points, '\n');
if (nbuf[0]!='\0') {
ERR_I(str_substitute(buf, nbuf, "#0", lbuf));
ERR_P(LscCmd(ser, buf));
i++;
if (i%10==0) sprintf(status, "downloading curve at line %d", i);
Progress(1);
}
points=t;
} while (t!=NULL);
@@ -239,6 +247,7 @@ int instCurve(char *nam, char *channel) {
/* write header, select curve */
str_upcase(head, chead);
ERR_P(LscCmd(ser, "CRVHDR [num]:[head];INCRV [chan]:[num];DISPFLD [fld],[chan],1"));
Progress(1);
logfileOut(LOG_MAIN, "curve selected on channel %s\n", chan);
saveTime=tim+30;
}
@@ -288,11 +297,11 @@ int configInput() {
char *ext;
retstat=-2; /* errors in following section are severe */
if (part->manual) {
sprintf(buf, ".%s.", part->device);
if (testpoint->manual) {
sprintf(buf, "'%s'", testpoint->device);
} else {
sprintf(buf, "%+d ", part->code);
if (part->code==0) return(0);
sprintf(buf, "%+d ", testpoint->code);
if (testpoint->code==0) return(0);
}
if (table!=NULL && tim>tableTime+60) { free(table); table=NULL; }; /* clear old table */
if (table==NULL) { /* read table */
@@ -305,38 +314,38 @@ int configInput() {
t=strstr(table, buf);
if (t==NULL) ERR_MSG("device not found");
i=sscanf(t, "%79[^\n!]", buf); /* read line */
t=strchr(buf, '.');
if (t==NULL) ERR_MSG("missing '.' in table file");
t=strchr(buf, '\'');
if (t==NULL) ERR_MSG("missing ' in table file");
t++;
n=1;
if (part==&samp) {
if (testpoint==&samp) {
samp.nSens=0;
i=sscanf(t, "%12s%d%d", nam, &nn, &n);
if (i<1) ERR_MSG("missing sensor name");
ext="s";
ext=".s";
} else {
cryo.nSens=0;
tLow=0; tHigh=0;
controlMode=0;
i=sscanf(t, "%12s%d%d%d%f%d%f%f%f", nam, &n, &nn, &controlMode, &tLimit, &resist, &power, &tLow, &tHigh);
if (i<7) ERR_MSG("missing some sensor parameters");
ext="x";
ext=".x";
}
if (n<0 || n>2) ERR_MSG("illegal value for nsensor");
if (n==0) return(0);
if (!part->manual) { /* set device name */
str_copy(part->device, nam);
part->device[strlen(part->device)-1]='\0'; /* strip off '.' */
nam[strlen(nam)-1]='\0'; /* strip off quote */
if (!testpoint->manual) { /* set device name */
str_copy(testpoint->device, nam);
concatDevice();
}
str_append(nam, ext);
ERR_I(retstat=instCurve(nam, part->ch1));
ERR_I(retstat=instCurve(nam, testpoint->ch1));
if (n==2) {
str_append(nam, "l");
ERR_I(retstat=instCurve(nam, part->ch2));
ERR_I(retstat=instCurve(nam, testpoint->ch2));
}
part->nSens=n;
testpoint->nSens=n;
return(0);
OnError: return(retstat);
}
@@ -363,6 +372,7 @@ int loadCache() {
bufi[3]=buf4;
for (i=60; i>21; i-=4) {
sprintf(buf, "CRVHDR?%d>buf1;CRVHDR?%d>buf2;CRVHDR?%d>buf3;CRVHDR?%d>buf4", i, i-1, i-2, i-3);
Progress(1);
ERR_P(LscCmd(ser, buf));
k=0;
for (j=i; j>i-4; j--) {
@@ -434,6 +444,57 @@ int SetTemp(int switchOn) {
}
int ReadTemp() {
char buf[256];
int i;
readTemp=0;
buf[0]='\0';
if (cryo.nSens>0) {
str_append(buf, "KRDG?[cryo.ch1]>cryo.t1;");
if (cryo.nSens>1) {
str_append(buf, "KRDG?[cryo.ch2]>cryo.t2;");
} else {
cryo.t2=0;
}
} else {
cryo.t1=0;
cryo.t2=0;
}
if (samp.nSens>0) {
str_append(buf, "KRDG?[samp.ch1]>samp.t1;");
if (samp.nSens>1) {
str_append(buf, "KRDG?[samp.ch2]>samp.t2;");
} else {
samp.t2=0;
}
} else {
samp.t1=0;
samp.t2=0;
}
i=strlen(buf);
if (i>0) {
buf[i-1]='\0'; /* strip off ';' */
ERR_P(LscCmd(ser, buf));
}
cryo.temp=WeightedAverage(cryo.nSens, cryo.t1, cryo.t2);
samp.temp=WeightedAverage(samp.nSens, samp.t1, samp.t2);
if (samp.temp==0.0) samp.temp=cryo.temp;
if (!deviceFlag
&& !samp.dirty && samp.codDefined && !samp.codChanged
&& !cryo.dirty && cryo.codDefined && !cryo.codChanged) {
configuring=0;
} else if (configuring==0) {
str_copy(status, "configuring");
configuring=1;
}
return(0);
OnError: return(-1);
}
int PeriodicTask() {
char buf[256], lbuf[16];
char *res;
int i, k;
@@ -451,8 +512,12 @@ int ReadTemp() {
ERR_P(LscCmd(ser, "*IDN?>buf1,buf2,serialNo,"));
if (0!=strcmp(buf1, "LSCI") || 0!=strcmp(buf2, "MODEL340") || serialNo==0) return(0);
if (k!=serialNo) {
if (cryo.manual || cryo.code!=0) cryo.dirty=1;
if (samp.manual || samp.code!=0) samp.dirty=1;
if (!configuring) {
str_copy(status, "controller connected");
configuring=1;
}
if (cryo.manual || cryo.code!=0) { cryo.dirty=1; }
if (samp.manual || samp.code!=0) { samp.dirty=1; }
ERR_P(LscCmd(ser, "RANGE:0")); /* switch off heater */
/* reload curve cache: */
if (cache!=NULL) free(cache);
@@ -471,12 +536,8 @@ int ReadTemp() {
noResp=0;
}
ERR_P(LscCmd(ser,
"KRDG?[cryo.ch1]>cryo.t1;KRDG?[cryo.ch2]>cryo.t2;KRDG?[samp.ch1]>samp.t1;KRDG?[samp.ch2]>samp.t2"));
ERR_I(ReadTemp());
cryo.temp=WeightedAverage(cryo.nSens, cryo.t1, cryo.t2);
samp.temp=WeightedAverage(samp.nSens, samp.t1, samp.t2);
if (samp.temp==0.0) samp.temp=cryo.temp;
if (cryo.dirty==0 && samp.dirty==0 && noResp==0 && tim>logTime) {
t2[0]=cryo.temp;
t2[1]=samp.temp;
@@ -512,24 +573,25 @@ int ReadTemp() {
cryo.code1=3*decod[cod2 % 8] ^ 2*decod[cod1 % 8]; /* ^ is exclusive OR */
samp.code1=-(3*decod[cod2 / 8] ^ 2*decod[cod1 / 8]);
for (i=0; i<2; i++) {
part=parts[i];
if (part->code1!=part->code) {
part->code=part->code1;
part->codChanged=1;
testpoint=testpoints[i];
if (testpoint->code1!=testpoint->code) {
testpoint->code=testpoint->code1;
testpoint->codChanged=1;
} else {
if (part->codChanged) {
part->codChanged=0;
if (part->code1==0) {
logfileOut(LOG_MAIN, "unplugged X\n");
if (testpoint->codChanged) {
testpoint->codChanged=0;
if (testpoint->code1==0) {
logfileOut(LOG_MAIN, "%s unplugged\n", testpoint->tname);
} else {
logfileOut(LOG_MAIN, "plugged (%d)\n", part->code1);
logfileOut(LOG_MAIN, "plugged %d on %s\n", testpoint->code1, testpoint->tname);
}
if (part->codDefined) {
part->manual=0;
if (testpoint->codDefined) {
testpoint->manual=0;
}
part->dirty=1;
testpoint->dirty=1;
if (!configuring) configuring=1;
}
part->codDefined=1;
testpoint->codDefined=1;
}
}
}
@@ -550,31 +612,34 @@ int ReadTemp() {
OnError: return(-1);
}
int inputSettings(Part *this) {
int inputSettings(Testpoint *this) {
part=this;
if (part->dirty && samp.codDefined) {
if (busy==0 || cryo.dirty>=0 && part->dirty>=0) { /* do not enter when busy and cryo.dirty/P indicates error on last time */
logfileOut(LOG_MAIN ,"configure inputs for ");
if (part->manual) {
logfileOut(LOG_MAIN ,"%s\n", part->device);
testpoint=this;
if (testpoint->dirty && samp.codDefined) {
if (busy==0 || cryo.dirty>=0 && testpoint->dirty>=0) { /* do not enter when busy and cryo.dirty/P indicates error on last time */
if (testpoint->manual) {
logfileOut(LOG_MAIN ,"configure %s inputs for %s\n", testpoint->tname, testpoint->device);
} else {
logfileOut(LOG_MAIN ,"%+d\n", part->code);
}
if (part->dirty>0) part->try=0;
part->nSens=0;
if (!part->manual) { part->device[0]='\0'; concatDevice(); }
part->dirty=configInput();
if (part->dirty<0) {
part->try++;
if (part->dirty!=-1 || part->try>3) {
logfileShowErr("fatal error P");
part->dirty=0; part->device[0]='\0'; concatDevice();
if (testpoint->code==0) {
logfileOut(LOG_MAIN ,"reset %s inputs\n", testpoint->tname);
} else {
logfileShowErr("try again P");
logfileOut(LOG_MAIN ,"configure %s inputs for code %+d\n", testpoint->tname, testpoint->code);
}
}
if (testpoint->dirty>0) testpoint->try=0;
testpoint->nSens=0;
if (!testpoint->manual) { testpoint->device[0]='\0'; concatDevice(); }
testpoint->dirty=configInput();
if (testpoint->dirty<0) {
testpoint->try++;
if (testpoint->dirty!=-1 || testpoint->try>3) {
logfileShowErr("fatal error");
testpoint->dirty=0; testpoint->device[0]='\0'; concatDevice();
} else {
logfileShowErr("try again");
}
} else {
ERR_P(LscCmd(ser, "ALARM [part.ch1]:[part.nSens],1,[tLimit],0,0,1;ALARM [part.ch2]:0;RELAY 1:1;BEEP:0"));
ERR_P(LscCmd(ser, "ALARM [testpoint.ch1]:[testpoint.nSens],1,[tLimit],0,0,1;ALARM [testpoint.ch2]:0;RELAY 1:1;BEEP:0"));
}
}
}
@@ -589,9 +654,9 @@ int Settings() {
if (cryo.dirty && cryo.codDefined || samp.dirty && samp.codDefined) {
for (i=0; i<2; i++) {
part=parts[i];
if (part->dirty) {
ERR_P(LscCmd(ser, "DISPFLD 2,[part.ch1],1;DISPFLD 4,[part.ch2],1"));
testpoint=testpoints[i];
if (testpoint->dirty) {
ERR_P(LscCmd(ser, "DISPFLD 2,[testpoint.ch1],1;DISPFLD 4,[testpoint.ch2],1"));
}
}
inputSettings(&cryo);
@@ -618,7 +683,7 @@ int Settings() {
}
}
power=pw;
logfileOut(LOG_MAIN, "power %f\n", power, iAmp, iRange);
logfileOut(LOG_INFO, "power %f\n", power, iAmp, iRange);
ERR_P(LscCmd(ser, "CLIMIT 1:[tLimit],0,0,[iAmp],[iRange]"));
tempC=0.001; tShift=0;
ERR_I(SetTemp(1));
@@ -654,6 +719,7 @@ int ExecuteRequest() {
char *t, *res;
struct CocClient *client;
if (readTemp) ReadTemp();
if (tim>auto_remote_time || setFlag) ERR_I(Settings());
if (setFlag) {
if (cryo.nSens>0) {
@@ -674,6 +740,10 @@ int ExecuteRequest() {
client->cmd[0]='\0';
}
if (deviceFlag) {
if (!configuring) {
str_copy(status, "configuring");
configuring=1;
}
t=strchr(device, '/');
if (t==NULL) {
if (0==strcmp(device, "0")) {
@@ -710,8 +780,7 @@ int mainBody(void)
int i, iret, tdif;
struct timeb tim1;
/* read & control temp */
ERR_I(ReadTemp());
ERR_I(PeriodicTask());
if (tim>=auto_remote_time) ERR_I(Settings());
logfileWrite(logMask);
@@ -731,7 +800,7 @@ int mainBody(void)
tim0.time+=i / 1000;
tim0.millitm=i % 1000;
if (tdif>1) {
logfileOut(LOG_MAIN ,"%d cycles lost\n", tdif-1);
logfileOut(LOG_INFO ,"%d cycles lost\n", tdif-1);
}
return(0);
OnError:
@@ -751,21 +820,23 @@ int main(int argc, char *argv[])
char buf[256], opt;
int port, msecTmo;
str_copy(cryo.tname,"main");
str_copy(cryo.ch1,"A");
str_copy(cryo.ch2,"B");
str_copy(samp.tname,"sample stick");
str_copy(samp.ch1,"C");
str_copy(samp.ch2,"D");
cryo.codChanged=1;
samp.codChanged=1;
logMask=LOG_MAIN+LOG_WARN;
logMask=LOG_MAIN+LOG_INFO;
binDir="bin/";
logDir="log/";
serverId="tecs";
host="lnsp26";
port=0;
msecTmo=0;
logfileOut(LOG_MAIN ,"%s ", argv[0]);
logfileOut(LOG_INFO ,"%s ", argv[0]);
for (i=1;i<argc;i++) {
if (argv[i]!=NULL) {
if (argv[i][0]=='-') {
@@ -799,10 +870,10 @@ int main(int argc, char *argv[])
i++;
port=atoi(argv[i]);
} else {
logfileOut(LOG_MAIN ,"?");
logfileOut(LOG_INFO ,"?");
}
if (opt!=' ') logfileOut(LOG_MAIN ,"-%c ", opt);
logfileOut(LOG_MAIN ,"%s ", argv[i]);
if (opt!=' ') logfileOut(LOG_INFO ,"-%c ", opt);
logfileOut(LOG_INFO ,"%s ", argv[i]);
}
}
if (port==0) port=9753;
@@ -811,22 +882,24 @@ int main(int argc, char *argv[])
str_copy(buf, logDir);
str_append(buf, serverId);
str_copy(status, "starting up");
logfileStatusBuf(status);
logfileInit(buf, logIt, use_stdout, logIt);
logfileOut(LOG_MAIN ,"\n");
logfileOut(LOG_INFO ,"\n");
logfileWrite(logMask);
ERR_I(CocInitServer(1024, port));
CocDefStruct(cryo, Part);
CocDefStruct(samp, Part);
CocDefPtr(part, Part);
CocDefStruct(cryo, Testpoint);
CocDefStruct(samp, Testpoint);
CocDefPtr(testpoint, Testpoint);
CocFltFld(Part, temp, CocRD);
CocFltFld(Part, t1, CocRD);
CocFltFld(Part, t2, CocRD);
CocFltFld(Testpoint, temp, CocRD);
CocFltFld(Testpoint, t1, CocRD);
CocFltFld(Testpoint, t2, CocRD);
CocStrFld(Part, ch1, CocRD);
CocStrFld(Part, ch2, CocRD);
CocStrFld(Testpoint, ch1, CocRD);
CocStrFld(Testpoint, ch2, CocRD);
CocDefFlt(htr, CocRD);
CocDefFlt(tempC, setFlag);
@@ -845,6 +918,7 @@ int main(int argc, char *argv[])
CocDefStr(head, CocRD);
CocDefStr(chan, CocRD);
CocDefStr(intype, CocRD);
CocDefStr(status, CocRD);
CocDefInt(cod1, CocRD);
CocDefInt(cod2, CocRD);
@@ -859,9 +933,11 @@ int main(int argc, char *argv[])
CocDefInt(iRange, CocRD);
CocDefInt(mode, CocRD);
CocDefInt(readTemp, CocWR);
CocDefInt(controlMode, CocWR);
CocDefInt(busy, CocRD);
CocDefInt(serialNo, CocRD);
CocDefInt(configuring, CocRD);
CocDefInt(quit, CocWR);
ERR_P(ser=SerOpen(host, msecTmo, idleHdl));
@@ -873,10 +949,10 @@ int main(int argc, char *argv[])
str_append(buf, ".dlog");
iret=DlogOpen(&dset, buf, 1);
if (iret<0) {
logfileOut(LOG_MAIN, "create data log file: %s\n", buf);
logfileOut(LOG_INFO, "create data log file: %s\n", buf);
ERR_I(DlogCreate(&dset, buf, tim0.time, 2, 7*24*60*60/logPeriod, logPeriod, 0.0));
} else {
logfileOut(LOG_MAIN, "opened data log file: %s\n", buf);
logfileOut(LOG_INFO, "opened data log file: %s\n", buf);
}
logfileWrite(logMask);
per=period;