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;
|
} CountDriv, *BeamMon;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int length;
|
||||||
|
char body[8192];
|
||||||
|
} BUFFER;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
prs232 controller;
|
prs232 controller;
|
||||||
int state;
|
int state;
|
||||||
@@ -69,10 +74,11 @@ typedef struct {
|
|||||||
char *host;
|
char *host;
|
||||||
int iPort;
|
int iPort;
|
||||||
float dummy_threshold;
|
float dummy_threshold;
|
||||||
struct { int length; char body[8192]; } buffer;
|
BUFFER buffer;
|
||||||
unsigned long long counter_value;
|
unsigned long long counter_value;
|
||||||
} BeamMon, *pBeamMon;
|
} BeamMon, *pBeamMon;
|
||||||
|
|
||||||
|
static int MonSend(CounterDriver *cntrData, char *pText, char *pReply, int iReplyLen);
|
||||||
|
|
||||||
static void flog(char flag, char* pText) {
|
static void flog(char flag, char* pText) {
|
||||||
FILE* file;
|
FILE* file;
|
||||||
@@ -92,6 +98,7 @@ static void flog(char flag, char* pText) {
|
|||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief Writes a line to the Monitor
|
/** \brief Writes a line to the Monitor
|
||||||
* used for sending commands.
|
* used for sending commands.
|
||||||
*
|
*
|
||||||
@@ -119,14 +126,93 @@ static int MonWrite(pBeamMon self, char* text) {
|
|||||||
return SUCCESS;
|
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
|
//0123456789012345678901234567890
|
||||||
BeamMon* self = NULL;
|
BeamMon* self = NULL;
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
/* TODO better than this */
|
/* 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) {
|
static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
||||||
@@ -138,10 +224,19 @@ static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
|||||||
char* pTerm;
|
char* pTerm;
|
||||||
|
|
||||||
self = (BeamMon *) cntrData->pData;
|
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)) {
|
while (availableRS232(self->controller)) {
|
||||||
len = sizeof(reply);
|
len = sizeof(reply);
|
||||||
iRet = readRS232(self->controller, reply, &len);
|
iRet = MonRead(self, reply, &len);
|
||||||
if (iRet < 0) {
|
if (iRet == FAILURE) {
|
||||||
return iRet;
|
return iRet;
|
||||||
}
|
}
|
||||||
iLen = 0;
|
iLen = 0;
|
||||||
@@ -174,18 +269,19 @@ static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
|||||||
if (strncasecmp(self->buffer.body, ero, sizeof(ero) - 1) == 0)
|
if (strncasecmp(self->buffer.body, ero, sizeof(ero) - 1) == 0)
|
||||||
if (self->state == HWBusy)
|
if (self->state == HWBusy)
|
||||||
self->state = HWPause;
|
self->state = HWPause;
|
||||||
const char rpt[] = "REPORT";
|
const char rpt[] = "READ";
|
||||||
if (strncasecmp(self->buffer.body, rpt, sizeof(rpt) - 1) == 0) {
|
if (strncasecmp(self->buffer.body, rpt, sizeof(rpt) - 1) == 0) {
|
||||||
if (self->state == HWBusy)
|
if (self->state == HWBusy) {
|
||||||
HandleReport(cntrData);
|
HandleReport(cntrData, &self->buffer);
|
||||||
else if (self->state == HWIdle)
|
MonWrite(self, "SICS READ");
|
||||||
MonWrite(self, "SICS REPORT OFF");
|
}
|
||||||
}
|
}
|
||||||
self->buffer.length = 0;
|
self->buffer.length = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief Returns the counter status,
|
/** \brief Returns the counter status,
|
||||||
@@ -202,9 +298,15 @@ static int MonHandleInput(CounterDriver *cntrData, int timeout) {
|
|||||||
*/
|
*/
|
||||||
static int MonGetStatus(CounterDriver *cntrData, float *fControl) {
|
static int MonGetStatus(CounterDriver *cntrData, float *fControl) {
|
||||||
BeamMon *self = NULL;
|
BeamMon *self = NULL;
|
||||||
|
int status;
|
||||||
|
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
MonHandleInput(cntrData, 0);
|
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)
|
if (cntrData->eMode == eTimer)
|
||||||
*fControl = cntrData->fTime;
|
*fControl = cntrData->fTime;
|
||||||
else
|
else
|
||||||
@@ -239,16 +341,20 @@ static int MonStart(CounterDriver *cntrData) {
|
|||||||
cntrData->fTime);
|
cntrData->fTime);
|
||||||
flog('.', str);
|
flog('.', str);
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
|
MonDrainInput(cntrData);
|
||||||
if (cntrData->eMode == eTimer) {
|
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 {
|
} 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",
|
snprintf(str, sizeof(str), "SICS SET TERMINAL=%llu",
|
||||||
(unsigned long long) cntrData->fPreset);
|
(unsigned long long) cntrData->fPreset);
|
||||||
MonWrite(self, str);
|
MonSend(cntrData, str,
|
||||||
MonWrite(self, "SICS START");
|
self->buffer.body, sizeof(self->buffer.body));
|
||||||
MonWrite(self, "SICS REPORT ON");
|
MonSend(cntrData, "SICS START",
|
||||||
|
self->buffer.body, sizeof(self->buffer.body));
|
||||||
self->state = HWBusy;
|
self->state = HWBusy;
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -261,9 +367,13 @@ static int MonPause(CounterDriver *cntrData) {
|
|||||||
BeamMon *self = NULL;
|
BeamMon *self = NULL;
|
||||||
|
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
MonWrite(self, "PAUSE");
|
MonWrite(self, "SICS PAUSE");
|
||||||
self->state = HWPause;
|
self->buffer.length = sizeof(self->buffer.body);
|
||||||
return SUCCESS;
|
if (MonRead(self, self->buffer.body, &self->buffer.length) == SUCCESS) {
|
||||||
|
self->state = HWPause;
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
return FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* \brief Continues a paused counting operation.
|
/* \brief Continues a paused counting operation.
|
||||||
@@ -274,9 +384,13 @@ static int MonContinue(CounterDriver *cntrData) {
|
|||||||
BeamMon *self = NULL;
|
BeamMon *self = NULL;
|
||||||
|
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
MonWrite(self, "CONTINUE");
|
MonWrite(self, "SICS CONTINUE");
|
||||||
self->state = HWBusy;
|
self->buffer.length = sizeof(self->buffer.body);
|
||||||
return SUCCESS;
|
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.
|
/** \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;
|
BeamMon *self = NULL;
|
||||||
|
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
MonWrite(self, "STOP");
|
MonWrite(self, "SICS STOP");
|
||||||
self->state = HWBusy;
|
self->buffer.length = sizeof(self->buffer.body);
|
||||||
return SUCCESS;
|
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.
|
/** \brief Reads the counter and the monitors in the lCounts array.
|
||||||
@@ -300,7 +418,12 @@ static int MonReadValues(CounterDriver *cntrData) {
|
|||||||
|
|
||||||
flog('.', "MonReadValues");
|
flog('.', "MonReadValues");
|
||||||
self = (BeamMon *) cntrData->pData;
|
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;
|
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) {
|
static int MonSend(CounterDriver *cntrData, char *pText, char *pReply, int iReplyLen) {
|
||||||
BeamMon *self = NULL;
|
BeamMon *self = NULL;
|
||||||
|
int status;
|
||||||
|
|
||||||
self = (BeamMon *) cntrData->pData;
|
self = (BeamMon *) cntrData->pData;
|
||||||
|
MonDrainInput(cntrData);
|
||||||
MonWrite(self, pText);
|
MonWrite(self, pText);
|
||||||
/* TODO*/
|
if ((status = MonRead(self, pReply, &iReplyLen)) == SUCCESS)
|
||||||
snprintf(pReply, iReplyLen, "MonSend Response\r\n");
|
{
|
||||||
return SUCCESS;
|
pReply[iReplyLen] = '\0';
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void KillMon(pCounterDriver cntrData) {
|
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) {
|
/*@null@*/ /*@only@*/ static prs232 MonConnect(/*@dependent@*/SConnection *pCon, char *host, int port) {
|
||||||
prs232 controller=NULL;
|
prs232 controller=NULL;
|
||||||
char pError[ERRLEN];
|
char pError[ERRLEN];
|
||||||
int usecTimeout = 50000; /* 50msec timeout */
|
int msecTimeout = 5000; /* 5000msec timeout */
|
||||||
|
|
||||||
controller=createRS232(host,port);
|
controller=createRS232(host,port);
|
||||||
if (controller==NULL) {
|
if (controller==NULL) {
|
||||||
@@ -429,9 +557,9 @@ static void KillMon(pCounterDriver cntrData) {
|
|||||||
KillRS232(controller);
|
KillRS232(controller);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
setRS232ReplyTerminator(controller,"\r\n");
|
setRS232ReplyTerminator(controller,"\n");
|
||||||
setRS232SendTerminator(controller,"\r\n");
|
setRS232SendTerminator(controller,"\r\n");
|
||||||
setRS232Timeout(controller, usecTimeout);
|
setRS232Timeout(controller, msecTimeout);
|
||||||
return controller;
|
return controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user