📄 network programming - cserialport.htm
字号:
<a href="/ui_frameworks/index.shtml">ui frameworks</a><br>
--><A
href="http://www.codeguru.com/advancedui/index.shtml">ui &
printing frameworks</A><BR></SMALL></TD></TR>
<TR>
<TD><SPAN class=navBarH2>Graphics & Multimedia</SPAN>
<BR><SMALL><A
href="http://www.codeguru.com/bitmap/index.shtml">bitmaps
& palettes</A><BR><A
href="http://www.codeguru.com/directx/index.shtml">directx</A><BR><A
href="http://www.codeguru.com/gdi/index.shtml">gdi</A><BR><A
href="http://www.codeguru.com/multimedia/index.shtml">multimedia</A><BR><A
href="http://www.codeguru.com/opengl/index.shtml">opengl</A><BR></SMALL></TD></TR>
<TR>
<TD><SPAN class=navBarH2>Internet & Networking</SPAN>
<BR><SMALL><A
href="http://www.codeguru.com/ieprogram/index.shtml">ie
programming</A><BR><A
href="http://www.codeguru.com/internet/index.shtml">internet
protocols</A><BR><A
href="http://www.codeguru.com/isapi/index.shtml">isapi</A><BR><A
href="http://www.codeguru.com/network/index.shtml">network
protocols</A><BR></SMALL></TD></TR>
<TR>
<TD><SPAN class=navBarH2>Miscellaneous</SPAN> <BR><SMALL><A
href="http://www.codeguru.com/misc/index.shtml">miscellaneous</A><BR></SMALL></TD></TR><!--
<tr><td><span class="navBarH2">Security</span></td></tr>
<tr><td><small>
<a href="/cryptoapi/index.shtml">crypto api</a><br>
<a href="/encryption/index.shtml">encryption/decryption</a><br>
<br>
</small></td></tr>
-->
<TR>
<TD><SPAN class=navBarH2>Visual Studio</SPAN> <BR><SMALL><!--
<a href="/addins/index.shtml">add-ins</a><br>
<a href="/tips/index.shtml">editor tips</a><br>
<a href="/macros/index.shtml">macros</a><br>
--><A
href="http://www.codeguru.com/debug/index.shtml">debugging</A><BR><A
href="http://www.codeguru.com/devstudio_macros/index.shtml">add-ins
& macros</A><BR><A
href="http://www.codeguru.com/tips/index.shtml">editor
tips</A><BR></SMALL></TD></TR>
<TR>
<TD><SPAN class=navBarH2>Windows Programming</SPAN>
<BR><SMALL><A
href="http://www.codeguru.com/ce/index.shtml">ce</A><BR><A
href="http://www.codeguru.com/clipboard/index.shtml">clipboard</A><BR><A
href="http://www.codeguru.com/dll/index.shtml">dll</A><BR><A
href="http://www.codeguru.com/files/index.shtml">file &
folder</A><BR><A
href="http://www.codeguru.com/help/index.shtml">help
systems</A><BR><A
href="http://www.codeguru.com/printing/index.shtml">printing</A><BR><A
href="http://www.codeguru.com/win32/index.shtml">win32</A><BR><A
href="http://www.codeguru.com/system/index.shtml">system</A><BR></SMALL></TD></TR>
<TR>
<TD><SPAN class=navBarH2>Windows & Dialogs</SPAN>
<BR><SMALL><A
href="http://www.codeguru.com/console/index.shtml">console</A><BR><A
href="http://www.codeguru.com/dialog/index.shtml">dialog</A><BR><A
href="http://www.codeguru.com/docking/index.shtml">docking
window</A><BR><A
href="http://www.codeguru.com/doc_view/index.shtml">doc/view</A><BR><A
href="http://www.codeguru.com/splitter/index.shtml">splitter</A><BR></SMALL></TD></TR>
<TR>
<TD><SPAN class=navBarH1>Interact</SPAN></TD></TR>
<TR>
<TD><SMALL><A
href="http://e-newsletters.internet.com/">Newsletters</A><BR><A
href="http://www.codeguru.com/Guestbook/guestbook.shtml">Guestbook</A><BR><A
href="http://www.codeguru.com/cgi-bin/recommend.cgi">Recommend
Us!</A><BR><A href="http://www.codeguru.com/about.shtml">About
Us</A><BR><BR></SMALL></TD></TR></TBODY></TABLE><!-- JOB BUTTON -->
<TABLE border=0 cellPadding=5 cellSpacing=0 width=150>
<TBODY>
<TR>
<TD align=middle><A href="http://jobs.internet.com/"
target=new><IMG alt="[Internet Jobs]"
src="Network Programming - CSerialPort.files/jobs.gif">
</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!--/navbar>
<!-- blank column for spacing -->
<TD vAlign=top width=20> </TD><!-- end blank column for spacing --><!-- begin content column -->
<TD vAlign=top><!--
<table cellpadding="0" cellspacing="0">
<tr>
--><!-- begin main content column --><!-- closign of column, row, and table are in subfoot file --><LINK
href="Network Programming - CSerialPort.files/global.css" rel=stylesheet>
<CENTER>
<H3><FONT color=#a0a099>A Communication Class for Serial Port
</FONT></H3></CENTER>
<HR>
This article was contributed by <A
href="mailto:remon.spekreijse@ict.nl">Remon Spekreijse</A>.
<H3>Background:</H3>I've searched for a good communication class for a
while and could not find one. That's when I decided to write my own and it
should be one that's easy to use.
<P>In the newgroups there are many questions about serial communication so
I thought: make it public! It's freeware. The only thing I expect from
users is that they drop me a mail. All modifications on this class are
free, but please let me know if it solves a bug or adds some good
features. Also comment your code and don't let me solve your bugs!
<H3>Target:</H3>The class is not intended to use as a baseclass for
modemcommunication but more for driving hardware or reading hardware via
the serial port.
<P>From the classes included there is only one class important:
CSerialPort. The other classes are only there to illustrate the use of
this class.
<H3>Usage:</H3>In your software you only need to create an instance of the
CSerialPort class and call InitPort. <PRE><FONT color=#008000>// the owner (CWnd) of the port (receives message)</FONT>
BOOL CSerialPort::InitPort(CWnd* pPortOwner,
UINT portnr, <FONT color=#008000>// portnumber (1..4)</FONT>
UINT baud, <FONT color=#008000>// baudrate</FONT>
char parity, <FONT color=#008000>// parity </FONT>
UINT databits, <FONT color=#008000>// databits </FONT>
UINT stopbits, <FONT color=#008000>// stopbits </FONT>
DWORD dwCommEvents, <FONT color=#008000>// EV_RXCHAR, EV_CTS etc</FONT>
UINT writebuffersize) <FONT color=#008000>// size of the writebuffer</FONT>
</PRE>
<P>The dwCommEvents flag can be used for communication with the owner of
this class.
<P>The flags can be one of the following (or combined with |):
<UL>
<LI>WM_COMM_BREAK_DETECTED A break was detected on input.
<LI>WM_COMM_CTS_DETECTED The CTS (clear-to-send) signal changed state.
<LI>WM_COMM_DSR_DETECTED The DSR (data-set-ready) signal changed state.
<LI>WM_COMM_ERR_DETECTED A line-status error occurred. Line-status
errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
<LI>WM_COMM_RING_DETECTED A ring indicator was detected.
<LI>WM_COMM_RLSD_DETECTED The RLSD (receive-line-signal-detect) signal
changed state.
<LI>WM_COMM_RXCHAR A character was received and placed in the input
buffer.
<LI>WM_COMM_RXFLAG_DETECTED The event character was received and placed
in the input buffer. </LI></UL>
<P>Accept the first parameter all parameters are optional. The standard
values are: <PRE>portnr = 1
baud = 19200
parity = 'N'
databits = 8,
stopsbits = 1,
dwCommEvents = EV_RXCHAR | EV_CTS,
nBufferSize = 512);
</PRE>
<P>So the follwing code is enough to make communication possible:
<P>in the header of the owner: <PRE> CSerialPort m_Serial;
</PRE>
<P>in the code: <PRE> m_Serial.InitPort(this);
m_Serial.StartMonitoring();
</PRE>
<P>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: <PRE>BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
<FONT color=#008000>//{{AFX_MSG_MAP(CCommtestDlg)</FONT>
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
<FONT color=#008000>//}}AFX_MSG_MAP</FONT>
END_MESSAGE_MAP()
</PRE>and they must be handled: <PRE>LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
<FONT color=#008000>// do something with the received character</FONT>
return 0;
}
</PRE>
<P>This is it for reading. Writing can be done with WriteChar or
WriteToPort
<H3>Downloads</H3><A
href="http://www.codeguru.com/network/serialport_demo.zip">Download sample
project - 66K</A>
<H3>History</H3>Date Posted: February 27, 1998<BR><!--comments-->
<H3>Comments:</H3>
<UL><!--startlist-->
<LI><A
href="http://www.codeguru.com/mfc/comments/31593.shtml">Handshaking</A>
- Fred (2002/01/10)
<LI><A href="http://www.codeguru.com/mfc/comments/31592.shtml">please
help me</A> - nurul (2002/01/10)
<LI><A href="http://www.codeguru.com/mfc/comments/30377.shtml">I cannot
shutdown port correctly! URGENT!</A> - pine Tan (2001/12/18)
<LI><A href="http://www.codeguru.com/mfc/comments/30044.shtml">How To
send <Ctrl>+<Z> to Serial Port </A>- ZYX (2001/12/05)
<LI><A href="http://www.codeguru.com/mfc/comments/30041.shtml">RECEIVING
SERIAL DATA(SETTING COM TIMEOUTS)URGENT</A> - BAMZAI (2001/12/04)
<LI><A href="http://www.codeguru.com/mfc/comments/29918.shtml">A way to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -