Como controlar un circuito conectado al puerto serial
Estimado experto, tengo un código para intentar controlar el circuito, este circuito es de un torniquete eléctrico. El código me ha ayudado a abrir el puerto, pero no he logrado conseguir que el torniquete gire. La imagen del circuito lo pueden ver haciendo clic aquí. Gracias de antemano!!
Las funciones Delphi:
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;
Las funciones Delphi:
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;