home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / wmseri / wmodem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-05  |  3.9 KB  |  168 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. // WMODEM.C
  14.  
  15. #include <windows.h>
  16. #include <string.h>    // for strstr
  17. #include "wmserial.h"
  18.  
  19.  
  20. char ModemResponse[13][20] = 
  21.    {
  22.     "OK\r\n",
  23.     "CONNECT\r\n",
  24.     "RING\r\n",
  25.     "NO CARRIER\r\n",
  26.     "ERROR\r\n",
  27.     "NO DIAL TONE\r\n",
  28.     "BUSY\r\n",
  29.     "NO ANSWER\r\n",
  30.     "CONNECT 1200\r\n",
  31.     "CONNECT 2400\r\n",
  32.     "CONNECT 4800\r\n",
  33.     "CONNECT 9600\r\n",
  34.     "CONNECT 14400\r\n"
  35.    };
  36.  
  37. static HWND hModemWnd;
  38.  
  39.    
  40. BOOL InitModem(HWND hWnd, int nComID)
  41.    {
  42.    int nResult;
  43.    BOOL bReturnValue;
  44.    HANDLE hBufferMem;
  45.    LPSTR szModemResponse;
  46.    char szInitString[] = "ATZ\r";
  47.  
  48.    // Save hWnd
  49.    hModemWnd = hWnd;
  50.  
  51.    // Get memory for response
  52.    hBufferMem = GlobalAlloc( GMEM_MOVEABLE, QBUFFSIZE );
  53.    if (!hBufferMem)
  54.        {
  55.        MessageBeep(MB_ICONEXCLAMATION);
  56.        MessageBox(hWnd,"Not enough memory for initializing modem.","Example Windows Terminal",MB_ICONSTOP);
  57.        SendMessage(hWnd,WM_CLOSE,0,0L);
  58.        return FALSE;
  59.        }
  60.  
  61.    szModemResponse = (LPSTR) GlobalLock(hBufferMem);
  62.  
  63.    // Send Initialization String
  64.    nResult = WriteComPort(nComID,szInitString,lstrlen(szInitString));
  65.    TTYWriteScreen(szInitString);
  66.    CheckComPort(nComID,NULL);
  67.    if (nResult < 0)
  68.        {
  69.        GlobalUnlock(hBufferMem);
  70.        return FALSE;
  71.        }
  72.  
  73.    // Get the response
  74.    nResult = GetModemResponse(nComID,szModemResponse,QBUFFSIZE,30);
  75.    CheckComPort(nComID,NULL);
  76.    if (nResult <= 0) 
  77.        {
  78.        GlobalUnlock(hBufferMem);
  79.        return FALSE;
  80.        }
  81.    else
  82.        TTYWriteScreen(szModemResponse);
  83.  
  84.    if (strstr(szModemResponse,"OK\r\n"))
  85.        bReturnValue = TRUE;
  86.    else
  87.        bReturnValue = FALSE;
  88.  
  89.    GlobalUnlock(hBufferMem);
  90.    return bReturnValue;
  91.    }
  92.  
  93.  
  94.  
  95. int HangUpModem(int nComID)
  96.    {
  97.    DWORD StartTick;
  98.    char szCloseString[] = "ATH0\r";
  99.  
  100.    // Force polled communication
  101.    EnableCommNotification(nComID,NULL,NULL,NULL);
  102.  
  103.    // Get back to command mode.
  104.    StartTick = GetTickCount();
  105.    while (StartTick+1000 > GetTickCount());
  106.    WriteComPort(nComID,"+++",3);
  107.    StartTick = GetTickCount();
  108.    while (StartTick+1000 > GetTickCount());
  109.  
  110.    // Send Hangup String
  111.    WriteComPort(nComID,szCloseString,lstrlen(szCloseString));
  112.  
  113.    return NULL;
  114.    }
  115.  
  116.  
  117.  
  118. int GetModemResponse(int nComID, LPSTR szDest, int nMaxLength, int Wait)
  119.    {
  120.    int nResult;
  121.    int CurrIndex, i;
  122.    DWORD StartTick;
  123.    BOOL Match = FALSE;
  124.  
  125.    // Force polled communication
  126.    EnableCommNotification(nComID,NULL,NULL,NULL);
  127.  
  128.    CurrIndex = 0;
  129.    szDest[0] = 0;
  130.    StartTick = GetTickCount();
  131.    do
  132.        {
  133.        nResult = ReadComPort(nComID, &szDest[CurrIndex], nMaxLength-lstrlen(szDest));
  134.        if (nResult < 0)
  135.            {
  136.            CurrIndex -= nResult;
  137.            szDest[CurrIndex] = 0;
  138.            }
  139.        else if (nResult > 0)
  140.            {
  141.            CurrIndex += nResult;
  142.            szDest[CurrIndex] = 0;
  143.  
  144.            // Quit if acceptable response in stringtable
  145.            for (i = 0; i < 13; i++)
  146.                {
  147.                if ( strstr(szDest,ModemResponse[i]) )
  148.                    {
  149.                    Match = TRUE;
  150.                    break;
  151.                    }
  152.                }
  153.            }
  154.        } while (!Match && nResult >= 0 && 
  155.                 StartTick + (DWORD) Wait * 1000 > GetTickCount() );
  156.  
  157.    if (nResult >= 0 && !Match)
  158.        MessageBox(hModemWnd,"Modem Timed Out.","Example Windows Terminal",MB_OK);
  159.  
  160.    if (nResult < 0)
  161.        return -lstrlen(szDest);
  162.    else
  163.        return lstrlen(szDest);
  164.    }
  165.  
  166.  
  167.  
  168.