📄 在delphi中用spcomm实现串口编程.htm
字号:
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按钮。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN lang=EN-US>2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">.安装后,在</SPAN><SPAN
lang=EN-US> System</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件面板中将出现一个红色控件</SPAN><SPAN
lang=EN-US> COM</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。现在就可以像</SPAN><SPAN
lang=EN-US> Delphi</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">自带控件一样使用</SPAN><SPAN
lang=EN-US> COM</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件了。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN lang=EN-US>SPCOMM</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的属性、方法和事件</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN lang=EN-US>1.</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>CommName</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:表示</SPAN><SPAN
lang=EN-US> COM1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US> COM2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等串口的名字;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>BaudRate:</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">根据实际需要设定的波特率,在串口打开后也可更改此值,实际波特率随之更改;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>ParityCheck</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:表示是否需要奇偶校验;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>ByteSize</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:根据实际情况设定的字节长度;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>Parity</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:奇偶校验位;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>StopBits</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:停止位;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>SendDataEmpty</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:这是一个布尔型属性,为</SPAN><SPAN
lang=EN-US> true</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">时表示发送缓存为空,或者发送队列里没有信息;为</SPAN><SPAN
lang=EN-US> false</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">时表示发送缓存不为空,或者发送队列里有信息。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN lang=EN-US>2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">.方法</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>Startcomm</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法用于打开串口,当打开失败时通常会报错。错误主要有</SPAN><SPAN
lang=EN-US> 7</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">种:⑴串口已经打开;⑵打开串口错误;⑶文件句柄不是通信句柄;⑷不能够安装通信缓存;⑸不能产生事件;⑹不能产生读进程;⑺不能产生写进程;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>StopComm</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法用于关闭串口,没有返回值;</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>WriteCommData(pDataToWrite: PChar;dwSizeofDataToWrite:Word
)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法是个带有布尔型返回值的函数,用于将一个字符串发送到写进程,发送成功返回</SPAN><SPAN
lang=EN-US> true</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,发送失败返回</SPAN><SPAN
lang=EN-US> false</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。执行此函数将立即得到返回值,发送操作随后执行。该函数有两个参数,其中</SPAN><SPAN
lang=EN-US> pDataToWrite</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是要发送的字符串,</SPAN><SPAN
lang=EN-US> dwSizeofDataToWrite</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是发送字符串的长度。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN lang=EN-US>3</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">.事件</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>OnReceiveData :procedure (Sender: TObject;Buffer:
Pointer;BufferLength: Word) of object </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当有数据输入缓存时将触发该事件,在这里可以对从串口收到的数据进行处理。</SPAN><SPAN
lang=EN-US> Buffer</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中是收到的数据,</SPAN><SPAN
lang=EN-US> BufferLength</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是收到的数据长度。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">●</SPAN><SPAN
lang=EN-US>OnReceiveError : procedure(Sender: TObject; EventMask : DWORD)
</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当接收数据出现错误时将触发该事件。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN lang=EN-US>SPCOMM</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的使用</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面是一个利用</SPAN><SPAN
lang=EN-US> SPCOMM</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件的串口通信的例子。</SPAN><SPAN
lang=EN-US> </SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"></SPAN></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以实现</SPAN><SPAN
lang=EN-US> PC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">机与单片机</SPAN><SPAN
lang=EN-US> 8051</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">之间的通信为例,首先要调通它们之间的握手信号。假定它们之间的通信协议是:</SPAN><SPAN
lang=EN-US> PC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</SPAN><SPAN
lang=EN-US> 8051</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一帧数据</SPAN><SPAN
lang=EN-US> 6</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节,</SPAN><SPAN
lang=EN-US> 8051</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</SPAN><SPAN
lang=EN-US> PC</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一帧数据也为</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -