app.cpp
上传用户:hxj5298
上传日期:2007-01-03
资源大小:18k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2. Module : APP.CPP
  3. Purpose: Provides the WinMain function for testing the serial port class
  4. Created: PJN / 23-05-1999
  5. History: None
  6. Copyright (c) 1999 by PJ Naughter.  
  7. All rights reserved.
  8. */
  9. /////////////////////////////////  Includes  //////////////////////////////////
  10. #include "stdafx.h"
  11. #include "SerialPort.h"
  12. ///////////////////////////////// Defines /////////////////////////////////////
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. //////////////////////////////// Implementation ///////////////////////////////
  19. class CSerialPortApp : public CWinApp
  20. {
  21. public:
  22.   virtual BOOL InitInstance();
  23. };
  24. CSerialPortApp theApp;
  25. BOOL CSerialPortApp::InitInstance()
  26. {
  27.   BYTE* pBuf = new BYTE[10000];
  28.   try
  29.   {
  30.     COMMCONFIG config;
  31.     CSerialPort::GetDefaultConfig(1, config);
  32.     CSerialPort port;
  33.     port.Open(1, 1200, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
  34.     HANDLE hPort = port.Detach();
  35.     port.Attach(hPort);
  36.     DWORD dwModemStatus;
  37.     port.GetModemStatus(dwModemStatus);
  38.     DCB dcb;
  39.     port.GetState(dcb);
  40.     dcb.BaudRate = 9600;
  41.     port.SetState(dcb);    
  42.     DWORD dwErrors;                      
  43.     port.ClearError(dwErrors);
  44.     port.SetBreak();
  45.     port.ClearBreak();
  46.     COMSTAT stat;
  47.     port.GetStatus(stat);
  48.     COMMTIMEOUTS timeouts;
  49.     port.GetTimeouts(timeouts);
  50.     port.Setup(10000, 10000);
  51.     port.GetConfig(config);
  52.     config.dcb.BaudRate = 9600;
  53.     port.SetConfig(config);
  54.     port.Set0WriteTimeout();
  55.     port.Set0ReadTimeout();
  56.     char sBuf[] = "This should appear on the serial port";
  57.     port.Write(sBuf, strlen(sBuf));
  58.     DWORD dwMask;
  59.     port.GetMask(dwMask);
  60.     port.SetMask(EV_TXEMPTY); 
  61.     //port.WaitEvent(dwMask);
  62.     port.TerminateOutstandingWrites();
  63.     port.TransmitChar('p');
  64.     port.Set0Timeout();
  65.     char sRxBuf[10];
  66.     DWORD dwRead = port.Read(sRxBuf, 10);
  67.     port.TerminateOutstandingReads();
  68.     port.ClearDTR();
  69.     port.ClearRTS();
  70.     port.SetDTR();
  71.     port.SetRTS();
  72.     port.SetXOFF();
  73.     port.SetXON();
  74.     COMMPROP properties;
  75.     port.GetProperties(properties);
  76.     port.ClearWriteBuffer();
  77.     port.ClearReadBuffer();
  78.     port.Flush();
  79.     port.Close();
  80.     //Try out the overlapped functions
  81.     CSerialPort port2;
  82.     port2.Open(1, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
  83.     CEvent event(FALSE, TRUE);
  84.     OVERLAPPED overlapped;
  85.     ZeroMemory(&overlapped, sizeof(OVERLAPPED));
  86.     overlapped.hEvent = event;
  87.     if (!port2.Write(pBuf, 10000, overlapped))
  88.     {
  89.       DWORD dwBytesWritten;
  90.       WaitForSingleObject(event, INFINITE);
  91.       port2.GetOverlappedResult(overlapped, dwBytesWritten, TRUE);
  92.     }
  93.     if (!port2.Read(pBuf, 10, overlapped))
  94.     {
  95.       DWORD dwBytesRead;
  96.       if (WaitForSingleObject(event, 1000) == WAIT_OBJECT_0)
  97.       {
  98.         TRACE(_T("Data was read from the serial portn"));
  99.         port2.GetOverlappedResult(overlapped, dwBytesRead, FALSE);
  100.       }
  101.       else
  102.         TRACE(_T("No data was read from the serial portn"));
  103.     }
  104.     port2.SetMask(EV_TXEMPTY); 
  105.     port2.WaitEvent(dwMask, overlapped);                  
  106.     /* for testing on NT only
  107.     port2.WriteEx(sBuf, strlen(sBuf));
  108.     SleepEx(INFINITE, TRUE);
  109.     port2.ReadEx(pBuf, 10);
  110.     SleepEx(INFINITE, TRUE);
  111.     */
  112.   }
  113.   catch (CSerialException* pEx)
  114.   {
  115.     TRACE(_T("Handle Exception, Message:%sn"), pEx->GetErrorMessage());
  116.     pEx->Delete();
  117.   }
  118.   delete [] pBuf;
  119.   return FALSE;
  120. }