阅读背景:

CSerial串口通信类的说明_liuxizhen2009的专栏

来源:互联网 

Background:
 
I've searched fora good communication class for a while and could not find one.
That's when Idecided to write my own and it should be one that's easy to use.
 
In the newgroupsthere are many questions about serial communication so I thought:
make it public! It'sfreeware. The only thing I expect from users is that they drop me a mail.
All modificationson this class are free, but please let me know if it solvers a bug
or adds some goodfeatures. Also comment your code and don't let me solve your bugs!
 
Target:
 
The class is notintended to use as a baseclass for modemcommunication but
more for drivinghardware or reading hardware via the serial port.
 
From the classesincluded there is only one class important: CSerialPort.
The other classesare only there to illustrate the use of this class.
 
Usage:
 
In your softwareyou only need to create an instance of the CSerialPort class
and callInitPort.
 
BOOLCSerialPort::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_CTSetc
                                                         UINT writebuffersize)        // size ofthe writebuffer
 
The dwCommEventsflag can be used for communication with the owner of this class.
 
The flags can beone of the following (or combined with |):
WM_COMM_BREAK_DETECTED        A break was detected on input.
WM_COMM_CTS_DETECTED               TheCTS (clear-to-send) signal changed state.
WM_COMM_DSR_DETECTED               TheDSR (data-set-ready) signal changed state.
WM_COMM_ERR_DETECTED              Aline-status error occurred. Line-status errors are                                        CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
WM_COMM_RING_DETECTED            Aring indicator was detected.
WM_COMM_RLSD_DETECTED            TheRLSD (receive-line-signal-detect) signal changed state.
WM_COMM_RXCHAR                    A character was received and placed inthe input buffer.
WM_COMM_RXFLAG_DETECTED      The event character was received andplaced in the input
                                             buffer. 
 
Accept the firstparameter 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 follwingcode is enough to make communication possible:
 
in the header ofthe owner:
         CSerialPort        m_Serial;
 
in the code:
         m_Serial.InitPort(this);
         m_Serial.StartMonitoring();
 
Then the treadthat watches the port is started and all events on the port are send to
the owner. Thereceive a character the owner needs a messageentry in the messagemap:
 
BEGIN_MESSAGE_MAP(CCommtestDlg,CDialog)
         //{
   Background:
 
I've searched fora good commun



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: