Ver Mensaje Individual
  #7  
Antiguo 18-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: 26
seoane Va por buen camino
Lo único que se me ocurre es que también tenga que estar activada la linea DTR además de RTS. Vamos a cambiar los Flags de tal modo que estas se activen al abrir el puerto y permanezcan activas. Echo este cambio no abría ninguna diferencia entre lo que estamos haciendo nosotros y lo que hace el hyperterminal.

Código Delphi [-]
function Peso(Puerto: String): String;
var
  hPort: THandle;
  DCB: TDCB;
  Leidos: Cardinal;
  C: char;
begin
  Result:= '';
  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 := 7;
        Parity   := ODDPARITY;
        StopBits := TWOSTOPBITS;
        // Aqui es donde indicamos que active RTS y DTR
        Flags    := $1013;
        // Si no funciona prueba tambien con este otro valor
        // Flags := $1011;
      end;
      if SetCommState(hPort, DCB) then
      begin
        PurgeComm(hPort, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or
          PURGE_RXCLEAR);
        // Aqui esta la rutina para leer
        C:= #0;
        // Esperamos el caracter de inicio
        repeat
          ReadFile(hPort,C,1,Leidos,nil);
        until C = #02;
        // Leemos el peso
        repeat
          ReadFile(hPort,C,1,Leidos,nil);
          if C <> #13 then
            Result:= Result + C;
        until C = #13;
        // aqui termina
      end;
    end;
    CloseHandle(hPort);
  end;
end;

Esperemos que haya suerte de esta vez, ya me quede sin ideas
Responder Con Cita