Delphi로 장치정보를 조회하기
by gomsun2
장치 정보를 조회하기
시스템의 장치 정보를 조회하는 방식은
- WQL질의
- Registory 조회
- Windows API 활용
같은 방식이 있습니다.
WQL 질의
WQL은 API의 접근대신에 시스템 정보를 추상화 시켜 놓은 아이입니다.
때문에, API를 몰라도 되며, API의 변화에 대응할 필요도 없습니다.
컨셉이 일관된 인터페이스제공(OS 버전따위는 상관없는)이기에 개발자에게는 매우 바람직한 아이라고 할 수 있습니다.
- 시스템의 정보를 가져올 수도 있으며, Event를 등록하면 Event를 받아 올 수도 있습니다. >_<
- 개발자는 테스트를 수행하고 질의를 가다듬어 대응 코드를 작성를 작성하면됩니다.
WMI Delphi Code Generator
WMI Delphi Code Generator
URL 멋집니다. ㅠ_ㅠ. theroadtodelphi, The Road To Delphi
WQL을 사용하신다면 꼭 사용하세요. 두번 사용하세요.
Delphi, PurePascal, C#, MS C++ 코드가 이쁘게 줄맞춰 생성됩니다.
생성된 코드를 사용하시면 됩니다.
registry 조회
- Comport: HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
- Bluetooth: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\
- 아래에 Btxxx 로 정의되는 아이들.
- 외에도 여러 곳에 나열되어 있을겁니다.
해당 레지스트리를 조회하여 원하는 정보를 취합할 수 있습니다.
unit mComportList;
interface
uses
Classes, SysUtils, Windows, System.Generics.Collections;
type
TComPortList = class(TStringList)
private
public
procedure Search;
end;
implementation
{ TComPortList }
procedure TComPortList.Search;
const
RegPath = 'HARDWARE\DEVICEMAP\SerialComm';
var
Reg: TRegistry;
SL: TStringList;
i: Integer;
begin
Reg := TRegistry.Create(KEY_READ);
SL := TStringList.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if not Reg.KeyExists(RegPath) then Exit;
if not Reg.OpenKey(RegPath, False) then
raise Exception.Create('TSerialComDeviceList.find_SerialComDevice: RegKey OpenFailed ');
Reg.GetValueNames(SL);
for i := 0 to SL.Count - 1 do
Values[Reg.ReadString(SL[i])] := SL[i];
Reg.CloseKey;
finally
FreeAndNil(SL);
FreeAndNil(Reg);
end;
end;
end.
Windows API (SetupDixxx) 사용
MSDN: http://msdn.microsoft.com/en-us/library/windows/hardware/ff551010(v=vs.85).aspx
- 링크와 같이 무수하게 많은 API가 있습니다.
- 일일이 Input, Output 파라미터, 각각의 구조체를 알아야지만 작성가능합니다. 손품을 팔아야 결과를 얻을 수 있습니다.
Comport와 USB의 연결정보는 아래의 예에서 찾을 수 있습니다.
이노야 님이 쓰신 글 :
: !!!참 쉽지 않네용, 고수님들의 조언을 부탁드립니다.
: unit Unit1;
:
: interface
:
: uses
: Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
: CPort, CPortTypes, CPortCtl, Registry, StdCtrls;
:
: const
: SendStart = Chr($73) + Chr($10) + Chr($04) + Chr($10) + Chr($40) + Chr($50) + Chr($AA);
: SendData = Chr($73) + Chr($10) + Chr($04) + Chr($10) + Chr($60) + Chr($70) + Chr($AA);
:
: RecvStart = Chr($73) + Chr($50) + Chr($04) + Chr($10) + Chr($30) + Chr($20) + Chr($AA);
: RecvEnd = Chr($73) + Chr($50) + Chr($04) + Chr($01) + Chr($70) + Chr($60) + Chr($AA);
:
: type
: TForm1 = class(TForm)
: ComPort1: TComPort;
: ComDataPacket1: TComDataPacket;
: ListBox1: TListBox;
: Memo1: TMemo;
: Button1: TButton;
: Button2: TButton;
: procedure ComDataPacket1Packet(Sender: TObject; const Str: String);
: procedure Button1Click(Sender: TObject);
: procedure Button2Click(Sender: TObject);
: private
: { Private declarations }
: function StrToHex(const S: string): string;//16진수변환
:
: public
: { Public declarations }
: com_port : string;
: end;
:
: var
: Form1: TForm1;
:
: implementation
:
: {$R *.DFM}
:
: function TForm1.StrToHex(const S: string): string;//16진수변환
: var
: i: Integer;
: begin
: Result := '';
: for i := 1 to Length(S) do
: Result := Result + IntToHex( Byte(S[i]), 2 );
: end;
:
: procedure TForm1.ComDataPacket1Packet(Sender: TObject; const Str: String);
: var
: I : Integer;
: begin
: if (Str = RecvStart) then // 최초 장비로부터 메세지를 받았을 때
: begin
: ComPort1.WriteStr(SendStart); // 장비와의 ID 체크를 위해 전송한다
: end
: else if (Str = RecvEnd) then // 장비로부터 종료 메세지를 받았을 때
: begin
: com_port := ComPort1.Port; // 실제 통신포트 저장 (요부분도 인식하질 않고요.)
:
: Exit; // 끝내면 된다
: end
: else // 장비ID정보를 받거나 측정데이터를 받았을 때
: begin
: ComPort1.WriteStr(SendData);
:
: for i := 1 to Length(Str) do
: Memo1.Lines.Add('['+IntToStr(i) + '] ' +Str[i] + ' (' + StrToHex(Str[i]) + ')' + ' : ' +
: IntTostr(StrToInt('$'+StrToHex(Str[i])))+#13);
: Memo1.Lines.Add('-------------');
:
: end;
: end;
:
: procedure TForm1.Button1Click(Sender: TObject);
: var
: TempReg: TRegistry;
: TempStr: TStrings;
: I: Integer;
: begin
: TempReg := TRegistry.Create;
:
: with TempReg do
: try
: RootKey := HKEY_LOCAL_MACHINE;
: if OpenKey('HARDWARE\DEVICEMAP\SERIALCOMM', True) then
: begin
: TempStr := TStringList.Create;
: try
: GetValueNames(TempStr);
: ListBox1.Clear;
: for I := 0 to TempStr.Count-1 do
: ListBox1.Items.Add(ReadString(TempStr.Strings[I]));
: finally
: TempStr.Free;
: end;
:
: CloseKey;
: end;
:
: finally
: Free;
: end;
: end;
:
: procedure TForm1.Button2Click(Sender: TObject);
: var
: i : integer ;
: cPort : string ;
: begin
: ComDataPacket1.StartString := #115;
: ComDataPacket1.StopString := #170;
:
: com_port := '';
: for I := 0 to ListBox1.Items.Count-1 do
: begin
: ListBox1.ItemIndex := I;
:
: if ComPort1.Connected then
: ComPort1.Close;
:
: ComPort1.Port := AnsiString(Listbox1.Items[I]);
: ComPort1.BaudRate := br38400;
: ComPort1.Open;
:
: if ComPort1.Connected then
: begin
: ComPort1.WriteStr(SendStart); // 장비와의 ID 체크를 위해 전송한다
: if com_port <> '' then // <-- 요부분 인식하지 않음
: begin
: cPort := TRIM(Listbox1.Items[I]);
: Memo1.Lines.Add(cPort);
: break;
: end;
: end;
: end;
: end;
:
: end.