有没有如题相关的的程序事例~~~请高手赐教
解决方案 »
- 如何在主对话框上嵌入子对话框??
- 使用GetWindowRect得到某个窗口的窗口坐标 问题。
- 怎样用C或C++实现类似QQ聊天中联系人的树形模式?
- VC6.0关于DATAGRID的不同列设置不同颜色的问题
- VC++2005离奇错误 急盼解决
- 急!!!来者有分! 直接使用socket访问一个HTTP服务器上的一个Web Page, 如何使用SOCKS代理呀
- 最快的字符串查找???
- 请问怎么能通过程序创建一个unicode文件,能让其在日文系统下显示日文中没有的字体如"测试",谢谢!
- CFileDialog的问题,帮忙,我已经想了好几天了
- 有关"拷贝构造函数"问题!
- 这条语句什么意思?
- 用opengl画三维圆柱体快疯了!
** FILENAME CSerialPort.cpp
**
** PURPOSE This class can read, write and watch one serial port.
** It sends messages to its owner when something happends on the port
** The class creates a thread for reading and writing so the main
** program is not blocked.
**
** CREATION DATE 15-09-1997
** LAST MODIFICATION 12-11-1997
**
** AUTHOR Remon Spekreijse
**
**
*/#include "stdafx.h"
#include "SerialPort.h"#include <assert.h>
// Constructor
//
CSerialPort::CSerialPort()
{
m_hComm = NULL; // initialize overlapped structure members to zero
m_ov.Offset = 0;
m_ov.OffsetHigh = 0; // create events
m_ov.hEvent = NULL;
m_hWriteEvent = NULL;
m_hShutdownEvent = NULL; m_szWriteBuffer = NULL; m_bThreadAlive = FALSE;
}// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive); TRACE("Thread ended\n"); //leak memory!!!
if(m_szWriteBuffer)
{
delete m_szWriteBuffer;
m_szWriteBuffer = NULL;
}
}//
// Initialize the port. This can be port 1 to 4.
//
BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL); // if the thread is alive: Kill
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended\n");
} // create events
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // initialize the event objects
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent; // initialize critical section
InitializeCriticalSection(&m_csCommunicationSync);
// set buffersize for writing and save the owner
m_pOwner = pPortOwner;// leak memory!
if(m_szWriteBuffer)
{
delete m_szWriteBuffer;
m_szWriteBuffer = NULL;
}
m_szWriteBuffer = new char[writebuffersize]; m_nPortNr = portnr; m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents; BOOL bResult = FALSE;
char *szPort = new char[50];
char *szBaud = new char[50]; // now it critical!
EnterCriticalSection(&m_csCommunicationSync); // if the port is already opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
} // prepare port strings
sprintf(szPort, "COM%d", portnr);
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits); // get a handle to the port
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices if (m_hComm == INVALID_HANDLE_VALUE)
{
// port not found
delete [] szPort;
delete [] szBaud; return FALSE;
} // set the timeout values
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000; // configure
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
if (BuildCommDCB(szBaud, &m_dcb))
{
if (SetCommState(m_hComm, &m_dcb))
; // normal operation... continue
else
ProcessErrorMessage("SetCommState()");
}
else
ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()"); delete [] szPort;
delete [] szBaud; // flush the port
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // release critical section
LeaveCriticalSection(&m_csCommunicationSync); TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr); return TRUE;
}
VC++ 程序员最好用 API 读写串行口;这个控件是为那些不能直接读写串行口的工具提供的,比如:VB、PB等。