serial class
所属分类:串口编程
开发工具:Visual C++
文件大小:8KB
下载次数:29
上传日期:2005-03-06 11:01:27
上 传 者:
tiantian2001
说明: vc++6.0写的串口操作的类
(vc 6.0 write operation of the serial category)
文件列表:
serial class (0, 2005-03-06)
serial class\SerialPort.h (2991, 1998-02-01)
serial class\SerialPort.cpp (17480, 1998-02-01)
In your software you only need to create an instance of the CSerialPort class and call InitPort.
// the owner (CWnd) of the port (receives message)
BOOL CSerialPort::InitPort(CWnd* pPortOwner,
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 of the writebuffer
The dwCommEvents flag can be used for communication with the owner of this class.
The flags can be one of the following (or combined with |):
WM_COMM_BREAK_DETECTED A break was detected on input.
WM_COMM_CTS_DETECTED The CTS (clear-to-send) signal changed state.
WM_COMM_DSR_DETECTED The DSR (data-set-ready) signal changed state.
WM_COMM_ERR_DETECTED A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
WM_COMM_RING_DETECTED A ring indicator was detected.
WM_COMM_RLSD_DETECTED The RLSD (receive-line-signal-detect) signal changed state.
WM_COMM_RXCHAR A character was received and placed in the input buffer.
WM_COMM_RXFLAG_DETECTED The event character was received and placed in the input buffer.
Accept the first parameter all parameters are optional. The standard values are:
portnr = 1
baud = 19200
parity = 'N'
databits = 8,
stopsbits = 1,
dwCommEvents = EV_RXCHAR | EV_CTS,
nBufferSize = 512);
So the follwing code is enough to make communication possible:
in the header of the owner:
CSerialPort m_Serial;
in the code:
m_Serial.InitPort(this);
m_Serial.StartMonitoring();
Then the tread that watches the port is started and all events on the port are send to the owner. The receive a character the owner needs a messageentry in the messagemap:
BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
//{{AFX_MSG_MAP(CCommtestDlg)
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
and they must be handled:
LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
// do something with the received character
return 0;
}
近期下载者:
相关文件:
收藏者: