home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / comm / setcomms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.8 KB  |  112 lines

  1. /*
  2.  *  SetCommState
  3.  *  setcomms.c
  4.  *
  5.  *  This program demonstrates the use of the function SetCommState.
  6.  *  It sets a communication device to the state specified by the 
  7.  *  device control block (DCB) . This function call is necessary
  8.  *  to set the communication to the desired configurations before 
  9.  *  any communication should take place. 
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. /* Procedure called when the application is loaded for the first time */
  16. BOOL WinInit( hInstance )
  17. HANDLE hInstance;
  18. {
  19.     WNDCLASS   wcClass;
  20.  
  21.     /* registering the parent window class */
  22.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  23.     wcClass.hIcon          = LoadIcon (hInstance, (LPSTR)"WindowIcon");
  24.     wcClass.lpszMenuName   = (LPSTR)NULL;
  25.     wcClass.lpszClassName  = (LPSTR)"Setcomms";
  26.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  27.     wcClass.hInstance      = hInstance;
  28.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  29.     wcClass.lpfnWndProc    = DefWindowProc;
  30.     wcClass.cbClsExtra     = 0;
  31.     wcClass.cbWndExtra     = 0;
  32.  
  33.     RegisterClass( (LPWNDCLASS)&wcClass );
  34.     return TRUE;        /* Initialization succeeded */
  35. }
  36.  
  37. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  38. HANDLE hInstance, hPrevInstance;
  39. LPSTR lpszCmdLine;
  40. int cmdShow;
  41. {
  42.     HWND  hWnd;            /* Handle to the parent window               */
  43.     short nCid;            /* Short integer identifying the opened
  44.                             * communication device                      */
  45.     short nFlag;           /* Result of the SetCommState function call  */
  46.     DCB   dcb;             /* The device control block                  */
  47.        
  48.     WinInit( hInstance );
  49.  
  50.     hWnd = CreateWindow((LPSTR)"Setcomms",
  51.                         (LPSTR)"Setting Communication Device",
  52.                         WS_OVERLAPPEDWINDOW,
  53.                         50,                /* x         */
  54.                         50,                /* y         */
  55.                         600,               /* width     */
  56.                         250,               /* height    */
  57.                         (HWND)NULL,        /* no parent */
  58.                         (HMENU)NULL,       /* use class menu */
  59.                         (HANDLE)hInstance, /* handle to window instance */
  60.                         (LPSTR)NULL        /* no params to pass on */
  61.                         );
  62.  
  63.     /* Make window visible according to the way the app is activated */
  64.     ShowWindow( hWnd, cmdShow );
  65.     UpdateWindow( hWnd );
  66.  
  67.     /* attempt to open the com1 port */
  68.     nCid = OpenComm ((LPSTR)"COM1", 20, 20);
  69.     if (nCid < 0) 
  70.       {
  71.         MessageBox (hWnd, (LPSTR)"Com port not opened!!",(LPSTR)NULL,MB_OK); 
  72.         return 0;
  73.       }
  74.  
  75.  
  76.     /* Get the current setting of the communication device and then
  77.      * made modifications to each field of the device control block. 
  78.      */
  79.     if (GetCommState (nCid, (DCB FAR *) &dcb) >= 0)
  80.       { dcb.BaudRate = 9600;
  81.         dcb.ByteSize = 8;
  82.         dcb.StopBits = ONESTOPBIT;
  83.         dcb.Parity   = NOPARITY;
  84.         dcb.fRtsflow = FALSE;
  85.         dcb.fDtrflow = FALSE;
  86.         dcb.fOutX    = FALSE;
  87.         dcb.fInX     = FALSE;
  88.         dcb.fNull    = FALSE;
  89.         dcb.XonLim   = 1;
  90.         dcb.XoffLim  = 20;
  91.       }
  92.  
  93.     nFlag = SetCommState ((DCB FAR *) &dcb);
  94.  
  95.     /* Check the return value of SetCommState to see if the function 
  96.      * was successful
  97.      */
  98.     if (nFlag == 0)
  99.       MessageBox (hWnd, (LPSTR)"Communication device set successfully",
  100.                   (LPSTR)"OK", MB_OK);
  101.    
  102.     if (nFlag < 0) 
  103.       MessageBox (hWnd, (LPSTR)"Communication device not set",
  104.                   (LPSTR)NULL, MB_OK); 
  105.      
  106.  
  107.     /* must close the communication device before leaving the program */
  108.     CloseComm (nCid);
  109.  
  110.     return 0; 
  111. }
  112.