Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Como detectar puerto serie Abierto (https://www.clubdelphi.com/foros/showthread.php?t=87828)

golf2008 03-03-2015 13:34:59

Como detectar puerto serie Abierto
 
Hola a todos, les quería preguntar como se puede hacer para detectar si un puerto serie está abierto.

Estoy trabajando en un Servidor Fiscal.

Desde ya muchas gracias

nlsgarcia 03-03-2015 18:22:30

golf2008,

Cita:

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

:rolleyes:

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.

golf2008 04-03-2015 15:15:30

Ok, muchas gracias.

Saludos


La franja horaria es GMT +2. Ahora son las 17:10:04.

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