home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / wmseri / wcom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-05  |  3.5 KB  |  152 lines

  1. //
  2. //     W M S E R I A L - Windows Modem Example
  3. //
  4. // This is only part of a multiple source project.  To build the
  5. // executable please use the makefile provided or construct
  6. // a project file containing the files and settings listed in
  7. // PROJECT.TXT.
  8. //
  9. // Remember to modify the directories to reflect your configuration.
  10. //
  11. //                                                     - J A A
  12.  
  13. // WCOM.C
  14.  
  15. #include <windows.h>
  16. #include <string.h>        // for strncat
  17. #include <mem.h>           // for memmove
  18. #include "wmserial.h"
  19.  
  20. static HWND hComPortWnd;
  21. static HANDLE hWriteBuffer;
  22. static LPSTR szWriteBuffer;
  23.  
  24.  
  25.  
  26. int InitComPort(HWND hWnd, LPSTR szCom)
  27.    {
  28.    DCB dcb;
  29.    char szSettings[25];
  30.    int nResult;
  31.  
  32.    // Save hWnd
  33.    hComPortWnd = hWnd;
  34.  
  35.    // Open Com Port
  36.    int nComID = OpenComm(szCom,QBUFFSIZE,QBUFFSIZE);
  37.    if (nComID < 0)
  38.        return nComID;
  39.  
  40.    lstrcpy(szSettings,szCom);
  41.    lstrcat(szSettings,":");
  42.    lstrcat(szSettings,BAUDRATE);
  43.    lstrcat(szSettings,",");
  44.    lstrcat(szSettings,PARITY);
  45.    lstrcat(szSettings,",");
  46.    lstrcat(szSettings,DATABITS);
  47.    lstrcat(szSettings,",");
  48.    lstrcat(szSettings,STOPBITS);
  49.    
  50.    nResult = BuildCommDCB(szSettings,&dcb);
  51.    if (nResult < 0)
  52.        return nResult;
  53.  
  54.    dcb.Id = nComID;
  55.  
  56.    // Set Com Port
  57.    nResult = SetCommState(&dcb);
  58.    if (nResult < 0)
  59.        return nResult;
  60.  
  61.    // Allocate Write Buffer
  62.    hWriteBuffer = GlobalAlloc( GMEM_MOVEABLE, QBUFFSIZE );
  63.    if (!hWriteBuffer)
  64.        {
  65.        MessageBeep(MB_ICONEXCLAMATION);
  66.        MessageBox(hWnd,"Not enough memory for initializing modem.","Example Windows Terminal",MB_ICONSTOP);
  67.        PostMessage(hWnd,WM_CLOSE,0,0L);
  68.        return NULL;
  69.        }
  70.    szWriteBuffer = (LPSTR) GlobalLock(hWriteBuffer);
  71.    szWriteBuffer[0] = 0;
  72.  
  73.    return nComID;
  74.    }
  75.  
  76.  
  77.  
  78. int CloseComPort(int nComID)
  79.    {
  80.    CloseComm(nComID);
  81.    if (hWriteBuffer)
  82.        GlobalUnlock(hWriteBuffer);
  83.    return NULL;
  84.    }
  85.  
  86.  
  87.  
  88. int ReadComPort(int nComID, LPSTR szDest, int nMaxLength)
  89.    {
  90.    COMSTAT ComStat;
  91.    int nTotal = 0;
  92.    int nResult = NULL;
  93.  
  94.    // It's usually a good idea to clear the in queue before
  95.    // quitting.  Windows will not send another WM_COMMNOTIFY
  96.    // if your application does not reduce the queue below
  97.    // the threshold after you get a notice.  
  98.    // If this seems backwards to you, you should know that 
  99.    // it does to me, too.
  100.    do
  101.        {
  102.        CheckComPort(nComID,&ComStat);
  103.  
  104.        // Check for overflow
  105.        if (nTotal + ComStat.cbInQue > nMaxLength)
  106.            break;
  107.  
  108.        nResult = ReadComm(nComID,&(szDest[nTotal]),ComStat.cbInQue);
  109.  
  110.        // Check for error
  111.        if (nResult < 0)
  112.            {
  113.            nTotal = -nTotal - nResult;
  114.            break;
  115.            }
  116.  
  117.        nTotal += nResult;
  118.        CheckComPort(nComID,&ComStat);
  119.        } while (ComStat.cbInQue);
  120.  
  121.    return nTotal;
  122.    }
  123.  
  124.  
  125.  
  126. int WriteComPort(int nComID,LPSTR szData,int nLength)
  127.    {
  128.    int nResult;
  129.  
  130.    if (szData != NULL) 
  131.        strncat(szWriteBuffer,szData,nLength);
  132.  
  133.    nResult = WriteComm(nComID,szWriteBuffer,lstrlen(szWriteBuffer));
  134.  
  135.    if ( nResult > 0 )
  136.        szWriteBuffer[0] = 0;
  137.    else if (nResult < 0)
  138.        {
  139.        memmove(szWriteBuffer,&(szWriteBuffer[-nResult]),
  140.                lstrlen(szWriteBuffer)-nResult);
  141.        }
  142.    return nResult;
  143.    }
  144.  
  145.  
  146.  
  147. int CheckComPort(int nComID,COMSTAT *pComStat)
  148.    {
  149.    return GetCommError(nComID,pComStat);
  150.    }
  151.  
  152.