home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / BRAINW32 / CODE / CH12 / COMMTEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-22  |  2.5 KB  |  99 lines

  1. //***************************************************************
  2. // From the book "Win32 System Services: The Heart of Windows NT"
  3. // by Marshall Brain
  4. // Published by Prentice Hall
  5. //
  6. // Copyright 1994, by Prentice Hall.
  7. //
  8. // This code demonstrates an extremely simple serial connection.
  9. //***************************************************************
  10.  
  11. // commtest.cpp
  12.  
  13. #include <windows.h>
  14. #include <iostream.h>
  15.  
  16. void ErrorHandler(char *message, DWORD error)
  17. {
  18.     cout << message << endl;
  19.     cout << "Error number = " << error << endl;
  20.     ExitProcess(1);
  21. }
  22.  
  23. void main()
  24. {
  25.     HANDLE comHandle;
  26.     BOOL success;
  27.     DCB dcb;
  28.     char str[100];
  29.     DWORD numWrite, numRead;
  30.     COMMTIMEOUTS timeouts;
  31.     
  32.     // Open the comm port. Can open COM, LPT,
  33.     // or \\\\.\\TELNET
  34.     comHandle = CreateFile("COM2", 
  35.         GENERIC_READ|GENERIC_WRITE, 
  36.         0, 0, OPEN_EXISTING,
  37.         FILE_ATTRIBUTE_NORMAL, 0);
  38.     if (comHandle == INVALID_HANDLE_VALUE)
  39.         ErrorHandler("In CreateFile",
  40.             GetLastError());
  41.  
  42.     // Get the current settings of the COMM port 
  43.     success = GetCommState(comHandle, &dcb);
  44.     if (!success) 
  45.         ErrorHandler("In GetCommState",
  46.             GetLastError());
  47.  
  48.    // Modify the baud rate, etc.
  49.     dcb.BaudRate = 2400;
  50.     dcb.ByteSize = 8;
  51.     dcb.Parity = NOPARITY;
  52.     dcb.StopBits = ONESTOPBIT;
  53.  
  54.     // Apply the new comm port settings
  55.     success = SetCommState(comHandle, &dcb);
  56.     if (!success) 
  57.         ErrorHandler("In SetCommState",
  58.             GetLastError());
  59.  
  60.     // Change the ReadIntervalTimeout so that
  61.     // ReadFile will return immediately. See
  62.     // help file
  63.     timeouts.ReadIntervalTimeout = MAXDWORD; 
  64.     timeouts.ReadTotalTimeoutMultiplier = 0;
  65.     timeouts.ReadTotalTimeoutConstant = 0;
  66.     timeouts.WriteTotalTimeoutMultiplier = 0;
  67.     timeouts.WriteTotalTimeoutConstant = 0;
  68.     SetCommTimeouts( comHandle, &timeouts );
  69.  
  70.     // Set the Data Terminal Ready line
  71.     EscapeCommFunction(comHandle, SETDTR);
  72.  
  73.     // Send an "at" command to the modem
  74.     // Be sure to use \r rather than \n
  75.     strcpy(str, "at\r");
  76.     success = WriteFile(comHandle, str, strlen(str),
  77.         &numWrite, 0);
  78.     if (!success) 
  79.         ErrorHandler("In WriteFile", GetLastError());
  80.  
  81.     // Wait 2 seconds and then retrieve from the
  82.     // modem
  83.     Sleep(2000);
  84.     success = ReadFile(comHandle, str,
  85.         100, &numRead, 0);
  86.     if (!success) 
  87.         ErrorHandler("In ReadFile", GetLastError());
  88.  
  89.     // Print the string received
  90.     cout << numRead << endl;
  91.     str[numRead]='\0';
  92.     cout << str << endl;
  93.  
  94.     // Clear the DTR line
  95.     EscapeCommFunction(comHandle, CLRDTR);
  96.  
  97.     CloseHandle(comHandle);  
  98. }
  99.