app.cpp
上传用户:hxj5298
上传日期:2007-01-03
资源大小:18k
文件大小:4k
- /*
- Module : APP.CPP
- Purpose: Provides the WinMain function for testing the serial port class
- Created: PJN / 23-05-1999
- History: None
- Copyright (c) 1999 by PJ Naughter.
- All rights reserved.
- */
- ///////////////////////////////// Includes //////////////////////////////////
- #include "stdafx.h"
- #include "SerialPort.h"
- ///////////////////////////////// Defines /////////////////////////////////////
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////// Implementation ///////////////////////////////
- class CSerialPortApp : public CWinApp
- {
- public:
- virtual BOOL InitInstance();
- };
- CSerialPortApp theApp;
- BOOL CSerialPortApp::InitInstance()
- {
- BYTE* pBuf = new BYTE[10000];
- try
- {
- COMMCONFIG config;
- CSerialPort::GetDefaultConfig(1, config);
- CSerialPort port;
- port.Open(1, 1200, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
- HANDLE hPort = port.Detach();
- port.Attach(hPort);
- DWORD dwModemStatus;
- port.GetModemStatus(dwModemStatus);
- DCB dcb;
- port.GetState(dcb);
- dcb.BaudRate = 9600;
- port.SetState(dcb);
- DWORD dwErrors;
- port.ClearError(dwErrors);
- port.SetBreak();
- port.ClearBreak();
- COMSTAT stat;
- port.GetStatus(stat);
- COMMTIMEOUTS timeouts;
- port.GetTimeouts(timeouts);
- port.Setup(10000, 10000);
- port.GetConfig(config);
- config.dcb.BaudRate = 9600;
- port.SetConfig(config);
- port.Set0WriteTimeout();
- port.Set0ReadTimeout();
- char sBuf[] = "This should appear on the serial port";
- port.Write(sBuf, strlen(sBuf));
- DWORD dwMask;
- port.GetMask(dwMask);
- port.SetMask(EV_TXEMPTY);
- //port.WaitEvent(dwMask);
- port.TerminateOutstandingWrites();
- port.TransmitChar('p');
- port.Set0Timeout();
- char sRxBuf[10];
- DWORD dwRead = port.Read(sRxBuf, 10);
- port.TerminateOutstandingReads();
- port.ClearDTR();
- port.ClearRTS();
- port.SetDTR();
- port.SetRTS();
- port.SetXOFF();
- port.SetXON();
- COMMPROP properties;
- port.GetProperties(properties);
- port.ClearWriteBuffer();
- port.ClearReadBuffer();
- port.Flush();
- port.Close();
- //Try out the overlapped functions
- CSerialPort port2;
- port2.Open(1, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
- CEvent event(FALSE, TRUE);
- OVERLAPPED overlapped;
- ZeroMemory(&overlapped, sizeof(OVERLAPPED));
- overlapped.hEvent = event;
- if (!port2.Write(pBuf, 10000, overlapped))
- {
- DWORD dwBytesWritten;
- WaitForSingleObject(event, INFINITE);
- port2.GetOverlappedResult(overlapped, dwBytesWritten, TRUE);
- }
- if (!port2.Read(pBuf, 10, overlapped))
- {
- DWORD dwBytesRead;
- if (WaitForSingleObject(event, 1000) == WAIT_OBJECT_0)
- {
- TRACE(_T("Data was read from the serial portn"));
- port2.GetOverlappedResult(overlapped, dwBytesRead, FALSE);
- }
- else
- TRACE(_T("No data was read from the serial portn"));
- }
- port2.SetMask(EV_TXEMPTY);
- port2.WaitEvent(dwMask, overlapped);
- /* for testing on NT only
- port2.WriteEx(sBuf, strlen(sBuf));
- SleepEx(INFINITE, TRUE);
- port2.ReadEx(pBuf, 10);
- SleepEx(INFINITE, TRUE);
- */
- }
- catch (CSerialException* pEx)
- {
- TRACE(_T("Handle Exception, Message:%sn"), pEx->GetErrorMessage());
- pEx->Delete();
- }
- delete [] pBuf;
- return FALSE;
- }