adjusting interaction with monitor - wip
r1050 | dcl | 2006-08-09 15:29:20 +1000 (Wed, 09 Aug 2006) | 2 lines
This commit is contained in:
@@ -62,6 +62,11 @@ typedef struct __COUNTDRIV {
|
||||
} CountDriv, *BeamMon;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int length;
|
||||
char body[8192];
|
||||
} BUFFER;
|
||||
|
||||
typedef struct {
|
||||
prs232 controller;
|
||||
int state;
|
||||
@@ -69,10 +74,11 @@ typedef struct {
|
||||
char *host;
|
||||
int iPort;
|
||||
float dummy_threshold;
|
||||
struct { int length; char body[8192]; } buffer;
|
||||
BUFFER buffer;
|
||||
unsigned long long counter_value;
|
||||
} BeamMon, *pBeamMon;
|
||||
|
||||
static int MonSend(CounterDriver *cntrData, char *pText, char *pReply, int iReplyLen);
|
||||
|
||||
static void flog(char flag, char* pText) {
|
||||
FILE* file;
|
||||
@@ -92,6 +98,7 @@ static void flog(char flag, char* pText) {
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
/** \brief Writes a line to the Monitor
|
||||
* used for sending commands.
|
||||
*
|
||||
@@ -119,14 +126,93 @@ static int MonWrite(pBeamMon self, char* text) {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static void HandleReport(CounterDriver *cntrData)
|
||||
/** \brief Reads a line from the Monitor
|
||||
* used for sending commands.
|
||||
*
|
||||
* \param *cntrData provides access to a monitor's data
|
||||
* \param *text pointer to NULL terminated text to receive.
|
||||
* \param *pLen inout pointer to length of text to receive.
|
||||
* \return
|
||||
* - SUCCESS
|
||||
* - FAILURE
|
||||
*/
|
||||
static int MonRead(pBeamMon self, char* text, int *pLen) {
|
||||
int i, status, retries=20;
|
||||
|
||||
for (i=0; i<retries; i++) {
|
||||
status = readRS232TillTerm(self->controller, text, pLen);
|
||||
switch (status) {
|
||||
case 1:
|
||||
if (pLen > 0)
|
||||
flog('<', text);
|
||||
return SUCCESS;
|
||||
case TIMEOUT:
|
||||
flog('<', "*TIMEOUT*");
|
||||
self->errorCode = status;
|
||||
continue;
|
||||
default:
|
||||
flog('<', "*ERROR*");
|
||||
self->errorCode = status;
|
||||
return FAILURE;
|
||||
}
|
||||
}
|
||||
flog('<', "***TIMEOUT***");
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
static void HandleReport(CounterDriver *cntrData, BUFFER* bp)
|
||||
{
|
||||
//REPORT 01:45:11.021097 29798167
|
||||
//READ xxxx 00:00:00.000000 0.000000 0 0.00
|
||||
//0123456789012345678901234567890
|
||||
BeamMon* self = NULL;
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
/* TODO better than this */
|
||||
self->counter_value = strtoull(&self->buffer.body[22], NULL, 10);
|
||||
char* cp = &bp->body[26];
|
||||
char* ep;
|
||||
cntrData->fTime = strtod(cp, &ep);
|
||||
cp = ep;
|
||||
switch (toupper(bp->body[5])) {
|
||||
case 'I': /* idle */
|
||||
case 'S': /* stopped */
|
||||
self->state = HWIdle;
|
||||
break;
|
||||
case 'R': /* running */
|
||||
self->state = HWBusy;
|
||||
break;
|
||||
case 'P': /* paused */
|
||||
self->state = HWPause;
|
||||
break;
|
||||
default:
|
||||
self->state = HWFault;
|
||||
break;
|
||||
}
|
||||
if (toupper(bp->body[8] == 'G')) /* Gated */
|
||||
self->state = HWNoBeam;
|
||||
self->counter_value = strtoull(cp, &ep, 10);
|
||||
cntrData->fLastCurrent = self->counter_value;
|
||||
char str[100];
|
||||
snprintf(str, sizeof(str), "READ %s, %f, %f, %f\n",
|
||||
cntrData->eMode == eTimer ? "eTimer" :
|
||||
cntrData->eMode == ePreset ? "ePreset" : "Unknown",
|
||||
cntrData->fPreset,
|
||||
cntrData->fLastCurrent,
|
||||
cntrData->fTime);
|
||||
flog('.', str);
|
||||
}
|
||||
|
||||
static int MonDrainInput(CounterDriver *cntrData) {
|
||||
int iRet, len;
|
||||
char reply[1024];
|
||||
BeamMon* self = NULL;
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
while (availableRS232(self->controller)) {
|
||||
len = sizeof(reply);
|
||||
iRet = MonRead(self, reply, &len);
|
||||
if (iRet == FAILURE) {
|
||||
return iRet;
|
||||
}
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
||||
@@ -138,10 +224,19 @@ static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
||||
char* pTerm;
|
||||
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
#if 0
|
||||
MonWrite(self, "SICS READ");
|
||||
self->buffer.length = sizeof(self->buffer.body);
|
||||
if (MonRead(self, self->buffer.body, &self->buffer.length) == SUCCESS) {
|
||||
HandleReport(cntrData, &self->buffer);
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAILURE;
|
||||
#else
|
||||
while (availableRS232(self->controller)) {
|
||||
len = sizeof(reply);
|
||||
iRet = readRS232(self->controller, reply, &len);
|
||||
if (iRet < 0) {
|
||||
iRet = MonRead(self, reply, &len);
|
||||
if (iRet == FAILURE) {
|
||||
return iRet;
|
||||
}
|
||||
iLen = 0;
|
||||
@@ -174,18 +269,19 @@ static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
||||
if (strncasecmp(self->buffer.body, ero, sizeof(ero) - 1) == 0)
|
||||
if (self->state == HWBusy)
|
||||
self->state = HWPause;
|
||||
const char rpt[] = "REPORT";
|
||||
const char rpt[] = "READ";
|
||||
if (strncasecmp(self->buffer.body, rpt, sizeof(rpt) - 1) == 0) {
|
||||
if (self->state == HWBusy)
|
||||
HandleReport(cntrData);
|
||||
else if (self->state == HWIdle)
|
||||
MonWrite(self, "SICS REPORT OFF");
|
||||
if (self->state == HWBusy) {
|
||||
HandleReport(cntrData, &self->buffer);
|
||||
MonWrite(self, "SICS READ");
|
||||
}
|
||||
}
|
||||
self->buffer.length = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/** \brief Returns the counter status,
|
||||
@@ -202,9 +298,15 @@ static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
||||
*/
|
||||
static int MonGetStatus(CounterDriver *cntrData, float *fControl) {
|
||||
BeamMon *self = NULL;
|
||||
int status;
|
||||
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
MonHandleInput(cntrData, 0);
|
||||
status = MonSend(cntrData, "SICS READ", self->buffer.body, sizeof(self->buffer.body));
|
||||
if (status == SUCCESS) {
|
||||
/* TODO */
|
||||
HandleReport(cntrData, &self->buffer);
|
||||
}
|
||||
if (cntrData->eMode == eTimer)
|
||||
*fControl = cntrData->fTime;
|
||||
else
|
||||
@@ -239,16 +341,20 @@ static int MonStart(CounterDriver *cntrData) {
|
||||
cntrData->fTime);
|
||||
flog('.', str);
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
MonDrainInput(cntrData);
|
||||
if (cntrData->eMode == eTimer) {
|
||||
MonWrite(self, "SICS SET TE_CHECK=TIMER");
|
||||
MonSend(cntrData, "SICS SET TE_CHECK=TIMER",
|
||||
self->buffer.body, sizeof(self->buffer.body));
|
||||
} else {
|
||||
MonWrite(self, "SICS SET TE_CHECK=COUNTER");
|
||||
MonSend(cntrData, "SICS SET TE_CHECK=COUNTER",
|
||||
self->buffer.body, sizeof(self->buffer.body));
|
||||
}
|
||||
snprintf(str, sizeof(str), "SICS SET TERMINAL=%llu",
|
||||
(unsigned long long) cntrData->fPreset);
|
||||
MonWrite(self, str);
|
||||
MonWrite(self, "SICS START");
|
||||
MonWrite(self, "SICS REPORT ON");
|
||||
MonSend(cntrData, str,
|
||||
self->buffer.body, sizeof(self->buffer.body));
|
||||
MonSend(cntrData, "SICS START",
|
||||
self->buffer.body, sizeof(self->buffer.body));
|
||||
self->state = HWBusy;
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -261,9 +367,13 @@ static int MonPause(CounterDriver *cntrData) {
|
||||
BeamMon *self = NULL;
|
||||
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
MonWrite(self, "PAUSE");
|
||||
self->state = HWPause;
|
||||
return SUCCESS;
|
||||
MonWrite(self, "SICS PAUSE");
|
||||
self->buffer.length = sizeof(self->buffer.body);
|
||||
if (MonRead(self, self->buffer.body, &self->buffer.length) == SUCCESS) {
|
||||
self->state = HWPause;
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
/* \brief Continues a paused counting operation.
|
||||
@@ -274,9 +384,13 @@ static int MonContinue(CounterDriver *cntrData) {
|
||||
BeamMon *self = NULL;
|
||||
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
MonWrite(self, "CONTINUE");
|
||||
self->state = HWBusy;
|
||||
return SUCCESS;
|
||||
MonWrite(self, "SICS CONTINUE");
|
||||
self->buffer.length = sizeof(self->buffer.body);
|
||||
if (MonRead(self, self->buffer.body, &self->buffer.length) == SUCCESS) {
|
||||
self->state = HWBusy;
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
/** \brief Cancels a counting operation. This is an emergency stop used when interrupting an operation.
|
||||
@@ -286,9 +400,13 @@ static int MonHalt(CounterDriver *cntrData) {
|
||||
BeamMon *self = NULL;
|
||||
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
MonWrite(self, "STOP");
|
||||
self->state = HWBusy;
|
||||
return SUCCESS;
|
||||
MonWrite(self, "SICS STOP");
|
||||
self->buffer.length = sizeof(self->buffer.body);
|
||||
if (MonRead(self, self->buffer.body, &self->buffer.length) == SUCCESS) {
|
||||
self->state = HWBusy;
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
/** \brief Reads the counter and the monitors in the lCounts array.
|
||||
@@ -300,7 +418,12 @@ static int MonReadValues(CounterDriver *cntrData) {
|
||||
|
||||
flog('.', "MonReadValues");
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
/* TODO*/
|
||||
MonWrite(self, "SICS READ");
|
||||
self->buffer.length = sizeof(self->buffer.body);
|
||||
if (MonRead(self, self->buffer.body, &self->buffer.length) == SUCCESS) {
|
||||
/* TODO*/
|
||||
HandleReport(cntrData, &self->buffer);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -383,12 +506,17 @@ static int MonGet(CounterDriver *cntrData, char *name, int iCter, float *fVal) {
|
||||
|
||||
static int MonSend(CounterDriver *cntrData, char *pText, char *pReply, int iReplyLen) {
|
||||
BeamMon *self = NULL;
|
||||
int status;
|
||||
|
||||
self = (BeamMon *) cntrData->pData;
|
||||
MonDrainInput(cntrData);
|
||||
MonWrite(self, pText);
|
||||
/* TODO*/
|
||||
snprintf(pReply, iReplyLen, "MonSend Response\r\n");
|
||||
return SUCCESS;
|
||||
if ((status = MonRead(self, pReply, &iReplyLen)) == SUCCESS)
|
||||
{
|
||||
pReply[iReplyLen] = '\0';
|
||||
return SUCCESS;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
static void KillMon(pCounterDriver cntrData) {
|
||||
@@ -411,7 +539,7 @@ static void KillMon(pCounterDriver cntrData) {
|
||||
/*@null@*/ /*@only@*/ static prs232 MonConnect(/*@dependent@*/SConnection *pCon, char *host, int port) {
|
||||
prs232 controller=NULL;
|
||||
char pError[ERRLEN];
|
||||
int usecTimeout = 50000; /* 50msec timeout */
|
||||
int msecTimeout = 5000; /* 5000msec timeout */
|
||||
|
||||
controller=createRS232(host,port);
|
||||
if (controller==NULL) {
|
||||
@@ -429,9 +557,9 @@ static void KillMon(pCounterDriver cntrData) {
|
||||
KillRS232(controller);
|
||||
return NULL;
|
||||
}
|
||||
setRS232ReplyTerminator(controller,"\r\n");
|
||||
setRS232ReplyTerminator(controller,"\n");
|
||||
setRS232SendTerminator(controller,"\r\n");
|
||||
setRS232Timeout(controller, usecTimeout);
|
||||
setRS232Timeout(controller, msecTimeout);
|
||||
return controller;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user