home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bioprn.zip / BIOSPRN.PAS < prev    next >
Pascal/Delphi Source File  |  1992-09-30  |  10KB  |  295 lines

  1. {$IFDEF Windows}
  2.   !! ERROR - This unit is not compatible with Windows !!
  3. {$ENDIF}
  4.  
  5. {$S-,R-,V-,I-,F+,O+,A-}
  6.  
  7. {$I OPDEFINE.INC}                 {!!.13}
  8.  
  9. {*********************************************************}
  10. {*                  BIOSPRN.PAS 1.01                     *}
  11. {*        Copyright (c) TurboPower Software 1989.        *}
  12. {*                 All rights reserved.                  *}
  13. {*********************************************************}
  14.  
  15. {
  16.  This unit supplements the BiosPrinter support in OPPRNLOW. It implements the
  17.  object type BiosPrinterWithRetry. This object enhances BiosPrinter's error
  18.  handling by maintaining seperate printer test and success mask values for
  19.  printer status and put character calls. It also adds retry and timeout
  20.  capabilities.  See BIOSPRN.DOC for more information.
  21.  
  22.  BiosPrinterWithRetry is derived from BiosPrinter (and therefore
  23.  FlexiblePrinter and BasePrinter).
  24. }
  25. Unit BiosPrn;
  26. interface
  27.  
  28. uses
  29.   Dpmi,      {!!.20}
  30.   Dos,
  31.   OpConst,   {!!.20}
  32.   OpRoot,
  33.   OpPrnLow;
  34.  
  35. type
  36.   BiosPrinterWithRetryPtr = ^BiosPrinterWithRetry;           {!!.13}
  37.   BiosPrinterWithRetry =
  38.     object(BiosPrinter)
  39.       bpwrPutTest   : Byte;
  40.       bpwrPutMask   : Byte;
  41.       bpwrRetries   : Word;
  42.       bpwrTimeOut   : LongInt;
  43.       constructor Init(LPTNumber : LPTType);
  44.       constructor InitCustom(LPTNumber : LPTType;
  45.                              PrinterTestNumber, SuccessMask : Byte);
  46.       constructor InitDeluxe(LPTNumber : LPTType;
  47.                              StatusTestNumber, StatusMask,
  48.                              PutTestNumber, PutMask : Byte;
  49.                              Retries : Word;
  50.                              TimeOut : LongInt);
  51.         {-Create a BiosPrinterWithRetry instance with custom parameters}
  52.       procedure PrnPutChar(Character : Char); Virtual;
  53.         {-Puts a character to the output device}
  54.  
  55.       procedure SetTestAndMaskCustom(StatusTestNo, StatusMask,
  56.                                      PutTestNo, PutMask : Byte);
  57.         {-Set the printer test and success mask for Status and Put calls}
  58.       procedure GetTestAndMaskCustom(var StatusTestNo, StatusMask,
  59.                                      PutTestNo, PutMask : Byte);
  60.         {-Return the printer test and success mask for Status and Put calls}
  61.       procedure SetRetryAndTimeOut(Retries : Word; TimeOut : LongInt);
  62.         {-Set Retry and timeout values}
  63.       procedure GetRetryAndTimeOut(var Retries : Word; var TimeOut : LongInt);
  64.         {-Return Retry and timeout values}
  65.       function PrnXlatErrorCode(Call : PrnCallType;
  66.                              ErrorCode : Word) : Word; Virtual;
  67.         {-translate a raw error code into appropriate user error code}
  68.  
  69.     {$IFDEF UseStreams}                         {!!.13}
  70.       {...Streams...}
  71.       constructor Load(var S : IdStream);
  72.         {-Load a BiosPrinter from a stream}
  73.       procedure Store(var S : IdStream);
  74.         {-Store a BiosPrinter in a stream}
  75.     {$ENDIF}                                    {!!.13}
  76.     end;
  77.  
  78. implementation
  79.  
  80. const
  81.   TicsPerDay = 1573038;      {Assumes 18.20646 tics/sec}
  82.  
  83. type
  84.   {For calculating timeouts}
  85.   EventTimer = record
  86.     StartTics : LongInt;
  87.     ExpireTics : LongInt;
  88.   end;
  89.  
  90. var
  91.   BiosTics : ^LongInt {absolute $40:$6C};      {!!.20}
  92.  
  93.   procedure NewEvent(var ET : EventTimer; Ticks : LongInt);
  94.     {-Returns a set EventTimer}
  95.   begin
  96.     with ET do begin
  97.       StartTics := BiosTics^; {!!.20}
  98.       ExpireTics := StartTics + Ticks;
  99.     end;
  100.   end;
  101.  
  102.   function CheckEvent(ET : EventTimer) : Boolean;
  103.     {-Returns True if ET has expired}
  104.   var
  105.     CurTics : LongInt;
  106.   begin
  107.     with ET do begin
  108.       {Get current tics; assume timer has expired}
  109.       CurTics := BiosTics^; {!!.20}
  110.       CheckEvent := False;
  111.  
  112.       {Check normal expiration}
  113.       if CurTics > ExpireTics then
  114.         Exit;
  115.       {Check wrapped CurTics}
  116.       if (CurTics < StartTics) and ((CurTics + TicsPerDay) > ExpireTics) then
  117.         Exit;
  118.  
  119.       {If we get here, timer hasn't expired yet}
  120.       CheckEvent := True;
  121.     end;
  122.   end;
  123.  
  124.   constructor BiosPrinterWithRetry.Init(LPTNumber : LPTType);
  125.  
  126.   begin
  127.     if not BiosPrinter.Init(LPTNumber) then
  128.       Fail;
  129.     bpwrPutTest := bpPrinterTest;
  130.     bpwrPutMask := bpSuccessMask;
  131.     bpwrRetries := 0;
  132.     bpwrTimeOut := 0;
  133.   end;
  134.  
  135.   constructor BiosPrinterWithRetry.InitCustom(LPTNumber : LPTType;
  136.                                            PrinterTestNumber,
  137.                                            SuccessMask : Byte);
  138.   begin
  139.     if not BiosPrinter.InitCustom(LPTNumber, PrinterTestNumber,
  140.                                   SuccessMask) then
  141.       Fail;
  142.     bpwrPutTest         := bpPrinterTest;
  143.     bpwrPutMask         := bpSuccessMask;
  144.     bpwrRetries := 0;
  145.     bpwrTimeOut := 0;
  146.   end;
  147.  
  148.   constructor BiosPrinterWithRetry.InitDeluxe(LPTNumber : LPTType;
  149.                                            StatusTestNumber, StatusMask,
  150.                                            PutTestNumber, PutMask : Byte;
  151.                                            Retries : Word;
  152.                                            TimeOut : LongInt);
  153.   begin
  154.     if not BiosPrinter.InitCustom(LPTNumber, StatusTestNumber,
  155.                                   StatusMask) then
  156.       Fail;
  157.     bpwrPutTest   := PutTestNumber;
  158.     bpwrPutMask   := PutMask;
  159.     bpwrRetries   := Retries;
  160.     bpwrTimeOut   := TimeOut;
  161.   end;
  162.  
  163. {$IFDEF UseStreams}
  164.   constructor BiosPrinterWithRetry.Load(var S : IdStream);
  165.  
  166.   begin
  167.     if not BiosPrinter.Load(S) then
  168.       Fail;
  169.     S.Read(bpwrPutTest, (SizeOf(Byte) * 2) + SizeOf(Word) + SizeOf(LongInt));
  170.   end;
  171.  
  172.   procedure BiosPrinterWithRetry.Store(var S : IdStream);
  173.  
  174.   begin
  175.     BiosPrinter.Store(S);
  176.     if S.PeekStatus <> 0 then
  177.       Exit;
  178.     S.Write(bpwrPutTest, (SizeOf(Byte) * 2) + SizeOf(Word) + SizeOf(LongInt));
  179.   end;
  180. {$ENDIF}
  181.  
  182.   procedure BiosPrinterWithRetry.GetTestAndMaskCustom(var StatusTestNo,
  183.                                                        StatusMask,
  184.                                                        PutTestNo,
  185.                                                        PutMask : Byte);
  186.   begin
  187.     BiosPrinter.GetTestAndMask(StatusTestNo, StatusMask);
  188.     PutTestNo := bpwrPutTest;
  189.     PutMask   := bpwrPutMask;
  190.   end;
  191.  
  192.   procedure BiosPrinterWithRetry.SetTestAndMaskCustom(StatusTestNo,
  193.                                                    StatusMask,
  194.                                                    PutTestNo,
  195.                                                    PutMask : Byte);
  196.   begin
  197.     BiosPrinter.SetTestAndMask(StatusTestNo, StatusMask);
  198.     bpwrPutTest := PutTestNo;
  199.     bpwrPutMask := PutMask;
  200.   end;
  201.   procedure BiosPrinterWithRetry.SetRetryAndTimeOut(Retries : Word;
  202.                                                     TimeOut : LongInt);
  203.     {-Set Retry and timeout values}
  204.   begin
  205.     bpwrRetries := Retries;
  206.     bpwrTimeOut := TimeOut;
  207.   end;
  208.  
  209.   procedure BiosPrinterWithRetry.GetRetryAndTimeOut(var Retries : Word;
  210.                                                     var TimeOut : LongInt);
  211.     {-Return Retry and timeout values}
  212.   begin
  213.     Retries := bpwrRetries;
  214.     TimeOut := bpwrTimeOut;
  215.   end;
  216.  
  217.   function BiosPrinterWithRetry.PrnXlatErrorCode(Call : PrnCallType;
  218.                                               ErrorCode : Word) : Word;
  219.     {-translate a raw error code into appropriate user error code}
  220.   begin
  221.     if @fpXlatPrim <> Nil then begin
  222.       PrnXlatErrorCode := fpXlatPrim(Call, ErrorCode);
  223.       Exit;
  224.     end;
  225.     PrnXlatErrorCode := 0;
  226.     case Call of
  227.       StatusCall :
  228.         case bpPrinterTest of
  229.           0 : begin end;                            {always succeed}
  230.           1 : if not PrnTest1Prim(ErrorCode) then       {test 1}
  231.                 PrnXlatErrorCode := PrinterNotReady;
  232.           2 : if not PrnTest2Prim(ErrorCode) then       {test 2}
  233.                 PrnXlatErrorCode := PrinterNotReady;
  234.           3 : if PrnTest3Prim(ErrorCode) then
  235.                 PrnXlatErrorCode := PrinterNotReady;
  236.           4 :                                       {test 4}
  237.               if (Byte(ErrorCode) and bpSuccessMask) <> bpSuccessMask then
  238.                 PrnXlatErrorCode := PrinterNotReady;
  239.           else
  240.             PrnXlatErrorCode := 255;  {special code indicating invalid test}
  241.         end;
  242.       PutCall :
  243.         case bpwrPutTest of
  244.           0 : begin end;                            {always succeed}
  245.           1 : if not PrnTest1Prim(ErrorCode) then       {test 1}
  246.                 PrnXlatErrorCode := PrinterNotReady;
  247.           2 : if not PrnTest2Prim(ErrorCode) then       {test 2}
  248.                 PrnXlatErrorCode := PrinterNotReady;
  249.           3 : if PrnTest3Prim(ErrorCode) then
  250.                 PrnXlatErrorCode := PrinterNotReady;
  251.           4 :                                       {test 4}
  252.               if (Byte(ErrorCode) and bpwrPutMask) <> bpwrPutMask then
  253.                 PrnXlatErrorCode := PrinterNotReady;
  254.  
  255.           else
  256.             PrnXlatErrorCode := 255;  {special code indicating invalid test}
  257.         end;
  258.     end;
  259.   end;
  260.  
  261.   procedure BiosPrinterWithRetry.PrnPutChar(Character : Char);
  262.     {-Puts a character to the output device}
  263.   var
  264.     I : Word;
  265.     ErrorCode : Word;
  266.     Timer : EventTimer;
  267.  
  268.   begin
  269.     {if number of retries = 0 then use Timeout value instead}
  270.     if bpwrRetries = 0 then begin
  271.       NewEvent(Timer, bpwrTimeOut);
  272.       repeat
  273.         ErrorCode := PrnStatus;
  274.       until (ErrorCode = 0) or (not CheckEvent(Timer));
  275.     end
  276.     else begin
  277.       if bpwrRetries = $FFFF then
  278.         I := bpwrRetries
  279.       else
  280.         I := Succ(bpwrRetries);
  281.       ErrorCode := 1;
  282.       while (I > 0) and (ErrorCode <> 0) do begin
  283.         ErrorCode := PrnStatus;
  284.         Dec(I);
  285.       end;
  286.     end;
  287.     {if printer is ready then send the character}
  288.     if ErrorCode = 0 then
  289.       FlexiblePrinter.PrnPutChar(Character);
  290.   end;
  291.  
  292. begin                                    {!!.20}
  293.   BiosTics := Ptr(BiosDataSele, $6C);    {!!.20}
  294. end.
  295.