Delphi - Delphi control de circuito conectado a puerto serial

 
Vista:

Delphi control de circuito conectado a puerto serial

Publicado por Gerardo (1 intervención) el 12/01/2012 06:36:28
Esimados compañeros, tengo un circuito conectado a COM1, este circuito abre un torniquete eléctrico, mismo que debo controlar desde el pc. tengo un código en Delphi, pero no he logrado que el torniquete gire. El circuito lo pueden ver en el url: http://admin.altahoteleria.mx/media/torniquete.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
unit Unit;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    ComboBox1: TComboBox;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure AbrirPuerto(Puerto: String);
    procedure CerrarPuerto;
    procedure ActivarRTS;
    procedure DesactivarRTS;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  hPort: Thandle;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.AbrirPuerto(Puerto: String);
var
  DCB: TDCB;
begin
  Puerto:= Uppercase(Puerto);
  // if (Puerto<>'COM1') and (Puerto<>'COM2') then exit;
  hPort:= CreateFile(PChar('\\.\'+Puerto), GENERIC_READ or GENERIC_WRITE,0, nil,
    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if hPort<>INVALID_HANDLE_VALUE then
  begin
    DCB.DCBlength:= sizeof(DCB);
    if GetCommState(hPort,DCB) then
    begin
      with DCB do
      begin
        BaudRate := CBR_9600;
        ByteSize := 8;
        Parity   := NOPARITY;
        StopBits := ONESTOPBIT;
        Flags    := $01;
      end;
      if SetCommState(hPort, DCB) then
        Exit;
    end;
    CloseHandle(hPort);
    hPort:= INVALID_HANDLE_VALUE;
  end;
end;
 
procedure TForm1.CerrarPuerto;
begin
  if hPort <> INVALID_HANDLE_VALUE then
  begin
    CloseHandle(hPort);
    hPort:= INVALID_HANDLE_VALUE;
  end;
end;
 
procedure TForm1.ActivarRTS;
begin
  if hPort <> INVALID_HANDLE_VALUE then
    EscapeCommFunction(hPort,SETRTS);
end;
 
procedure TForm1.DesactivarRTS;
begin
  if hPort <> INVALID_HANDLE_VALUE then
    EscapeCommFunction(hPort,CLRRTS);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Puertos.txt');
  ComboBox1.ItemIndex := 0;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Button1.Caption = 'Activar' then
  begin
    Button1.Caption := 'Desactivar';
    ComboBox1.Enabled := False;
    AbrirPuerto(ComboBox1.Text);
    ActivarRTS;
  end else
  begin
    Button1.Caption := 'Activar';
    ComboBox1.Enabled := True;
    DesactivarRTS;
    CerrarPuerto;
  end;
end;
 
end.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 65
Oro
Ha mantenido su posición en Delphi (en relación al último mes)
Gráfica de Delphi

Delphi control de circuito conectado a puerto serial

Publicado por E.T. (1244 intervenciones) el 12/01/2012 20:06:23
No se mucho del manejo de los puertos, pero viendo el codigo, la función ActivarRTS nunca se va a ejecutar, pues en la funcion AbrirPuerto estas cerrando el handle del puerto:

1
2
CloseHandle(hPort);
  hPort:= INVALID_HANDLE_VALUE;


mientras que en la funcion ActivarRTS tienes

1
if hPort <> INVALID_HANDLE_VALUE then


Por lo tanto hPort nunca es diferente de INVALID_HANDLE_VALUE al entrar a dicha funcion
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar