Ver Mensaje Individual
  #7  
Antiguo 15-11-2008
[FGarcia] FGarcia is offline
Miembro Premium
 
Registrado: sep 2005
Ubicación: Cordoba, Veracruz, México
Posts: 1.123
Reputación: 22
FGarcia Va por buen camino
Como ando con el chamaco atravesado no habia dado la vuelta por aca. En la carpeta ejemplos de ComPort viene un ejemplo de aplicacion tipo consola para un modem. Por si las dudas lo pego aqui:

Código Delphi [-]
program ModTest;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  CPort,
  Windows,
  SyncObjs;
var
  ComPort: TComPort;
  Events: TComEvents;
  Answer, Data: string;
  Step: Integer;
  Event: TEvent;

function CtrlHandler(CtrlType: LongWord): Boolean;
begin
  Event.SetEvent;
  Result := True;
end;
begin
  Event := TEvent.Create(nil, True, False, '');
  SetConsoleCtrlHandler(@CtrlHandler, True);
  try
    ComPort := TComPort.Create(nil);
    try
      if ParamCount > 0 then
        ComPort.Port := ParamStr(1)
      else
        ComPort.Port := 'COM1';
      ComPort.Events := [];
      ComPort.FlowControl.ControlDTR := dtrEnable;
      ComPort.FlowControl.ControlRTS := rtsEnable;
      ComPort.Open; // open port
      ComPort.WriteStr('AT'#13#10); // send test command
      Answer := '';
      Step := 0;
      repeat
        Events := [evRxChar];
        ComPort.WaitForEvent(Events, Event.Handle, 5000);  // wait for charachters
        if evRxChar in Events then
        begin
          ComPort.ReadStr(Data, ComPort.InputCount);
          Answer := Answer + Data;
          if Pos('OK', Answer) > 0 then
            Break;
        end;
        Inc(Step)
      until (Events = []) or (Step = 20);
      if Pos('OK', Answer) > 0 then
        WriteLn('Modem found on ' + ComPort.Port)
      else
        WriteLn('Modem NOT found on ' + ComPort.Port);
    finally
      ComPort.Free;
    end;
  except
    on E: Exception do
      WriteLn('Error: ' + E.Message);
  end;
  Event.Free;
end.

y del archivo de ayuda:

Cita:

Reading from port
Reading from input buffer can be performed in two ways. Usually application calls one of the read methods inside OnRxChar event, which triggers when charachter(s) arrive in input buffer. If read method is called inside OnRxChar event, read timeouts should be set to no wait, that is, read method checks input buffer and returns immidiately, since the number of bytes in input buffer is already known. Application can also call read method outside OnRxChar, but it should set read timeouts properly. See TComTimeouts for more details.
If component is linked to other component that needs incoming data, like TComDataPacket or TCustomComTerminal, OnRxChar event is is not called, however, the component calls OnRxBuf event. The application can not read the data from input buffer inside OnRxBuf event, since it has already been read. The data is placed automatically by the component in Buffer parameter of OnRxBuf event. Whether OnRxChar or OnRxBuf event is called, can be checked with TriggersOnRxChar property.
Method Description
Read Reads from input buffer to non-typed variable.
ReadAsync Reads from input buffer to non-typed variable in
asynchronous mode.
ReadStr Reads from input buffer to string type variable.
ReadStrAsync Reads from input buffer to string type variable in
asnychronous mode.

Example (inside OnRxChar)
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
begin
ComPort1.ReadStr(Str, Count);
// do something with Str variable
end;

Example (outside OnRxChar)
var
Str: String;
begin
// set timeouts here or at design time
ComPort1.ReadStr(Str, NumberOfBytes);
// do something with Str variable
end;

Example (inside OnRxBuf)
procedure TForm1.ComPort1RxBuf(Sender: TObject; const Buffer; Count: Integer);
begin
// application does not have to read data from input buffer
// data is already in buffer parameter
HandleData(Buffer, Count); // handle data
end;
Mas de la ayuda en la lectura de eventos

Cita:
Waiting for events

When Events property is not empty, a special thread is created for monitoring port events when application calls Open method. This is possible only when you use TComPort component in an application (thread) which creates message loop. Most applications have message loop (GUI, NT services). However, if you want to use TComPort component in a console application, you have to set Events property to empty before calling Open method or your application will crash. To monitor events in console application, you have to use WaitForEvent method.

Example

var
ComPort: TComPort;
Events: TComEvents;

begin
ComPort := TComPort.Create(nil);
try
ComPort.Events := []; // do not create monitoring thread
ComPort.Open; // open port
Events := [evCTS, evDSR, evRLSD]; // define events to wait for
ComPort.WaitForEvents(Events, nil, WaitInfinite); // wait until at least one event happens
if evCTS in Events then

WriteLn('CTS Changed'); // CTS signal changed state
ComPort.Close; // close port
finally
ComPort.Free;
end;
end;



Je Je si las dudas persisten el lunes regresa de vacaciones sensei egostar
__________________
ESTO ES UN FORO ... NO UN MÓVIL
¿Por qué no escribir de una manera comprensible para que los humanos lo podamos entender?

Última edición por FGarcia fecha: 15-11-2008 a las 18:00:31.
Responder Con Cita