- introduced initRS232Finished and initRS232WithFlags

- allow empty terminator in readRS232TillTerm
This commit is contained in:
zolliker
2005-09-05 08:22:13 +00:00
parent 6070ad7b29
commit 69c79b207a
3 changed files with 45 additions and 7 deletions

View File

@ -217,7 +217,7 @@ int readRS232TillTerm(prs232 self, void *data, int *datalen){
replylen = *datalen; replylen = *datalen;
iRet = NETReadTillTermNew(self->pSock,self->timeout,self->replyTerminator, iRet = NETReadTillTermNew(self->pSock,self->timeout,self->replyTerminator,
(char *)data, replylen); (char *)data, replylen);
if(self->debug > 0 && iRet == 1) if(self->debug > 0 && iRet > 0)
{ {
printf("RS232 IN/TERM : %s",(char *)data); printf("RS232 IN/TERM : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL) if(strchr((char *)data,'\n') == NULL)
@ -243,7 +243,11 @@ int readRS232TillTerm(prs232 self, void *data, int *datalen){
{ {
return iRet; return iRet;
} }
*datalen = strlen((char *)data); if (*self->replyTerminator != 0) {
*datalen = strlen((char *)data);
} else {
*datalen = iRet;
}
return 1; return 1;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
@ -328,7 +332,7 @@ int transactRS232(prs232 self, void *send, int sendLen,
reply, replyLen); reply, replyLen);
if(self->debug > 0) if(self->debug > 0)
{ {
if(iRet == 1) if(iRet > 0)
{ {
printf("RS232 IN/TRANS: %s",(char *)reply); printf("RS232 IN/TRANS: %s",(char *)reply);
if(strchr((char *)reply,'\n') == NULL) if(strchr((char *)reply,'\n') == NULL)
@ -407,7 +411,7 @@ int getRS232Timeout(prs232 self){
return self->timeout; return self->timeout;
} }
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
int initRS232(prs232 self) int initRS232WithFlags(prs232 self, int flags)
{ {
int iRet; int iRet;
@ -422,7 +426,7 @@ int initRS232(prs232 self)
free(self->pSock); free(self->pSock);
self->pSock = NULL; self->pSock = NULL;
} }
self->pSock = NETConnect(self->pHost, self->iPort); self->pSock = NETConnectWithFlags(self->pHost, self->iPort, flags);
if(!self->pSock){ if(!self->pSock){
return FAILEDCONNECT; return FAILEDCONNECT;
} else{ } else{
@ -434,6 +438,23 @@ int initRS232(prs232 self)
} }
} }
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
int initRS232(prs232 self)
{
return initRS232WithFlags(self, 0);
}
/*--------------------------------------------------------------------*/
int initRS232Finished(prs232 self)
{
int iret;
iret = NETConnectFinished(self->pSock);
if (iret < 0) {
return FAILEDCONNECT;
} else {
return iret;
}
}
/*--------------------------------------------------------------------*/
void closeRS232(prs232 self) void closeRS232(prs232 self)
{ {
assert(self); assert(self);
@ -453,6 +474,8 @@ prs232 createRS232(char *host, int iPort)
{ {
prs232 pNew = NULL; prs232 pNew = NULL;
assert(iPort > 0);
/* /*
create data structure create data structure
*/ */
@ -512,6 +535,11 @@ prs232 createRS232(char *host, int iPort)
} }
} }
/*-------------------------------------------------------------------*/ /*-------------------------------------------------------------------*/
static void KillAndFreeRS232(void *pData) {
KillRS232(pData);
free(pData);
}
/*-------------------------------------------------------------------*/
static int checkSet(SConnection *pCon, int argc, int rights) static int checkSet(SConnection *pCon, int argc, int rights)
{ {
if(argc < 3) if(argc < 3)
@ -828,12 +856,12 @@ int RS232Factory(SConnection *pCon, SicsInterp *pSics,
/* /*
create the command create the command
*/ */
iRet = AddCommand(pSics,argv[1],RS232Action, KillRS232, pNew); iRet = AddCommand(pSics,argv[1],RS232Action, KillAndFreeRS232, pNew);
if(!iRet) if(!iRet)
{ {
sprintf(pError,"ERROR: duplicate command %s not created", argv[1]); sprintf(pError,"ERROR: duplicate command %s not created", argv[1]);
SCWrite(pCon,pError,eError); SCWrite(pCon,pError,eError);
KillRS232(pNew); KillAndFreeRS232(pNew);
return 0; return 0;
} }
return 1; return 1;

View File

@ -65,6 +65,8 @@
int errorBufferLen); int errorBufferLen);
int getRS232Timeout(prs232 self); int getRS232Timeout(prs232 self);
int initRS232(prs232 self); int initRS232(prs232 self);
int initRS232WithFlags(prs232 self, int flags);
int initRS232Finished(prs232 self);
void closeRS232(prs232 self); void closeRS232(prs232 self);
prs232 createRS232(char *host, int iPort); prs232 createRS232(char *host, int iPort);
void KillRS232(void *pData); void KillRS232(void *pData);

View File

@ -69,6 +69,8 @@ The following interface functions are provided:
int errorBufferLen); int errorBufferLen);
int getRS232Timeout(prs232 self); int getRS232Timeout(prs232 self);
int initRS232(prs232 self); int initRS232(prs232 self);
int initRS232WithFlags(prs232 self, int flags);
int initRS232Finished(prs232 self);
void closeRS232(prs232 self); void closeRS232(prs232 self);
prs232 createRS232(char *host, int iPort); prs232 createRS232(char *host, int iPort);
void KillRS232(void *pData); void KillRS232(void *pData);
@ -103,6 +105,12 @@ directly followed by a read.
iCode. iCode.
\item[initRS232] tries to close and reopen the RS232 connection. This is \item[initRS232] tries to close and reopen the RS232 connection. This is
useful for the automatic fixing of communication problems encountered. useful for the automatic fixing of communication problems encountered.
\item[initRS232WithFlags] the same as initRS232, but with flags. flags = 1:
do not block on connect (use initRS232Finished to check success).
flags=2: wait 1000 ms after last close or restart (workaround for a bug
in Lantronix terminal server). flags=3: both options. flags=0: no opitons.
\item[initRS232Finished] returns 0 when connect in progress, 1 when finished,
FAILEDCONNECT on failure.
\item[closeRS232] closes a network connection but does not delete the datastructure. \item[closeRS232] closes a network connection but does not delete the datastructure.
\item[createRS232] creates a new rs232 data structure with all \item[createRS232] creates a new rs232 data structure with all
parameters at default values. The connection is NOT opened. parameters at default values. The connection is NOT opened.