如何获取本机器的串口?(100分)

  • 主题发起人 主题发起人 laixbsh
  • 开始时间 开始时间
L

laixbsh

Unregistered / Unconfirmed
GUEST, unregistred user!
如何获取本机器的串口?
比如有:com1,com2
 
用API函数
 
读注册表
 
用哪个API函数呢?
 
读注册本啦!
用api大概也是用openfile吧!!!
 
好象不是,我记不太清楚了。你查查API的CHM啊
 
procedure TComSet.FormCreate(Sender: TObject);
var R:TRegistry;
St:TStringList;
i: Integer;
begin
R:=TRegistry.Create;
St:=TStringList.Create;
R.RootKey:=HKEY_LOCAL_MACHINE;
R.OpenKey('/Hardware/DeviceMap/SerialComm',false);
R.GetValueNames(St);
for i:=0 to St.Count-1 do
SelPort.Items.Add(R.ReadString(St.Strings));
R.Free;
St.Free;
end;
 
你看看这个例子
procedure GetCommList(CommList:TStrings);
var
reg:tregistry;
i:integer;
s:string;
list:TStringList;
begin
List:=Tstringlist.create;
reg:=tregistry.create;
reg.RootKey:=HKEY_LOCAL_MACHINE;
reg.OpenKey('Hardware/DeviceMap/SerialComm',true);
reg.GetValueNames(List);
for i:=0 to List.Count-1 do
begin
s:=List;
if (s='')or(strlicomp(@s[1],'com',3)<>0) then
begin
s:=reg.ReadString(s);
if (s='')or(strlicomp(@s[1],'com',3)<>0) then continue;
end;
commList.Add(s);
end;
reg.closekey;
reg.free;
List.free;
end;
 
uses WinSpool;

procedure GetComPortNames(AList: TStrings);
var
BytesNeeded, Returned, I: DWORD;
Success: Boolean;
Buffer: Pointer;
InfoPtr: PPortInfo1;
TempStr: string;
begin
Success := EnumPorts(nil, 1, nil, 0, BytesNeeded, Returned);
if (not Success) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
GetMem(Buffer, BytesNeeded);
try
Success := EnumPorts(nil, 1, Buffer, BytesNeeded, BytesNeeded, Returned);
for I := 0 to Returned - 1 do
begin
InfoPtr := PPortInfo1(DWORD(Buffer) + I * SizeOf(TPortInfo1));
TempStr := InfoPtr^.pName;
if Copy(TempStr, 1, 3) = 'COM' then
AList.Add(Copy(TempStr, 1, 4));
end;
finally
FreeMem(Buffer);
end;
end;
if not Success then
raise EComError.Create('EnumPorts function failed');
end;
 
读注册表
 
The following example shows how to tell Windows to relaunch your application when Windows starts up if it was running when the system shut down. When Windows starts up, it launches each application listed in the RunOnce key and then deletes the entry for that application. Therefore, you do not need to remove the entry written here.

procedure TForm1.WMEndSession(var Message: TWMEndSession);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/RunOnce',
True)
then Reg.WriteString('MyApp','"' + ParamStr(0) + '"');
finally
Reg.CloseKey;
Reg.Free;
inherited;
end;
end;
 
后退
顶部