ABSPOSAW ; IHS/FCS/DRS - Modem - wait for char,str ; [ 09/12/2002 10:06 AM ]
;;1.0;PHARMACY POINT OF SALE;**3**;JUN 21, 2001;Build 38
Q
;
; Utilities for waiting for certain characters or strings
; Beware, WAITOK, WAITSTR have different return values!
; WAITOK(DIALOUT,TIMEOUT)
; WAITSTR(DIALOUT,STRING,TIMEOUT)
; WAITCHAR(DIALOUT,CHAR,TIMEOUT)
; WAIT1(STRING,TIMEOUT) ; on current device
; WAIT2(CHARSTRING,TIMEOUT) ; on current device
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;
; WAITOK(DIALOUT,TIMEOUT)
; Wait for the "OK" response to a modem command
; Returns true if received, false if timed out.
;
WAITOK(DIALOUT,TIMEOUT) ; returns 1 if you got "OK", 0 if you didn't,
N IO S IO=$$IO^ABSPOSA(DIALOUT)
N RETVAL
I $$WAITSTR(DIALOUT,"OK",$S($G(TIMEOUT):TIMEOUT,1:20)) D S RETVAL=1
.D LOG^ABSPOSL($T(+0)_" - MODEM - WAITOK - Success",$G(ECHO))
E D S RETVAL=0
.D LOG^ABSPOSL($T(+0)_" - MODEM - WAITOK - Failure",$G(ECHO))
Q:$Q RETVAL Q
;
;
; WAITSTR(DIALOUT,STRING,TIMEOUT)
; Wait for some expected string.
; TIMEOUT defaults to 60 seconds.
; Returns 0 if string was received, success
; Returns -1 if NOT received, failure.
;
WAITSTR(DIALOUT,STR,TIMEOUT) ;EP - wait for a given string
; returns 0 if okay, nonzero if not received
N IO,RETVAL S IO=$$IO^ABSPOSA(DIALOUT) U IO
D LOG^ABSPOSL($T(+0)_" - MODEM - Waiting for string "_STR,$G(ECHO))
N RET S RET=$$WAIT1(STR,$S($G(TIMEOUT):TIMEOUT,1:60))
N X S X=$T(+0)_" - MODEM - WAITSTR - "
I RET D S RETVAL=0
. S X=X_"Received expected "_STR
. D LOG^ABSPOSL(X,$G(ECHO))
E D S RETVAL=-1
. S X=X_"Did NOT receive expected "_STR
. N RECD S RECD=$P(RET,U,2,$L(RET,U))
. I RECD="" S X=X_" - Received nothing"
. D LOG^ABSPOSL(X,$G(ECHO))
. I RECD]"" D
. . N I F I=$L(RECD):-1:1 Q:$L(RECD)>250 D
. . . I $E(RECD,I)?1C D
. . . . S RECD=$E(RECD,1,I-1)_"\"_$A(RECD,I)_"\"_$E(RECD,I+1,$L(RECD))
. . S X=$T(+0)_" - MODEM - WAITSTR - Received instead: "_RECD
. . D LOG^ABSPOSL(X,$G(ECHO))
Q:$Q RETVAL Q
;
; WAIT1(STRING,TIMEOUT,MAXCHAR)
; Wait for the given string.
; Returns true for success, false for failure.
; Returns 0^$ZE if error happened (most likely disconnect).
; (Note! Different return value types than WAITSTR has!)
;
; To protect against terminal server overflowing with $C(0),
; we impose MAXCHAR, default 3000 characters
;
WAIT1(WAITTXT,TIMEOUT,MAXCHAR) ;
;returns 1 for success 0 for failure (diff. from WAITSTR)
; Appended to the return value: "^" and the message it received
N START,END,FLAG,CHAR,MSG,TIMEOUTA,X,NCHAR
I '$D(MAXCHAR) S MAXCHAR=3000
S TIMEOUTA=0 ; counts up to TIMEOUT and then you've got a real timeout
;Read input buffer until WAITTXT has been received or timeout
S (FLAG,MSG)=""
S X="W1ZE^"_$T(+0),@^%ZOSF("TRAP")
F NCHAR=1:1:MAXCHAR D Q:TIMEOUTA'<TIMEOUT!(FLAG'="")
.S CHAR=""
.R *CHAR:1 S TIMEOUTA=$S($T:0,1:TIMEOUTA+1) Q:TIMEOUTA
.S:CHAR>127 CHAR=CHAR-128 ; 7-Bit communications
.S:$L(MSG)=255 MSG=$E(MSG,1,254)
.S MSG=MSG_$C(CHAR)
.S FLAG=$S(MSG[WAITTXT:1,1:"")
Q $S(MSG[WAITTXT:1,1:0)_U_MSG
W1ZE D LOGZE("WAIT1") Q 0_U_$$ZE^ABSPOS
;
; Waiting for a particular control charcter
WAITCHAR(DIALOUT,CHARS,TIMEOUT) ;EP -
; returns 0 if okay, nonzero if not received
I '$G(TIMEOUT) S TIMEOUT=60
N IO,RETVAL S IO=$$IO^ABSPOSA(DIALOUT) U IO
N X S X=$T(+0)_" - MODEM - Waiting for"
I $L(CHARS)>1 S X=X_" any of"
S X=X_":"
N I F I=1:1:$L(CHARS) S X=X_" $C("_$A(CHARS,I)_")"
S X=X_" timeout "_$G(TIMEOUT)
D LOG^ABSPOSL(X,$G(ECHO))
S RETVAL=$$WAIT2(CHARS,TIMEOUT)
N X S X=$T(+0)_" - MODEM - WAITCHAR - "
I RETVAL]"" D
. S X=X_"Received "_RETVAL
. D LOG^ABSPOSL(X,$G(ECHO))
. I RETVAL="EOT" D D HANGUP^ABSPOSAB(DIALOUT) S RETVAL="+++"
. . D LOG^ABSPOSL($T(+0)_" - MODEM - Received EOT from host")
E D
. S X=X_"Did NOT receive what was expected."
. D LOG^ABSPOSL(X,$G(ECHO))
Q:$Q RETVAL Q
;---------------------------------------------------------------------
;Monitors a port and waits for particular control characters within a
;specified time frame
; WCHARS - String of control characters
; TIMEOUT - Time frame (in seconds) - at least 1
; Returns "STX" or "ETX" or "EOT" or "ENQ" or "ACK" or "NAK" or ""
; or it may return "RUNAWAY"
;--------------------------------------------------------------------
WAIT2(WCHARS,TIMEOUT) ;
N I,MAXI,START,END,FLAG,ACH,CCH,EOT,TIMEOUTA,X
S EOT=$C(4),TIMEOUTA=0
I WCHARS'[EOT S WCHARS=WCHARS_EOT ; always be on the lookout for EOT
S FLAG="",MAXI=3000
S X="W2ZE^"_$T(+0),@^%ZOSF("TRAP")
F I=1:1:MAXI D Q:TIMEOUTA'<TIMEOUT!(FLAG'="")
. S (ACH,CCH)=""
. R *ACH:1 S TIMEOUTA=$S($T:0,1:TIMEOUTA+1) Q:TIMEOUTA
. S:ACH>127 ACH=ACH-128 Q:'ACH
. S CCH=$C(ACH)
. S:WCHARS[CCH FLAG=ACH
I I=MAXI Q "RUNAWAY" ; runaway byte stream?!
;D LOG^ABSPOSL("TEMPORARY: Stopped with END="_END_",$H="_$H_",FLAG="_FLAG)
Q $P(",STX,ETX,EOT,ENQ,ACK,,,,,,,,,,,,,,,NAK",",",FLAG)
W2ZE D LOGZE("WAIT2") Q ""
LOGZE(WHERE) D LOG^ABSPOSL($T(+0)_" - MODEM - "_WHERE_" - $ZE="_$$ZE^ABSPOS) Q
ABSPOSAW ; IHS/FCS/DRS - Modem - wait for char,str ; [ 09/12/2002 10:06 AM ]
+1 ;;1.0;PHARMACY POINT OF SALE;**3**;JUN 21, 2001;Build 38
+2 QUIT
+3 ;
+4 ; Utilities for waiting for certain characters or strings
+5 ; Beware, WAITOK, WAITSTR have different return values!
+6 ; WAITOK(DIALOUT,TIMEOUT)
+7 ; WAITSTR(DIALOUT,STRING,TIMEOUT)
+8 ; WAITCHAR(DIALOUT,CHAR,TIMEOUT)
+9 ; WAIT1(STRING,TIMEOUT) ; on current device
+10 ; WAIT2(CHARSTRING,TIMEOUT) ; on current device
+11 ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+12 ;
+13 ;
+14 ; WAITOK(DIALOUT,TIMEOUT)
+15 ; Wait for the "OK" response to a modem command
+16 ; Returns true if received, false if timed out.
+17 ;
WAITOK(DIALOUT,TIMEOUT) ; returns 1 if you got "OK", 0 if you didn't,
+1 NEW IO
SET IO=$$IO^ABSPOSA(DIALOUT)
+2 NEW RETVAL
+3 IF $$WAITSTR(DIALOUT,"OK",$SELECT($GET(TIMEOUT):TIMEOUT,1:20))
Begin DoDot:1
+4 DO LOG^ABSPOSL($TEXT(+0)_" - MODEM - WAITOK - Success",$GET(ECHO))
End DoDot:1
SET RETVAL=1
+5 IF '$TEST
Begin DoDot:1
+6 DO LOG^ABSPOSL($TEXT(+0)_" - MODEM - WAITOK - Failure",$GET(ECHO))
End DoDot:1
SET RETVAL=0
+7 IF $QUIT
QUIT RETVAL
QUIT
+8 ;
+9 ;
+10 ; WAITSTR(DIALOUT,STRING,TIMEOUT)
+11 ; Wait for some expected string.
+12 ; TIMEOUT defaults to 60 seconds.
+13 ; Returns 0 if string was received, success
+14 ; Returns -1 if NOT received, failure.
+15 ;
WAITSTR(DIALOUT,STR,TIMEOUT) ;EP - wait for a given string
+1 ; returns 0 if okay, nonzero if not received
+2 NEW IO,RETVAL
SET IO=$$IO^ABSPOSA(DIALOUT)
USE IO
+3 DO LOG^ABSPOSL($TEXT(+0)_" - MODEM - Waiting for string "_STR,$GET(ECHO))
+4 NEW RET
SET RET=$$WAIT1(STR,$SELECT($GET(TIMEOUT):TIMEOUT,1:60))
+5 NEW X
SET X=$TEXT(+0)_" - MODEM - WAITSTR - "
+6 IF RET
Begin DoDot:1
+7 SET X=X_"Received expected "_STR
+8 DO LOG^ABSPOSL(X,$GET(ECHO))
End DoDot:1
SET RETVAL=0
+9 IF '$TEST
Begin DoDot:1
+10 SET X=X_"Did NOT receive expected "_STR
+11 NEW RECD
SET RECD=$PIECE(RET,U,2,$LENGTH(RET,U))
+12 IF RECD=""
SET X=X_" - Received nothing"
+13 DO LOG^ABSPOSL(X,$GET(ECHO))
+14 IF RECD]""
Begin DoDot:2
+15 NEW I
FOR I=$LENGTH(RECD):-1:1
IF $LENGTH(RECD)>250
QUIT
Begin DoDot:3
+16 IF $EXTRACT(RECD,I)?1C
Begin DoDot:4
+17 SET RECD=$EXTRACT(RECD,1,I-1)_"\"_$ASCII(RECD,I)_"\"_$EXTRACT(RECD,I+1,$LENGTH(RECD))
End DoDot:4
End DoDot:3
+18 SET X=$TEXT(+0)_" - MODEM - WAITSTR - Received instead: "_RECD
+19 DO LOG^ABSPOSL(X,$GET(ECHO))
End DoDot:2
End DoDot:1
SET RETVAL=-1
+20 IF $QUIT
QUIT RETVAL
QUIT
+21 ;
+22 ; WAIT1(STRING,TIMEOUT,MAXCHAR)
+23 ; Wait for the given string.
+24 ; Returns true for success, false for failure.
+25 ; Returns 0^$ZE if error happened (most likely disconnect).
+26 ; (Note! Different return value types than WAITSTR has!)
+27 ;
+28 ; To protect against terminal server overflowing with $C(0),
+29 ; we impose MAXCHAR, default 3000 characters
+30 ;
WAIT1(WAITTXT,TIMEOUT,MAXCHAR) ;
+1 ;returns 1 for success 0 for failure (diff. from WAITSTR)
+2 ; Appended to the return value: "^" and the message it received
+3 NEW START,END,FLAG,CHAR,MSG,TIMEOUTA,X,NCHAR
+4 IF '$DATA(MAXCHAR)
SET MAXCHAR=3000
+5 ; counts up to TIMEOUT and then you've got a real timeout
SET TIMEOUTA=0
+6 ;Read input buffer until WAITTXT has been received or timeout
+7 SET (FLAG,MSG)=""
+8 SET X="W1ZE^"_$TEXT(+0)
SET @^%ZOSF("TRAP")
+9 FOR NCHAR=1:1:MAXCHAR
Begin DoDot:1
+10 SET CHAR=""
+11 READ *CHAR:1
SET TIMEOUTA=$SELECT($TEST:0,1:TIMEOUTA+1)
IF TIMEOUTA
QUIT
+12 ; 7-Bit communications
IF CHAR>127
SET CHAR=CHAR-128
+13 IF $LENGTH(MSG)=255
SET MSG=$EXTRACT(MSG,1,254)
+14 SET MSG=MSG_$CHAR(CHAR)
+15 SET FLAG=$SELECT(MSG[WAITTXT:1,1:"")
End DoDot:1
IF TIMEOUTA'<TIMEOUT!(FLAG'="")
QUIT
+16 QUIT $SELECT(MSG[WAITTXT:1,1:0)_U_MSG
W1ZE DO LOGZE("WAIT1")
QUIT 0_U_$$ZE^ABSPOS
+1 ;
+2 ; Waiting for a particular control charcter
WAITCHAR(DIALOUT,CHARS,TIMEOUT) ;EP -
+1 ; returns 0 if okay, nonzero if not received
+2 IF '$GET(TIMEOUT)
SET TIMEOUT=60
+3 NEW IO,RETVAL
SET IO=$$IO^ABSPOSA(DIALOUT)
USE IO
+4 NEW X
SET X=$TEXT(+0)_" - MODEM - Waiting for"
+5 IF $LENGTH(CHARS)>1
SET X=X_" any of"
+6 SET X=X_":"
+7 NEW I
FOR I=1:1:$LENGTH(CHARS)
SET X=X_" $C("_$ASCII(CHARS,I)_")"
+8 SET X=X_" timeout "_$GET(TIMEOUT)
+9 DO LOG^ABSPOSL(X,$GET(ECHO))
+10 SET RETVAL=$$WAIT2(CHARS,TIMEOUT)
+11 NEW X
SET X=$TEXT(+0)_" - MODEM - WAITCHAR - "
+12 IF RETVAL]""
Begin DoDot:1
+13 SET X=X_"Received "_RETVAL
+14 DO LOG^ABSPOSL(X,$GET(ECHO))
+15 IF RETVAL="EOT"
Begin DoDot:2
+16 DO LOG^ABSPOSL($TEXT(+0)_" - MODEM - Received EOT from host")
End DoDot:2
DO HANGUP^ABSPOSAB(DIALOUT)
SET RETVAL="+++"
End DoDot:1
+17 IF '$TEST
Begin DoDot:1
+18 SET X=X_"Did NOT receive what was expected."
+19 DO LOG^ABSPOSL(X,$GET(ECHO))
End DoDot:1
+20 IF $QUIT
QUIT RETVAL
QUIT
+21 ;---------------------------------------------------------------------
+22 ;Monitors a port and waits for particular control characters within a
+23 ;specified time frame
+24 ; WCHARS - String of control characters
+25 ; TIMEOUT - Time frame (in seconds) - at least 1
+26 ; Returns "STX" or "ETX" or "EOT" or "ENQ" or "ACK" or "NAK" or ""
+27 ; or it may return "RUNAWAY"
+28 ;--------------------------------------------------------------------
WAIT2(WCHARS,TIMEOUT) ;
+1 NEW I,MAXI,START,END,FLAG,ACH,CCH,EOT,TIMEOUTA,X
+2 SET EOT=$CHAR(4)
SET TIMEOUTA=0
+3 ; always be on the lookout for EOT
IF WCHARS'[EOT
SET WCHARS=WCHARS_EOT
+4 SET FLAG=""
SET MAXI=3000
+5 SET X="W2ZE^"_$TEXT(+0)
SET @^%ZOSF("TRAP")
+6 FOR I=1:1:MAXI
Begin DoDot:1
+7 SET (ACH,CCH)=""
+8 READ *ACH:1
SET TIMEOUTA=$SELECT($TEST:0,1:TIMEOUTA+1)
IF TIMEOUTA
QUIT
+9 IF ACH>127
SET ACH=ACH-128
IF 'ACH
QUIT
+10 SET CCH=$CHAR(ACH)
+11 IF WCHARS[CCH
SET FLAG=ACH
End DoDot:1
IF TIMEOUTA'<TIMEOUT!(FLAG'="")
QUIT
+12 ; runaway byte stream?!
IF I=MAXI
QUIT "RUNAWAY"
+13 ;D LOG^ABSPOSL("TEMPORARY: Stopped with END="_END_",$H="_$H_",FLAG="_FLAG)
+14 QUIT $PIECE(",STX,ETX,EOT,ENQ,ACK,,,,,,,,,,,,,,,NAK",",",FLAG)
W2ZE DO LOGZE("WAIT2")
QUIT ""
LOGZE(WHERE) DO LOG^ABSPOSL($TEXT(+0)_" - MODEM - "_WHERE_" - $ZE="_$$ZE^ABSPOS)
QUIT