Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 02-02-2007
vejerf vejerf is offline
Miembro
 
Registrado: ene 2007
Posts: 206
Poder: 18
vejerf Va por buen camino
Puertos COM libres/ocupados???

Buenas tardes...
os expongo mi problema a ver si podeis ayudarme... que seguro q si...
pues bien tengo una aplicacion que debe recoger datos de los puertos serie y tengo q hacerlo genérico, es decir, independiente del número de puertos COM que tenga el ordenador... y la pregunta es...
existe algún método que me diga que puertos COM tengo en mi ordenador y cuales están libres/ocupados???
Muchas gracias a tod@s...
Responder Con Cita
  #2  
Antiguo 02-02-2007
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Poder: 24
seoane Va por buen camino
Se me ocurren un par de formas de hacerlo.

Una es consultando la entrada del registro:
Código:
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
Es decir:
Código Delphi [-]
uses Registry;

procedure GetPortList(List: TStringList);
var
  i: integer;
begin
  List.Clear;
  with TRegistry.Create do
  try
    RootKey:= HKEY_LOCAL_MACHINE;
    Access:= KEY_READ;
    if OpenKey('HARDWARE\DEVICEMAP\SERIALCOMM',FALSE) then
    begin
      GetValueNames(List);
      for i:= List.Count - 1 downto 0 do
        List[i]:= ReadString(List[i]);
      CloseKey;
    end;
  finally
    Free;
  end;
end;


// Por ejemplo para mostrarlo en un combobox
var
  List: TStringList;
begin
  List:= TStringList.Create;
  try
    GetPortList(List);
    // El Combobox se llama Combobox1
    Combobox1.Items.Assign(List);
  finally
    List.Free;
  end;
end;

Ahora sería interesante saber si el puerto esta ocupado:
Código Delphi [-]
function Ocupado(Puerto: String): Boolean;
var
  Handle: THandle;
begin
  Handle:= CreateFile(PChar('\\.\' + Puerto),GENERIC_READ or GENERIC_WRITE,0,
    nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    CloseHandle(Handle);
    Result:= FALSE;
  end else
    Result:= TRUE;
end;

// Siguiendo el ejemplo anterior, solo mostramos los libres
var
  List: TStringList;
  i: integer;
begin
  List:= TStringList.Create;
  try
    GetPortList(List);
    Combobox1.Items.Clear;
    for i:= 0 to List.Count - 1 do
      if not Ocupado(List[i]) then
        Combobox1.Items.Add(List[i]);
  finally
    List.Free;
  end;
end;

Pero ya que tenemos una función que nos dice si un puerto esta disponible, por que no la usamos también para listar los puertos. Solo tenemos que comprobar un numero razonable de puertos, aquí uso 10 pero si lo crees necesario usa 100.
Código Delphi [-]
// Otra vez el mismo Combobox
var
  i: integer;
begin
  Combobox1.Clear;
  for i:= 0 to 10 do
    if not Ocupado('COM' + IntToStr(i)) then
      Combobox1.Items.Add('COM' + IntToStr(i));
end;
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Que archivos libres usar brandolin Conexión con bases de datos 18 15-12-2006 13:29:18
Sacar habitaciones libres por dia - Problema consulta SQL- VRO Firebird e Interbase 0 01-09-2005 11:12:22
Como detectar los puertos libres en un máquina JDNA Internet 1 08-05-2004 06:07:55
Puertos Serie mrmanuel Varios 9 05-03-2004 04:04:48
Puertos USB tarco35 Varios 0 25-10-2003 18:30:46


La franja horaria es GMT +2. Ahora son las 21:44:44.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi