📄 unitcommset.~pas
字号:
//******************************************************************************
//*单元名称:UnitCommSet *
//*单元功能:串口设置以及串口可以个数的获得 *
//*具体描述: *
//*开发作者:张志华 *
//*电子邮件:E-mail(MSN):luer88@163.net; QQ:29763034 TEL:13088096280 *
//*开发日期:2004-10-11 *
//*使用方法: *
//*属 性: *
//*修改日期: *
//******************************************************************************
unit UnitCommSet;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons
,IniFiles;
type
Tfrm_CommSet = class(TForm)
Cmd_NO: TSpeedButton;
Cmd_Ok: TSpeedButton;
ComboBox2: TComboBox;
Edit_BaudRate: TLabeledEdit;
Edit_CommName: TComboBox;
Label1: TLabel;
Label2: TLabel;
Panel1: TPanel;
procedure Cmd_OkClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
procedure GetAvailableComPorts;
end;
var
frm_CommSet: Tfrm_CommSet;
implementation
{$R *.dfm}
//获取当前串口
{
********************************* Tfrm_CommSet *********************************
}
procedure Tfrm_CommSet.Cmd_OkClick(Sender: TObject);
var
f: TIniFile;
begin
f:=TIniFile.Create(ExtractFileDir(application.Exename)+'\SystemSet.ini');
f.WriteString('CommSeting','CommName',Trim(Edit_CommName.Text)); //服务端口号
f.WriteString('CommSeting','BaudRate',Trim(Edit_BaudRate.Text)); //服务端口号
Application.MessageBox('系统提示!','串口设置成功!',MB_ICONERROR+MB_OK);
f.Free;
system.Halt;
end;
procedure Tfrm_CommSet.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=Cafree;;
system.Halt;
end;
procedure Tfrm_CommSet.FormCreate(Sender: TObject);
begin
GetAvailableComPorts; //获取当前串口
end;
procedure Tfrm_CommSet.GetAvailableComPorts;
var
i: Integer;
S: string;
ComName: array[0..9] of Char;
TheComHandle: Integer;
begin
(*** Okay, Clear everything in the PortsComboBox because
** we want to start off fresh.
*)
Edit_CommName.Clear();
(*
** Windows 95/98 can only handle up to 50 COM ports.
** All we are doing here is checking to see if Windows
** "can" open up the COM port. If so, then we know it's
** available. And so we add it to our ComboBox list
** and of course close the COM port each time we check it.
*)
for i := 1 to 50 do
begin
StrFmt(ComName, '\\.\COM%d', [i]);
TheComHandle := CreateFile
(
ComName, // name
GENERIC_READ or GENERIC_WRITE, // access attributes
0, // no sharing
nil, // no security
OPEN_EXISTING, // creation action
FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_OVERLAPPED, // attributes
0 // no template
);
if (DWORD(TheComHandle) <> INVALID_HANDLE_VALUE) then
begin
S := Format('COM%d', [i]);
Edit_CommName.Items.Add(S);
end;
CloseHandle(TheComHandle);
end; {end of for loop}
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -