- Modified SPS core to use rs232controller instead of the obsolete

asynsrv server from David Maden
This commit is contained in:
koennecke
2009-11-16 10:58:30 +00:00
parent 8618a31a31
commit 09826fb097

108
sps.c
View File

@ -12,13 +12,19 @@
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 <string.h>
#include <math.h>
#include <tcl.h>
#include "fortify.h"
#include "hardsup/serialsinq.h"
#include "sics.h"
#include "rs232controller.h"
#include "sics.h"
#include "lld.h"
#include "bit.h"
@ -47,24 +53,24 @@ static int init(pSPS self)
assert(self);
assert(self->pHost);
prs232 rs232;
/* check if in simulation mode */
if (self->iMode)
return 1;
/* open the port */
iRet =
SerialOpen(&self->pData, self->pHost, self->iPort, self->iChannel);
if ((iRet != 1) || (self->pData == NULL)) {
self->iLastError = COMMDEAD;
return 0;
rs232 = createRS232(self->pHost,self->iPort);
self->pData = rs232;
iRet = initRS232(rs232);
if(iRet != 1){
self->iLastError = COMMDEAD;
return 0;
}
/* configure terminators */
SerialATerm(&self->pData, "1\r\n");
SerialSendTerm(&self->pData, "\r\n");
SerialConfig(&self->pData, 5000);
setRS232SendTerminator(rs232,"\r\n");
setRS232ReplyTerminator(rs232,"\n");
setRS232Timeout(rs232,10000);
/* setRS232Debug(rs232,1); */
return 1;
}
@ -74,18 +80,26 @@ static int SPSCommand(pSPS self, char *pCommand, char *pReply,
{
int iRet, i;
char pError[132];
prs232 rs232;
assert(self);
assert(pCommand);
assert(pReply);
rs232 = (prs232)self->pData;
/*
try at least three times to get the command through before
giving up
*/
for (i = 0; i < 3; i++) {
iRet = SerialWriteRead(&self->pData, pCommand, pReply, iReplyLen);
if (iRet != 1) {
if(availableRS232(rs232)){
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) {
case TIMEOUT:
self->iLastError = COMMTMO;
@ -93,10 +107,10 @@ static int SPSCommand(pSPS self, char *pCommand, char *pReply,
break;
default:
self->iLastError = iRet;
SerialError(iRet, pError, 131);
getRS232Error(iRet,pError,131);
WriteToCommandLog("SYS: SPS-TROUBLE:", pError);
SerialClose(&self->pData);
iRet = init(self);
closeRS232(rs232);
iRet = initRS232(rs232);
if (iRet == 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 0;
@ -470,17 +475,24 @@ void SPSAddPermission(pSPS self, int iByte, int iBit, int iRights)
void RemoveSPS(void *pData)
{
pSPS self = NULL;
prs232 rs232 = NULL;
self = (pSPS) pData;
if (!self)
return;
rs232 = (prs232)self->pData;
if (self->pHost) {
free(self->pHost);
}
SerialClose(&self->pData);
if(rs232 != NULL){
closeRS232(rs232);
KillRS232(rs232);
}
if (self->pDes) {
DeleteDescriptor(self->pDes);
}
@ -538,7 +550,7 @@ int SPSFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
/* get port number */
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iVal);
if (iRet != TCL_OK) {
sprintf(pBueffel,
snprintf(pBueffel,255,
"ERROR: expected integer argument for port, got %s",
argv[3]);
SCWrite(pCon, pBueffel, eError);
@ -550,7 +562,7 @@ int SPSFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
/* get channel number */
iRet = Tcl_GetInt(pSics->pTcl, argv[4], &iVal);
if (iRet != TCL_OK) {
sprintf(pBueffel,
snprintf(pBueffel,255,
"ERROR: expected integer argument for channel, got %s",
argv[4]);
SCWrite(pCon, pBueffel, eError);
@ -571,7 +583,7 @@ int SPSFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
/* install command */
iRet = AddCommand(pSics, argv[1], SPSAction, RemoveSPS, pNew);
if (!iRet) {
sprintf(pBueffel, "ERROR: duplicate SPS command %s NOT created",
snprintf(pBueffel,255, "ERROR: duplicate SPS command %s NOT created",
argv[1]);
SCWrite(pCon, pBueffel, eError);
RemoveSPS(pNew);
@ -594,7 +606,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
/* we need at least 3 arguments */
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);
return 0;
}
@ -604,7 +616,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
if (strcmp(argv[1], "push") == 0) { /* operate a button */
/* four arguments needed */
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
@ -612,7 +624,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
/* convert arguments */
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
if (iRet != TCL_OK) {
sprintf(pBueffel,
snprintf(pBueffel,255,
"ERROR: expected integer argument for byte, got %s",
argv[2]);
SCWrite(pCon, pBueffel, eError);
@ -620,7 +632,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
}
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
@ -637,21 +649,21 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
} else if (strcmp(argv[1], "status") == 0) { /* status bits */
/* which bit ? */
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
}
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
}
iRet = SPSGetStatus(self, iByte, &iSet);
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);
return 0;
}
@ -660,7 +672,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
return 1;
} else if (strcmp(argv[1], "stat2") == 0) { /* status bits */
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
@ -668,14 +680,14 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
/* which bit ? */
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
}
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
@ -698,7 +710,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
/* which ADC ? */
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
@ -723,7 +735,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
/* we need lots of parameters */
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]);
SCWrite(pCon, pBueffel, eError);
return 0;
@ -731,7 +743,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
/* convert arguments */
iRet = Tcl_GetInt(pSics->pTcl, argv[2], &iByte);
if (iRet != TCL_OK) {
sprintf(pBueffel,
snprintf(pBueffel,255,
"ERROR: expected integer argument for byte, got %s",
argv[2]);
SCWrite(pCon, pBueffel, eError);
@ -739,7 +751,7 @@ int SPSAction(SConnection * pCon, SicsInterp * pSics, void *pData,
}
iRet = Tcl_GetInt(pSics->pTcl, argv[3], &iBit);
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]);
SCWrite(pCon, pBueffel, eError);
return 0;