Ver Mensaje Individual
  #2  
Antiguo 17-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
Puestos a escoger me quedo con el método de transmisión continua. Aquí te queda el código, no esta muy optimizado, pero dada la urgencia que tienes puede servirte para salir del apuro. Comprueba que los parámetros del puerto serie (velocidad, bits de parada, etc) son correctos, si no estas seguro, lo mejor, es utilizar el hypertermial del propio windows, ahí podrás probar diferentes configuraciones del puerto hasta conseguir la correcta.

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 := 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);
        // 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;

// Un ejemplo de como usar la funcion
ShowMessage(Peso('COM2'));

Pruebalo, sino funciona prueba distintos parámetros con el hyperterminal, y si aun así no funciona a lo mejor tenemos que jugar con los valores del Pin RTS, pero primero vamos a probar así.
Responder Con Cita