📄 comtest.pas
字号:
unit ComTest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, ExtCtrls, Spin;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup; //选定串口的控件
Button1: TButton; //运行串口的按钮
Button2: TButton; //发送文本的按钮
Button3: TButton; //清除文本的按钮
Label1: TLabel; //显示串口句柄
Label2: TLabel; //显示发送情况
Label3: TLabel; //显示接收情况
Timer1: TTimer; //接收数据的时钟
ComboBox1: TComboBox; //选取波特率的控件
ComboBox2: TComboBox; //选取数据位的控件
ComboBox3: TComboBox; //选取停止位的控件
ComboBox4: TComboBox; //选取校验方式的控件
Memo1: TMemo; //编辑发送文本
Memo2: TMemo; //显示接收文本
procedure sendstr(ss:string);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
k,nn,sendtimes:integer; //定义计数器
comf1:integer; //定义串口句柄
Dcb1:DCB; //定义设备控制结构体
COMMTIMEOUTS1:COMMTIMEOUTS; //定义超时结构体
Readbuf1,Writebuf1:array [1..256] of byte; //定义读写缓冲区
implementation
{$R *.DFM}
procedure TForm1.sendstr(ss:string); //定义发送文本的方法
var i:integer; //临时变量
NumberOfBytesWritten:Cardinal; //实际发送的字节数
ssw:widestring;
begin
for i:=1 to length(ss) do writebuf1[i]:=ord(ss[i]);
//将文本字符导入输出缓冲区
if WriteFile(comf1,writebuf1,length(ss),NumberOfBytesWritten,0) then
//调用API函数发送
begin
inc(sendtimes); //成功后计数
label2.caption:='第'+inttostr(sendtimes)+'次 '+
inttostr(NumberOfBytesWritten)+'字符'; //显示发送情况
end;
end;
procedure TForm1.Button1Click(Sender: TObject); //运行串口的按钮按下
var ComPort:PChar; //定义以null结尾的字符串变量
begin
sendtimes:=0; //初始化发送次数
ComPort:=PChar(RadioGroup1.Items[RadioGroup1.ItemIndex]);
//确定串口名称
if Button1.Caption='运行串口' then //当串口未运行时
begin
comf1:=Createfile(ComPort, GENERIC_WRITE + GENERIC_READ,
0,nil, OPEN_EXISTING, 0,0);
//创建串口句柄为可读写方式, comf1为串口句柄
if comf1>0 then //如果串口句柄创建成功
begin
Button2.Enabled:=true; //允许发送文本
SetupComm(comf1,16000,16000); //设置操作系统的发送和接收缓冲区大小
dcb1.Baudrate:=strtoint(ComboBox1.text); //设定波特率
dcb1.ByteSize:=strtoint(ComboBox2.text); //设定数据位
dcb1.StopBits:=ComboBox3.ItemIndex; //设定停止位
dcb1.Parity:=ComboBox4.ItemIndex; //设定校验方式
SetCommState(comf1,Dcb1); //确定串口设备控制方式
with COMMTIMEOUTS1 do //超时设定结构体
begin
ReadIntervalTimeout:=0; //间隔时间
ReadTotalTimeoutMultiplier:=1; //重复都次数
ReadTotalTimeoutConstant:=100; //读超时限定
WriteTotalTimeoutMultiplier:=0; //重复写次数
WriteTotalTimeoutConstant:=150; //写超时限定
end;
SetCommTimeOuts(comf1,COMMTIMEOUTS1); //确定超时设定
timer1.enabled:=true; //允许读数据
Button1.Caption:='终止串口'; //显示允许终止
end
else exit; //如果串口句柄未创建成功退出
end
else //如果串口已经运行允许终止
begin
timer1.enabled:=false; //不允许读数据
closeHandle(comf1); //关闭串口句柄
comf1:=0;
Button1.Caption:='运行串口'; //显示允许运行串口
Button2.Enabled:=false; //不允发送读数据
end;
label1.caption:=Comport+'='+inttostr(comf1); //显示串口句柄
k:=0; nn:=0; //初始化计数器
Timer1.Interval:=300; //读时钟设为300毫秒
end;
procedure TForm1.Button2Click(Sender: TObject); //发送按钮按下
begin
sendstr(memo1.Text); //发送memo1中的文本
label2.Caption:='字符数'+inttostr(length(memo1.Text));
end;
procedure TForm1.Timer1Timer(Sender: TObject); //读数据时钟
var i:integer; //临时变量
NumberOfBytesRead:Cardinal; //实际读到的字节数
ss:string; //临时字符串
begin
if ReadFile(comf1,readbuf1,60, NumberOfBytesRead,0)then
//读60个字节到readbuf1
if NumberOfBytesRead>0 then //如果实际读到字节数>0
begin
inc(k); //计读次数
nn:=nn+NumberOfBytesRead; //计字节数
label3.caption:='第'+inttostr(k)+ '次收到'+
inttostr(NumberOfBytesRead)+'字符 共:'+inttostr(nn)+'字符';
//显示收到的情况
ss:='';
for i:=1 to NumberOfBytesRead do ss:=ss+chr(readbuf1[i]);
//由二进制区到字符串
memo2.Lines.add(ss); //放入memo2
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
memo1.Lines.Clear; //清除编辑发送的文本
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -