home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / hex2prn.zip / hex2prn.c next >
C/C++ Source or Header  |  1992-04-23  |  7KB  |  178 lines

  1. /*
  2.  * hex2prn.c Begun: Tue  04-21-1992        Revised: Wed  04-22-1992
  3.  * M. F. Kaplon    Using IBM C Set 2 vs.6.177 and Toolkit20 Vs.6.304 of OS/2 2.0
  4.  * Send Set Up String to  Printer defined as : LPT1
  5.  * Compile and statically link run time library as:  icc hex2prn.c
  6.  * Compile and dynamically link run time library as: icc /Gd+ hex2prn.c
  7.  * Dynamic link option much smaller(4K vs 23K) but loads a little slower.
  8. */
  9.  
  10. #pragma langlvl(mig)   /* needed to link conio.h - migration libs required */
  11.  
  12. #define INCL_BASE
  13. #define INCL_NOPMAPI
  14. #define INCL_DOSDEVIOCTL
  15.  
  16. #include <os2.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <conio.h>     /* if want unbuffered Keyboard input using getch() */
  20.  
  21. ULONG DosDeviceHandle(BYTE *DevName,ULONG *OpenAction,ULONG FileSize,USHORT FileAttribute,USHORT OpenFlag,USHORT OpenMode,PEAOP2 pEABuf);
  22. void  DosDevErr(char *fname,USHORT categ,USHORT func,ULONG msgnum);
  23. BYTE hex2dec(BYTE *strin);  /* convert 2 char hex symbol to a BYTE */
  24.  
  25.  
  26. /*
  27.  * Global Variables for use in dosDevIOCtl calls in a more general context
  28.  * The variables with first character p are pointers but will be declared as
  29.  * variables or structures and an address passed
  30. */
  31. ULONG   ulCategory = 5;    /* Device Category - Printer */
  32. ULONG   ulFunction ;       /* Device Function */
  33. BYTE    ByteParmBuf;       /* Address of Command Specific Argument BYTE */
  34. ULONG   ulParmLengthMax;   /* Max Length in BYTEs of ParmList */
  35. ULONG   ParmLengthInOut;   /* Address of length in BYTEs passed in Parms on Input and length returned on Output */
  36. ULONG   ulDataLengthMax;   /* Length in BYTES of Data Area */
  37. ULONG   DataLengthInOut;   /* Address of length in BYTEs passed in Data Area on Input and length returned on Output */
  38. BYTE    ByteData;          /* DataBuffer to receive Byte Data */
  39. ULONG   ulrc;              /* Return code */
  40.  
  41. int main(int argc,char **argv)
  42. {
  43.    ULONG   DevHandle;      /* Handle returned by DosOpen or standard device handle */
  44.    ULONG   OpenAction;     /* filled in by call to DosDevHandle */
  45.    USHORT  i;              /* counter for string addressing */
  46.    BYTE    PrnString[96];  /* 290/3 = 96.6 set up string to printer */
  47.    ULONG   BytesWritten;
  48.  
  49.  
  50.    system("cls");          /* clear screen */
  51.  
  52.    if (argc == 1)  {       /* no command line arguments passed */
  53.        DosBeep(500,500);
  54.        printf("\n This program passes printer codes to the printer - it is called as:");
  55.        printf("\n Hex2prn ?? ?? ?? ............ where the ?? are separated by a space.");
  56.        printf("\n Each ?? represents(in Hex format) a one byte component of a printer command ");
  57.        printf("\n If only one symbol is needed it should be prefixed with a 0.");
  58.        printf("\n Up to a total of 96 2-Character Hex combinations may be passed.");
  59.        exit(1);
  60.    }
  61.  
  62.    for (i=1;i<=argc-1;i++)  {  /* go thru the arguments passed */
  63.        strupr(argv[i]);        /* convert each to upper case */
  64.        if ( strlen(argv[i]) != 2)  {
  65.             DosBeep(500,500);
  66.             printf("\nData should be passed as two char Hex sequences, space separated\n");
  67.             exit(1);
  68.        }
  69.        if (!( (*argv[i] >= '0' && *argv[i] <= '9') || (*argv[i] >= 'A' && *argv[i] <= 'F'))) {
  70.             DosBeep(500,500);
  71.             printf("\nIncorrect parameter passed");
  72.             exit(1);
  73.        }
  74.        if (!(( *(argv[i]+1) >= '0' && *(argv[i]+1) <= '9') || (*(argv[i]+1) >= 'A' && *(argv[i]+1) <= 'F'))) {
  75.             DosBeep(500,500);
  76.             printf("\nIncorrect parameter passed");
  77.             exit(1);
  78.        }
  79.        PrnString[i-1] = hex2dec(argv[i]);    /* convert to byte array */
  80.    }
  81.  
  82.    /*  get device handle for printer */
  83.    DevHandle = DosDeviceHandle("LPT1",&OpenAction,0,0,0x11,0x42,NULL);
  84.  
  85.    /* parms for Category 5 - Function 66H Return Printer Status */
  86.    ulFunction       = 0x66;  /* 10010000 = 0x90 is Not Busy and Printer Selected */
  87.    ulParmLengthMax  = 1;
  88.    ParmLengthInOut  = 1;
  89.    ulDataLengthMax  = 1 ;
  90.    DataLengthInOut  = 0;
  91.    ByteParmBuf = 0;
  92.  
  93.    while (1) {
  94.        if ( (ulrc=DosDevIOCtl(DevHandle,ulCategory,ulFunction,&ByteParmBuf,
  95.                           ulParmLengthMax,&ParmLengthInOut,&ByteData,
  96.                           ulDataLengthMax, &DataLengthInOut)) != 0 )
  97.             DosDevErr("DosDevIOCtl",ulCategory,ulFunction,ulrc);
  98.        if (! (ByteData & 16) )  {
  99.             DosBeep(500,500);
  100.             printf("\nPrinter Not On :Press <Esc> to abort or Turn On and Press Any Other Key\n");
  101.             if (getch() == 27)
  102.                 exit(0);
  103.             else
  104.               continue;
  105.        }
  106.        break;
  107.    }
  108.    if (ByteData == 0x90)  /*   printf("\nPrinter Selected and Ready");  */
  109.        ;
  110.    else {
  111.        printf("\nPrinter not ready - Aborting");
  112.        exit(1);
  113.    }
  114.    DosWrite(DevHandle,PrnString,(ULONG)(argc - 1),&BytesWritten);
  115. /*   printf("\nBytes Written = %lu",BytesWritten);  */
  116.  
  117.    DosClose(DevHandle);
  118.  
  119.    return 0;
  120. }
  121.  
  122. /*
  123.  * Returns Dos Device Handle for selected device .
  124.  * Is passed parameters for DosOpen Call
  125.  * BYTE    PrinterId[5] ; Name as "LPT1",etc
  126.  * ULONG   OpenAction;  ; filled in by call
  127.  * ULONG   FileSize = 0;       for Printer
  128.  * USHORT  FileAttribute = 0;  for Printer
  129.  * USHORT  OpenFlag = 0x11;    for Printer
  130.  * USHORT  OpenMode = 0x42 ;   for Printer
  131.  * PEAOP2  pEABuf = NULL;      pointer to extended attribute structure
  132. */
  133. ULONG DosDeviceHandle(BYTE *DevName,ULONG *OpenAction,ULONG FileSize,USHORT FileAttribute,USHORT OpenFlag,USHORT OpenMode,PEAOP2 pEABuf)
  134. {
  135.     ULONG  DeviceHandle;
  136.     ULONG  ulrc;
  137.  
  138.     if ( (ulrc=DosOpen(DevName,&DeviceHandle,OpenAction,FileSize,FileAttribute,OpenFlag,OpenMode,pEABuf))  != 0) {
  139.         DosDevErr("DosOpen Error - Aborting",0,0,ulrc);
  140.         exit(1);
  141.     }
  142. /*    printf("\nDosOpen OK Returns : Device Handle for %s = %lu and OpenAction = %lu\n",DevName,DeviceHandle,OpenAction); */
  143.     return DeviceHandle;
  144. }
  145.  
  146. /*
  147.  * Error Message for function calls non PM API
  148.  * For non DosDevIOCtl category = function = 0
  149. */
  150. void DosDevErr(char *fname,USHORT categ,USHORT func,ULONG msgnum)
  151. {
  152.    printf("\nAborting! %s Category %u  Function %XH  Error_# %lu",fname,categ,func,msgnum);
  153.    printf("\n%s\n",strerror(msgnum));
  154.    exit(1);
  155. }
  156.  
  157. BYTE hex2dec(BYTE *strin)  /* convert 2 char hex symbol to a BYTE */
  158. {
  159.     BYTE   i,res=0;
  160.  
  161.     for (i=0; i <= 1; i++)  {
  162.         if (strin[i] >= '0' && strin[i]<= '9' )  {
  163.             if (i == 1)
  164.                 res = res + strin[i] - 48;
  165.             else
  166.                 res = res + (strin[i] - 48)*16;
  167.         }
  168.         if (strin[i] >= 'A' && strin[i] <= 'F')  {
  169.             if (i == 1)
  170.                res = res + strin[i] - 55;
  171.             else
  172.                res = res + (strin[i] - 55)*16;
  173.         }
  174.     }
  175.     return res;
  176. }
  177.  
  178.