Program Prog1; 
var
z,Loop:integer;  
CRC,crc1: String; 
                                                                  
procedure checksum;
        begin              
            

                     For Loop := 1 to 16  do    Begin 
                          CRC:= (inttohex(Z,6)[5]+inttohex(Z,6)[6]) + CRC;
                          Z := z + 1;
                     end;      
                          Z := Z - 31;    
                     For Loop := 1 to 16  do    Begin 
                          CRC:= (inttohex(Z,6)[5]+inttohex(Z,6)[6]) + CRC;
                          Z := z + 1; 
                     end; 

        end;                               

        
Procedure ReadKM;  
  var 
     Code_1: string;     
     KM: Integer;                                                                                                                                                            
   begin 
         AddMsg('Ang nabasa ay.....');  
         Code_1:=inttohex(GetByteHexEdit($82),2)+inttohex(GetByteHexEdit($81),2)+inttohex(GetByteHexEdit($80),2);                                                             
         KM := strtoint( '$'+Code_1) ;    
         AddMsg('Aktuwal na kilometrahe' + ' = '+ IntToStr(KM)+' Kilometro');                              
         MsgBox(IntToStr(KM) +' Kilometro', 'Aktuwal na KM', MB_OK);
         Addmsg('Lahat ng Kalkulasyon ay ginawa ni Hcip, Manila Philippines, July 2010'); 
   end; 
   ///////////////////////////////////////////////////////////////////////////////////////
procedure Readeep;
    begin                                   
        clearmsgs;                                                              
        ReadDevice;                                        
        VerifyDevice;                                                                                                                 
        ReadKM; 
        MsgBox ('"Siguraduhing naka-save ang data bago mag write"   ', 'Babala!!!', MB_iconinformation);
   end;  
//////////////////////////////////////////////////////////////////////////////////////////////   
procedure WriteKM;                                                                                                
var                                  
  n,new_KM,crc2,CRC3,x: integer;                
  Code,n1,n2: String;                                                                
                                                                                             
begin  
  clearmsgs;
  SetProgramModifiedOnly( true ); 
    if InBox( 'Bagong KM', 'Ipasok ang Bagong Kilometro',code) then begin 
     end
       else                               
        exit;  
                if code = '' then begin                                       
                 msgbox('Walang Data','Babala!!', MB_ok);
                 Writekm;   exit;
                end;    
    if strtoint (code) >255 then begin
        if MsgBox( 'Wrong Checksum for 256 KM up', 'How to calculate CRC?', MB_RETRYCANCEL ) = IDRETRY then begin 
           WriteKM;
        end; 
        exit;
    end; 

 n1 :=  inttohex(Strtoint(code),6);     
    Z := (Strtoint(code)/32)* 30 +90;   
    Checksum; 
        crc2:= strtoint(code) - (strtoint(code) /32) * 32  
        CRC3 := CRC2 * 2 +1;            
        n2 := crc[crc3] + crc[crc3 +1];                                         

        X := 128;
        while x < 173 do begin  
           SetByteHexEdit( x,   (strtoint ('$'+n1[5]+n1[6])));                       
           SetByteHexEdit( X +1,(strtoint ('$'+n1[3]+n1[4])));                 
           SetByteHexEdit( X +2,(strtoint ('$'+n1[1]+n1[2])));
           SetByteHexEdit( X +3,(strtoint ('$'+n2[1]+n2[2])));
           x :=x +4;
        end;
   
        RefreshHexEdit;                                        
    if MsgBox( 'Program?', 'Confirm', MB_YESNO ) = IDYES then begin                                                       
        ProgramDevice; 
        AddMsg('Verifying Device Code'); 
        VerifyDevice;                                                                                 
        ReadKM;
    end; 
    //////////////////////////////////////////////////////////////////////////////////
end;                                                                                                       
                                                                                                 
begin                                                                     
AddDeviceGroup( 'Dash TESTING', 'Select a car' );                                     
     AddDevice('Dash TRW', '24C08', 'Dash TESTING', '24C08' ); 
     AddAction( 'Read KM', 'Readeep', 'Dash TRW' );                                                            
     AddAction( 'New KM', 'WriteKM',  'Dash TRW' );                                                       
                                             
end.                                                             
