home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / SLIDCO.ZIP / PMCOMDRV.C < prev    next >
C/C++ Source or Header  |  1993-02-02  |  9KB  |  345 lines

  1. #define INCL_DOS
  2. #define INCL_ERRORS
  3. #define INCL_DOSDEVIOCTL
  4. #define INCL_DOSDEVICES
  5. #define INCL_PM
  6. #include <os2.h>
  7. #include <process.h>
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stddef.h>
  12. #include <string.h>
  13.  
  14.  
  15. #include "protype.h"
  16. #include "sliddlg.h"
  17. #include "extern.h"
  18.  
  19. #define PRTBUFSZE  50
  20.  
  21. PBYTE pbBase;
  22. HQUEUE hqTermWrt;
  23.  
  24. ULONG AcutalWritten;
  25.  
  26.  
  27. #define STACK_SIZE         8096
  28. #define FOURKBLKSIZE       16000
  29. #define TERM_WRITE_QUE     "\\queues\\termwrt.que"
  30.  
  31.  
  32.  
  33. /***************************************************************************/
  34. /***************************************************************************/
  35. void InitTermDriver(void)
  36. {
  37.  
  38.    CreateTermWrtQueue();
  39.  
  40.    Create_Threads();
  41.  
  42.    Allocate_Buffer_Memory();
  43.  
  44.  
  45. }   
  46.  
  47.  
  48. /***************************************************************************/
  49. /*                                                                          */
  50. /***************************************************************************/
  51. void   Allocate_Buffer_Memory(void)
  52. {
  53.    APIRET rc;
  54.    UCHAR   PrintBuf[PRTBUFSZE];
  55.  
  56.    //Get a chuck of memory off the heap
  57.    rc = DosAllocMem ( (PVOID *) &pbBase, FOURKBLKSIZE, fALLOC);
  58.    if (rc)
  59.       {
  60.       sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  61.       WinDebugMsg(PrintBuf,hwndMain);
  62.       }
  63.    //Set this memory so we can sub alloc any size block
  64.    rc = DosSubSetMem (pbBase, DOSSUB_INIT, FOURKBLKSIZE);
  65.    if (rc)
  66.       {
  67.       sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  68.       WinDebugMsg(PrintBuf,hwndMain);
  69.       }
  70. }
  71.  
  72. /***************************************************************************/
  73. /*                                                                         */
  74. /***************************************************************************/
  75. void Term_Write_Thread(void * Parm1)
  76. {
  77.  
  78.    UCHAR   PrintBuf[PRTBUFSZE];
  79.  
  80.    APIRET rc;
  81.    REQUESTDATA Request;
  82.    ULONG    DataLength;
  83.    PVOID    DataAddress;
  84.    ULONG      ElementCode;
  85.    BOOL32    NoWait;
  86.    BYTE       ElemPriority;
  87.    HEV      SemHandle;
  88.  
  89.    HQUEUE   QueueHandle;
  90.    PID      OwnerPID;
  91.  
  92.  
  93.    ULONG BytesWritten;
  94.  
  95.    BUFFERFORMAT * BuffPtr;
  96.  
  97.    ElementCode = 0;
  98.    NoWait = 0;
  99.    SemHandle = 0;
  100.  
  101.    rc = DosOpenQueue (&OwnerPID,   &QueueHandle,   TERM_WRITE_QUE);
  102.    if (rc)
  103.       {
  104.       sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  105.       WinDebugMsg(PrintBuf,hwndMain);
  106.       }
  107.  
  108.  
  109.    while (1)
  110.       {
  111.       rc = DosReadQueue(QueueHandle,
  112.                         &Request,
  113.                         &DataLength,
  114.                         &DataAddress,
  115.                         ElementCode,
  116.                         DCWW_WAIT,
  117.                         &ElemPriority,
  118.                         SemHandle);
  119.       
  120.       if (rc)
  121.          {
  122.          sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  123.          WinDebugMsg(PrintBuf,hwndMain);
  124.          }
  125.  
  126.       BuffPtr = (BUFFERFORMAT *) DataAddress;
  127.  
  128.       while (HandleComm == 0)
  129.          {
  130.          DosSleep(500);
  131.          }
  132.  
  133.  
  134.       rc = DosWrite(HandleComm,
  135.                            BuffPtr->Buff,
  136.                            strlen(BuffPtr->Buff),
  137.                            &BytesWritten);
  138.       if (rc)
  139.          {
  140.          sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  141.          WinDebugMsg(PrintBuf,hwndMain);
  142.          }
  143.       
  144.       rc = DosSubFreeMem( BuffPtr->pbBase,
  145.                           BuffPtr->BuffPtr,
  146.                           BuffPtr->ulSize);
  147.       if (rc)
  148.          {
  149.            sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  150.            WinDebugMsg(PrintBuf,hwndMain);
  151.          }
  152.       }
  153. }
  154.  
  155.  
  156.  
  157. /***************************************************************************/
  158. /*                                                                         */
  159. /***************************************************************************/
  160. void Term_Read_Thread(void * Parm1)
  161. {
  162.    UCHAR   PrintBuf[PRTBUFSZE];
  163.    
  164.    UCHAR Buf[10];
  165.    ULONG BytesRead, BytesWritten,BufferLength;
  166.  
  167.    APIRET rc;
  168.  
  169.    LONG   val;
  170.    char *stopstring;
  171.    static UCHAR ValBuf[20];
  172.    static int idx;
  173.  
  174.    idx = 0;
  175.    while(1)
  176.       {
  177.        while (HandleComm == 0)
  178.          {
  179.          DosSleep(500);
  180.          }
  181.       BufferLength = 1;
  182.       rc = DosRead(HandleComm, Buf, BufferLength, &BytesRead);
  183.       if (rc)
  184.             {
  185.             sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  186.             WinDebugMsg(PrintBuf,hwndMain);
  187.             }
  188.  
  189.       if (BytesRead != 0) //got a char from termial
  190.          {
  191.          if (Buf[0] == '\r')  //got a carriage return
  192.             {
  193.             if ( idx == 0)  //only a carriage return, ignore
  194.                continue;
  195.             ValBuf[idx]= '\0'; //null terminate buffer
  196.             idx = 0;
  197.             val  = strtol(ValBuf,&stopstring ,10);
  198.             WinPostMsg(hwndDlg, WM_TERMINAL_MSG, (MPARAM) val ,(MPARAM) 0 );
  199.             }
  200.          else
  201.             {
  202.             ValBuf[idx++] = Buf[0];
  203.             }
  204.          }
  205.       }
  206. }
  207.  
  208.  
  209.  
  210. /***************************************************************************/
  211. /*                                                                         */
  212. /***************************************************************************/
  213. void Create_Threads(void)
  214. {
  215.    
  216.  
  217.    
  218.     _beginthread( Term_Read_Thread,
  219.                            NULL,
  220.                            STACK_SIZE,
  221.                            NULL);
  222.  
  223.     _beginthread( Term_Write_Thread,
  224.                            NULL,
  225.                            STACK_SIZE,
  226.                            NULL);
  227.  
  228.  
  229.  
  230. }
  231.  
  232.  
  233. /***************************************************************************/
  234. /*                                                                         */
  235. /***************************************************************************/
  236. void CreateTermWrtQueue(void)
  237. {
  238.    APIRET rc;
  239.    UCHAR   PrintBuf[PRTBUFSZE];
  240.  
  241.    rc  = DosCreateQueue(&hqTermWrt, QUE_FIFO | QUE_CONVERT_ADDRESS, TERM_WRITE_QUE);
  242.    if (rc)
  243.       {
  244.       sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  245.       WinDebugMsg(PrintBuf,hwndMain);
  246.       }
  247.  
  248. }
  249. /***************************************************************************/
  250. /*                                                                         */
  251. /***************************************************************************/
  252. BUFFERFORMAT * AllocateMsgBuffer(ULONG BuffSize)
  253. {
  254.    
  255.    UCHAR   PrintBuf[PRTBUFSZE];
  256.    APIRET  rc;
  257.    ULONG i = 1;
  258.    BUFFERFORMAT * BuffPtr;
  259.    
  260.    rc = DosSubAllocMem(pbBase,
  261.                       (PVOID *) &BuffPtr,
  262.                       BuffSize +(sizeof(BUFFERFORMAT)) );
  263.    if (rc)
  264.          {
  265.          sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  266.          WinDebugMsg(PrintBuf,hwndMain);
  267.          }
  268.  
  269.    BuffPtr->pbBase = pbBase;
  270.    BuffPtr->BuffPtr = (PBYTE) BuffPtr;
  271.    BuffPtr->ulSize = BuffSize +(sizeof(BUFFERFORMAT)) ;
  272.  
  273.    for (i=0; i < BuffSize; i++)
  274.          {
  275.          BuffPtr->Buff[i] = '\0';
  276.          }
  277.  
  278.    return(BuffPtr);
  279. }
  280.  
  281. /****************************************************************************
  282. INPUT: void *ptr must be NULL terminated
  283.  
  284. ACTION:
  285.  
  286.    1.   Allocates a buffer
  287.    2.   Copies the data over   to buffer from calling routine
  288.    3.   Post buffer to write terminal thread
  289. ****************************************************************************/
  290. void PrintTerm(void *ptr)
  291. {
  292.    APIRET  rc;
  293.    ULONG   Request;
  294.    ULONG   ElemPriority;
  295.    ULONG   BytesRead;
  296.    UCHAR   PrintBuf[PRTBUFSZE];
  297.  
  298.    BUFFERFORMAT * BuffPtr;
  299.    ULONG BuffSize;
  300.  
  301.    BuffSize = strlen(ptr)+1;
  302.    
  303.    rc = DosSubAllocMem(pbBase, (PVOID *) &BuffPtr, BuffSize);
  304.    if (rc)
  305.       {
  306.       sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  307.       WinDebugMsg(PrintBuf,hwndMain);
  308.       return;
  309.       }
  310.  
  311.    BuffPtr->pbBase = pbBase;
  312.    BuffPtr->BuffPtr = (PBYTE) BuffPtr;
  313.    BuffPtr->ulSize = BuffSize;
  314.  
  315.  
  316.    strcpy(BuffPtr->Buff, ptr);
  317.  
  318.    BytesRead =  strlen(BuffPtr->Buff);
  319.    BytesRead += sizeof(BUFFERFORMAT);
  320.  
  321.    do
  322.       {
  323.       ElemPriority = 0;
  324.  
  325.       rc = DosWriteQueue(hqTermWrt,
  326.                      Request,
  327.                      BytesRead,
  328.                      BuffPtr,
  329.                      ElemPriority);
  330.  
  331.       if (rc == ERROR_QUE_NO_MEMORY)
  332.          {
  333.          DosSleep(1000);
  334.          }
  335.       } while (rc == ERROR_QUE_NO_MEMORY);
  336.    
  337.    if (rc)
  338.       {
  339.       sprintf(PrintBuf,"RC=%u Line=%u\nFile: %s",rc,__LINE__,__FILE__);
  340.       WinDebugMsg(PrintBuf,hwndMain);
  341.       }
  342. }
  343.  
  344.  
  345.