Ver Mensaje Individual
  #14  
Antiguo 23-03-2008
Avatar de eduarcol
[eduarcol] eduarcol is offline
Miembro Premium
 
Registrado: ago 2003
Ubicación: En los estados Zulia y Merida de Venezuela
Posts: 4.151
Reputación: 25
eduarcol Va por buen camino
Segun lo poco que entendi tengo unas propuestas, son dos lineas basicamente las puse en rojo para que las identifiques. En la primera devuelvo cero si el resultado es correcto, y en la segunda lo agrego al memo

Código Delphi [-]
unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms,
 Dialogs, StdCtrls;
 type
 TForm1 = class(TForm)
 Memo1: TMemo;
 Button1: TButton;
 Button2: TButton;
 Label1: TLabel;
 OpenDialog1: TOpenDialog;
 procedure Button1Click(Sender: TObject);
 procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
  type buffer=array of byte;  //assign as Dynamic array
  var HexBuf:buffer;
 function ByteToHex(InByte:byte):shortstring;
const Digits:array[0..15] of char='0123456789ABCDEF';
begin
 result:=digits[InByte shr 4]+digits[InByte and $0F];
end;
  function readline(HexLine:string; var Buf:buffer):integer;
  var ADDR,count:integer;
  CHKSUM,SUMLINE,RECLEN,RECTYPE,DATA:byte; t:shortstring;
  begin
  // Array length may be change, here is 100,000
 SetLength(HexBuf,100000);
 if HexLine[1]=':' then begin
 t:='$'+copy(HexLine,2,2);   // get length
 RECLEN:=strtoint(t);
 CHKSUM:=0;
 CHKSUM:=CHKSUM+RECLEN;
 t:='$'+copy(HexLine,4,4); // get address
 ADDR:=strtoint(t);
 CHKSUM:=CHKSUM+lo(ADDR)+hi(ADDR);
 t:='$'+copy(HexLine,8,2);
 RECTYPE:=strtoint(t);
 CHKSUM:=CHKSUM+RECTYPE;
 case RECTYPE of
 0:begin             // datablock
 count:=0;
 while (count < RECLEN) do begin
 t:='$'+copy(HexLine,10+2*count,2);
 DATA:=strtoint(t);
 CHKSUM:=CHKSUM+DATA;
 Buf[ADDR+count]:=DATA;
 inc(count);
 end;
 t:='$'+copy(HexLine,10+2*count,2);
 SUMLINE:=strtoint(t);
 Result := 0;
 end;
 1:begin   // end of file
 t:='$'+copy(HexLine,10,2);
 SUMLINE:=strtoint(t);
 result:=1;
 end;
 else
 begin
 result := -2;  // invalid record type
 exit;
 end;
 end; //case
 // test checksum
 DATA:=SUMLINE+CHKSUM;
 if (DATA<>0) then result:=-3; // checksum error
 end
 else result:=-1; // no record
 end;
procedure TForm1.Button1Click(Sender: TObject);
var Fname,line:string;
Fp : textfile;
 ErrorCode:integer;
begin
 Fname:='test.hex';
 AssignFile(Fp,Fname); { File selected in dialog }
 Reset(Fp);
 while not eof(Fp) do
   begin
  Readln(Fp,line);{ Read first line of file }
  ErrorCode := readline(line,HexBuf);
   if (ErrorCode=0) then
  begin
   // Do some thing if read one line OK
   // such send data to serial port,parallel port etc.
      Memo1.Lines.Add(ByteToHex(HexBuf));
   end
    else
  begin
  // Error handle here
  //  0 = No error
  // -1 = File not Intel Hex format
  // -2 = Invalid record type
  // -3 = Checksum error
   end;
    end;
   CloseFile(Fp);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Label1.Caption:=OpenDialog1.FileName;
end;
end;
end.
__________________
...Yo naci en esta ribera del arauca vibr@d0r
Soy hermano de la espuma,
de la garza, de la rosa y del sol...
Viva Venezuela

Última edición por eduarcol fecha: 23-03-2008 a las 04:44:35. Razón: mejorar el resultado
Responder Con Cita