- Modified SPS core to use rs232controller instead of the obsolete
asynsrv server from David Maden
This commit is contained in:
98
sps.c
98
sps.c
@ -12,13 +12,19 @@
|
|||||||
|
|
||||||
Mark Koennnecke, Juli 1998
|
Mark Koennnecke, Juli 1998
|
||||||
|
|
||||||
|
Feature added: bipa command, Mark Koennecke, May 2009
|
||||||
|
|
||||||
|
Reworked to use rs232controller rather then the older serialport code
|
||||||
|
|
||||||
|
Mark Koennecke, November 2009
|
||||||
|
|
||||||
---------------------------------------------------------------------------*/
|
---------------------------------------------------------------------------*/
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <tcl.h>
|
#include <tcl.h>
|
||||||
#include "fortify.h"
|
#include "sics.h"
|
||||||
#include "hardsup/serialsinq.h"
|
#include "rs232controller.h"
|
||||||
#include "sics.h"
|
#include "sics.h"
|
||||||
#include "lld.h"
|
#include "lld.h"
|
||||||
#include "bit.h"
|
#include "bit.h"
|
||||||
@ -47,23 +53,23 @@ static int init(pSPS self)
|
|||||||
|
|
||||||
assert(self);
|
assert(self);
|
||||||
assert(self->pHost);
|
assert(self->pHost);
|
||||||
|
prs232 rs232;
|
||||||
|
|
||||||
/* check if in simulation mode */
|
/* check if in simulation mode */
|
||||||
if (self->iMode)
|
if (self->iMode)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* open the port */
|
rs232 = createRS232(self->pHost,self->iPort);
|
||||||
iRet =
|
self->pData = rs232;
|
||||||
SerialOpen(&self->pData, self->pHost, self->iPort, self->iChannel);
|
iRet = initRS232(rs232);
|
||||||
if ((iRet != 1) || (self->pData == NULL)) {
|
if(iRet != 1){
|
||||||
self->iLastError = COMMDEAD;
|
self->iLastError = COMMDEAD;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
setRS232SendTerminator(rs232,"\r\n");
|
||||||
/* configure terminators */
|
setRS232ReplyTerminator(rs232,"\n");
|
||||||
SerialATerm(&self->pData, "1\r\n");
|
setRS232Timeout(rs232,10000);
|
||||||
SerialSendTerm(&self->pData, "\r\n");
|
/* setRS232Debug(rs232,1); */
|
||||||
SerialConfig(&self->pData, 5000);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -74,18 +80,26 @@ static int SPSCommand(pSPS self, char *pCommand, char *pReply,
|
|||||||
{
|
{
|
||||||
int iRet, i;
|
int iRet, i;
|
||||||
char pError[132];
|
char pError[132];
|
||||||
|
prs232 rs232;
|
||||||
|
|
||||||
assert(self);
|
assert(self);
|
||||||
assert(pCommand);
|
assert(pCommand);
|
||||||
assert(pReply);
|
assert(pReply);
|
||||||
|
|
||||||
|
rs232 = (prs232)self->pData;
|
||||||
/*
|
/*
|
||||||
try at least three times to get the command through before
|
try at least three times to get the command through before
|
||||||
giving up
|
giving up
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < 3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
iRet = SerialWriteRead(&self->pData, pCommand, pReply, iReplyLen);
|
if(availableRS232(rs232)){
|
||||||
if (iRet != 1) {
|
memset(pReply,0, iReplyLen);
|
||||||
|
iRet = iReplyLen;
|
||||||
|
readRS232(rs232, pReply, &iRet);
|
||||||
|
/* printf("Dirt on the SPS cable: %d bytes, text = %s\n", iRet, pReply); */
|
||||||
|
}
|
||||||
|
iRet = transactRS232(rs232, pCommand,strlen(pCommand), pReply, iReplyLen);
|
||||||
|
if (iRet < 0) {
|
||||||
switch (iRet) {
|
switch (iRet) {
|
||||||
case TIMEOUT:
|
case TIMEOUT:
|
||||||
self->iLastError = COMMTMO;
|
self->iLastError = COMMTMO;
|
||||||
@ -93,10 +107,10 @@ static int SPSCommand(pSPS self, char *pCommand, char *pReply,
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
self->iLastError = iRet;
|
self->iLastError = iRet;
|
||||||
SerialError(iRet, pError, 131);
|
getRS232Error(iRet,pError,131);
|
||||||
WriteToCommandLog("SYS: SPS-TROUBLE:", pError);
|
WriteToCommandLog("SYS: SPS-TROUBLE:", pError);
|
||||||
SerialClose(&self->pData);
|
closeRS232(rs232);
|
||||||
iRet = init(self);
|
iRet = initRS232(rs232);
|
||||||
if (iRet == 0) {
|
if (iRet == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -105,15 +119,6 @@ static int SPSCommand(pSPS self, char *pCommand, char *pReply,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now we may still have a TMO or OFL message */
|
|
||||||
if (strstr(pReply, "?TMO") != NULL) {
|
|
||||||
self->iLastError = COMMTMO;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (strstr(pReply, "?OFL") != NULL) {
|
|
||||||
self->iLastError = SPSOFFLINE;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -470,17 +475,24 @@ void SPSAddPermission(pSPS self, int iByte, int iBit, int iRights)
|
|||||||
void RemoveSPS(void *pData)
|
void RemoveSPS(void *pData)
|
||||||
{
|
{
|
||||||
pSPS self = NULL;
|
pSPS self = NULL;
|
||||||
|
prs232 rs232 = NULL;
|
||||||
|
|
||||||
|
|
||||||
self = (pSPS) pData;
|
self = (pSPS) pData;
|
||||||
|
|
||||||
if (!self)
|
if (!self)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
rs232 = (prs232)self->pData;
|
||||||
if (self->pHost) {
|
if (self->pHost) {
|
||||||
free(self->pHost);
|
free(self->pHost);
|
||||||
}
|
}
|
||||||
|
|
||||||
SerialClose(&self->pData);
|
if(rs232 != NULL){
|
||||||
|
closeRS232(rs232);
|
||||||
|
KillRS232(rs232);
|
||||||
|
}
|
||||||
|
|
||||||
if (self->pDes) {
|
if (self->pDes) {
|
||||||
DeleteDescriptor(self->pDes);
|
DeleteDescriptor(self->pDes);
|
||||||
}
|
}
|
||||||
@ -538,7 +550,7 @@ int SPSFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* get port number */
|
/* get port number */
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iVal);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iVal);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel,
|
snprintf(pBueffel,255,
|
||||||
"ERROR: expected integer argument for port, got %s",
|
"ERROR: expected integer argument for port, got %s",
|
||||||
argv[3]);
|
argv[3]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
@ -550,7 +562,7 @@ int SPSFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* get channel number */
|
/* get channel number */
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[4], &iVal);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[4], &iVal);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel,
|
snprintf(pBueffel,255,
|
||||||
"ERROR: expected integer argument for channel, got %s",
|
"ERROR: expected integer argument for channel, got %s",
|
||||||
argv[4]);
|
argv[4]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
@ -571,7 +583,7 @@ int SPSFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* install command */
|
/* install command */
|
||||||
iRet = AddCommand(pSics, argv[1], SPSAction, RemoveSPS, pNew);
|
iRet = AddCommand(pSics, argv[1], SPSAction, RemoveSPS, pNew);
|
||||||
if (!iRet) {
|
if (!iRet) {
|
||||||
sprintf(pBueffel, "ERROR: duplicate SPS command %s NOT created",
|
snprintf(pBueffel,255, "ERROR: duplicate SPS command %s NOT created",
|
||||||
argv[1]);
|
argv[1]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
RemoveSPS(pNew);
|
RemoveSPS(pNew);
|
||||||
@ -594,7 +606,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
|
|
||||||
/* we need at least 3 arguments */
|
/* we need at least 3 arguments */
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
sprintf(pBueffel, "ERROR: need at least two arguments to %s", argv[0]);
|
snprintf(pBueffel,255, "ERROR: need at least two arguments to %s", argv[0]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -604,7 +616,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
if (strcmp(argv[1], "push") == 0) { /* operate a button */
|
if (strcmp(argv[1], "push") == 0) { /* operate a button */
|
||||||
/* four arguments needed */
|
/* four arguments needed */
|
||||||
if (argc < 4) {
|
if (argc < 4) {
|
||||||
sprintf(pBueffel, "ERROR: need at least two arguments to %s push",
|
snprintf(pBueffel,255, "ERROR: need at least two arguments to %s push",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@ -612,7 +624,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* convert arguments */
|
/* convert arguments */
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel,
|
snprintf(pBueffel,255,
|
||||||
"ERROR: expected integer argument for byte, got %s",
|
"ERROR: expected integer argument for byte, got %s",
|
||||||
argv[2]);
|
argv[2]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
@ -620,7 +632,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
}
|
}
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel, "ERROR: expected integer argument for bit, got %s",
|
snprintf(pBueffel,255, "ERROR: expected integer argument for bit, got %s",
|
||||||
argv[3]);
|
argv[3]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@ -637,21 +649,21 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
} else if (strcmp(argv[1], "status") == 0) { /* status bits */
|
} else if (strcmp(argv[1], "status") == 0) { /* status bits */
|
||||||
/* which bit ? */
|
/* which bit ? */
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
sprintf(pBueffel, "ERROR: need at least two arguments to %s push",
|
snprintf(pBueffel,255, "ERROR: need at least two arguments to %s push",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel, "ERROR: expected integer argument for bit, got %s",
|
snprintf(pBueffel, 255, "ERROR: expected integer argument for bit, got %s",
|
||||||
argv[2]);
|
argv[2]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
iRet = SPSGetStatus(self, iByte, &iSet);
|
iRet = SPSGetStatus(self, iByte, &iSet);
|
||||||
if (iRet <= 0) {
|
if (iRet <= 0) {
|
||||||
sprintf(pBueffel, "ERROR: failed to read status bit %d", iByte);
|
snprintf(pBueffel,255, "ERROR: failed to read status bit %d", iByte);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -660,7 +672,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
return 1;
|
return 1;
|
||||||
} else if (strcmp(argv[1], "stat2") == 0) { /* status bits */
|
} else if (strcmp(argv[1], "stat2") == 0) { /* status bits */
|
||||||
if (argc < 4) {
|
if (argc < 4) {
|
||||||
sprintf(pBueffel, "ERROR: need at least two arguments to %s stat2",
|
snprintf(pBueffel,255, "ERROR: need at least two arguments to %s stat2",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@ -668,14 +680,14 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* which bit ? */
|
/* which bit ? */
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel, "ERROR: expected integer argument for bit, got %s",
|
snprintf(pBueffel,255, "ERROR: expected integer argument for bit, got %s",
|
||||||
argv[2]);
|
argv[2]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel, "ERROR: expected integer argument for bit, got %s",
|
snprintf(pBueffel, 255, "ERROR: expected integer argument for bit, got %s",
|
||||||
argv[3]);
|
argv[3]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@ -698,7 +710,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* which ADC ? */
|
/* which ADC ? */
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel, "ERROR: expected integer argument for ADC, got %s",
|
snprintf(pBueffel,255, "ERROR: expected integer argument for ADC, got %s",
|
||||||
argv[2]);
|
argv[2]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@ -723,7 +735,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
|
|
||||||
/* we need lots of parameters */
|
/* we need lots of parameters */
|
||||||
if (argc < 5) {
|
if (argc < 5) {
|
||||||
sprintf(pBueffel, "ERROR: need at least three arguments to %s perm",
|
snprintf(pBueffel,255, "ERROR: need at least three arguments to %s perm",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@ -731,7 +743,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
/* convert arguments */
|
/* convert arguments */
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel,
|
snprintf(pBueffel,255,
|
||||||
"ERROR: expected integer argument for byte, got %s",
|
"ERROR: expected integer argument for byte, got %s",
|
||||||
argv[2]);
|
argv[2]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
@ -739,7 +751,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
}
|
}
|
||||||
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
|
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
sprintf(pBueffel, "ERROR: expected integer argument for bit, got %s",
|
snprintf(pBueffel,255, "ERROR: expected integer argument for bit, got %s",
|
||||||
argv[3]);
|
argv[3]);
|
||||||
SCWrite(pCon, pBueffel, eError);
|
SCWrite(pCon, pBueffel, eError);
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user