Ver Mensaje Individual
  #2  
Antiguo 08-07-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Si solo quieres mandar un byte puede que esta funcion te resulte util:
Código Delphi [-]
function MandarByte(Puerto: String; B: Byte): Boolean;
var
  hPort: THandle;
  DCB: TDCB;
  Escritos: Cardinal;
begin
  Result:= FALSE;
  Puerto:= Uppercase(Puerto);
  // Cambiar esto si es necesario un puerto diferente
  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
      // Cambiar esto para una configuracion del puerto diferente
      with DCB do
      begin
        BaudRate := CBR_9600;
        ByteSize := 8;
        Parity   := NOPARITY;
        StopBits := ONESTOPBIT;
        Flags    := $01;
      end;
      if SetCommState(hPort, DCB) then
      begin
        PurgeComm(hPort, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or
          PURGE_RXCLEAR);
        Result := WriteFile(hPort, B, 1, Escritos, nil);
      end;
    end;
    CloseHandle(hPort);
  end;
end;

Para usarla, por ejemplo, para mandar un 1:
Código Delphi [-]
  MandarByte('COM1',1);

Como sugerencia: Puedes usar los pines DTR y RTS para mandar señales sin necesidad de un circuito muy complicado, pero si ya tienes el circuito montado, da lo mismo.
Responder Con Cita