Com口函数
自动获取Com口,函数列表
procedure EnumComPorts(Ports: TStrings); //自动获取com口函数varKeyHandle: HKEY;ErrCode, Index: Integer;ValueName, Data: string;ValueLen, DataLen, ValueType: DWORD;TmpPorts: TStringList;beginErrCode := RegOpenKeyEx(HKEY_LOCAL_MACHINE,'HARDWARE\DEVICEMAP\SERIALCOMM',0,KEY_READ,KeyHandle);if ErrCode <> ERROR_SUCCESS thenExit; // raise EComPort.Create(CError_RegError, ErrCode);TmpPorts := TStringList.Create;tryIndex := 0;repeatValueLen := 256;DataLen := 256;SetLength(ValueName, ValueLen);SetLength(Data, DataLen);ErrCode := RegEnumValue(KeyHandle,Index,PChar(ValueName),Cardinal(ValueLen),nil,@ValueType,PByte(PChar(Data)),@DataLen);if ErrCode = ERROR_SUCCESS thenbeginSetLength(Data, DataLen);TmpPorts.Add(Data);Inc(Index);endelseif ErrCode <> ERROR_NO_MORE_ITEMS thenexit; //raise EComPort.Create(CError_RegError, ErrCode);until (ErrCode <> ERROR_SUCCESS) ;TmpPorts.Sort;Ports.Assign(TmpPorts);finallyRegCloseKey(KeyHandle);TmpPorts.Free;end;end;
procedure TForm1.FormCreate(Sender: TObject);beginEnumComPorts(COMBOBOX1.Items); //得到串口列表end;