Ver Mensaje Individual
  #2  
Antiguo 03-03-2015
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
golf2008,

Cita:
Empezado por golf2008
...como se puede hacer para detectar si un puerto serie está abierto...


Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hCom : LongWord;
  ComSerial : Byte = 1;

implementation

{$R *.dfm}

// Abre un Puerto Serial Habilitado en Windows
function OpenPort(COMSerial : Byte) : Boolean;
var
   UserMsg : String;
   COMPort : String;
begin

   if (COMSerial = 0) then
   begin
      UserMsg := 'Puerto Serial Invalido';
      MessageDlg(UserMsg,mtError,[mbOK],0);
      Result := False;
      Exit;
   end;

   COMPort := '\\.\COM' + IntToStr(COMSerial);

   hCom := CreateFile(PChar(COMPort), GENERIC_READ OR GENERIC_WRITE, 0, Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
   if hCom = INVALID_HANDLE_VALUE then
   begin
      UserMsg := 'El Puerto de Comunicacion Serial No Esta Habilitado en Windows';
      MessageDlg(UserMsg,mtError,[mbOK],0);
      hCom := 0;
      Result := False;
      Exit;
   end;

   UserMsg := Format('El Puerto de Comunicacion Serial COM%d Fue Abierto',[ComSerial]);
   MessageDlg(UserMsg,mtInformation,[mbOK],0);
   Result := True;

end;

// Cierra un Puerto Serial Habilitado en Windows
function ClosePort(COMSerial : Byte) : Boolean;
var
   UserMsg : String;
begin

   If hCom > 0 then
   begin

      if not CancelIo(hCom) then
      begin
         UserMsg := 'CancelIo : ' + SysErrorMessage(GetLastError);
         MessageDlg(UserMsg,mtError,[mbOK],0);
         Result := False;
         Exit;
      end;

      if not CloseHandle(hCom) then
      begin
         UserMsg := 'CloseHandle : ' + SysErrorMessage(GetLastError);
         MessageDlg(UserMsg,mtError,[mbOK],0);
         Result := False;
         Exit;
      end;

      UserMsg := Format('El Puerto de Comunicacion Serial COM%d Fue Cerrado',[ComSerial]);
      MessageDlg(UserMsg,mtInformation,[mbOK],0);
      Result := True;
      hCom := 0;

   end
   else
   begin
      UserMsg := Format('El Puerto de Comunicacion Serial COM%d no esta Abierto',[ComSerial]);
      MessageDlg(UserMsg,mtError,[mbOK],0);
      Result := True;
   end;

end;

// Abre un Puerto Serial en Windows
procedure TForm1.Button1Click(Sender: TObject);
begin
   OpenPort(ComSerial);
end;

// Cierra un Puerto Serial en Windows
procedure TForm1.Button2Click(Sender: TObject);
begin
   ClosePort(ComSerial);
end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, permite abrir y cerrar un puerto serial que este físicamente habilitado en Windows.

Espero sea útil

Nelson.
Responder Con Cita