تستطيع اختبار ما اذا كان منفذ كوم موجودا او لا او غيره
ببعض التعليمات البسيطة
مثال في لغة ديلفي
فقط ضع ثلاث ازرار و هذا هو الكود لليونت كامل
==========================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function ComPortAvailable(Port: PChar): Boolean;
var
DeviceName: array[0..80] of Char;
ComFile: THandle;
begin
StrPCopy(DeviceName, Port);
ComFile := CreateFile(DeviceName, GENERIC_READ or GENERIC_WRITE, 0, nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
Result := ComFile <> INVALID_HANDLE_VALUE;
CloseHandle(ComFile);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if ComPortAvailable('COM1:') then
ShowMessage('Port available')
else
ShowMessage('Port not available');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if ComPortAvailable('COM2:') then
ShowMessage('Port available')
else
ShowMessage('Port not available');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if ComPortAvailable('COM3:') then
ShowMessage('Port available')
else
ShowMessage('Port not available');
end;
end.
====================================