📄 faq87.htm
字号:
<HTML>
<HEAD>
<TITLE>Send and receive data from a serial port (RS-232)</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Send and receive data from a serial port (RS-232)
</H3>
<P>
DOS programmers may be familiar with how to send and receive serial data by reading and writing directly from the port's
hardware. In windows, you do not interface with the hardware directly. Instead, you use the CreateFile, ReadFile, and
WriteFile API functions to send and receive data. The following code snippet demonstrates how to open a serial port and
send the message "hello world".
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
HANDLE hCom <b>=</b> CreateFile<b>(</b><font color="blue">"COM1"</font><b>,</b>
GENERIC_READ <b>|</b> GENERIC_WRITE<b>,</b>
<font color="blue">0</font><b>,</b>
NULL<b>,</b>
OPEN_EXISTING<b>,</b>
<font color="blue">0</font><b>,</b>
NULL <b>)</b><b>;</b>
<b>if</b><b>(</b>hCom<b>)</b>
<b>{</b>
DCB dcb<b>;</b>
ZeroMemory<b>(</b><b>&</b>dcb<b>,</b> <b>sizeof</b><b>(</b>dcb<b>)</b><b>)</b><b>;</b>
dcb<b>.</b>DCBlength <b>=</b> <b>sizeof</b><b>(</b>dcb<b>)</b><b>;</b>
dcb<b>.</b>BaudRate <b>=</b> <font color="blue">57600</font><b>;</b>
dcb<b>.</b>ByteSize <b>=</b> <font color="blue">8</font><b>;</b>
dcb<b>.</b>Parity <b>=</b> NOPARITY<b>;</b> <font color="navy">// NOPARITY and friends are</font>
dcb<b>.</b>StopBits <b>=</b> ONESTOPBIT<b>;</b> <font color="navy">// #defined in windows.h</font>
<font color="navy">// Set the port properties and write the string out the port.</font>
<b>if</b><b>(</b>SetCommState<b>(</b>hCom<b>,</b><b>&</b>dcb<b>)</b><b>)</b>
<b>{</b>
DWORD ByteCount<b>;</b>
<b>const</b> <b>char</b> <b>*</b>msg <b>=</b> <font color="blue">"Hello world!"</font><b>;</b>
WriteFile<b>(</b>hCom<b>,</b>msg<b>,</b> strlen<b>(</b>msg<b>)</b><b>,</b><b>&</b>ByteCount<b>,</b>NULL<b>)</b><b>;</b>
<b>}</b>
CloseHandle<b>(</b>hCom<b>)</b><b>;</b>
<b>}</b>
<b>}</b>
</pre>
<P>
This code example demonstrates how to get started with serial port programming in windows, but it is far from being
complete. For more information, read the help section entitled "Communications" in the <TT>win32.hlp</TT> help file.
The API provides a host of functions for configuring the port.
</P>
<P>
<B>Note:</B> The communications API functions are not that easy to work with from code. You may want to explore using
a class or component that encapsulates the serial port. Turbopower sells a product called AsyncPro that can be used
for serial communication. There are also some freeware alternatives out there. I also have a a class that encapsulates
the serial port. You can download it from the table below. My class is very simple, and probably not quite as powerful
as some of the third party products that you can buy. But it does make it a lot easier to send and receive data when
compared with the raw api calls. Note that my class is just that, a class. It is not a graphical component.
</P>
<BR>
<TABLE BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">
<TR> <TD colspan = 2><B>Downloads for this FAQ</B> </TD> </TR>
<TR> <TD><TT><A HREF="Download/commport.zip">commport.zip </A></TT></TD><TD>Source code for <TT>TCommPort</TT>, a serial port class.</TD> </TR>
<TR> <TD><TT><A HREF="Download/term.zip">term.zip </A></TT></TD><TD>Demo terminal program that uses <TT>TCommPort</TT> (BCB4).</TD> </TR>
<TR> <TD><TT><A HREF="Download/termx.zip">termx.zip </A></TT></TD><TD>Demo w/EXE. 283k.</TD> </TR>
</TABLE>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -