Program Prog1;
var 
Code,Code_1: string;
KM: Integer;

                                  
                              
Procedure ReadKM;
begin                                                 
  if not ReadDevice then      
   begin                                     
    MsgBox( 'Read Error', 'Error', MB_OK );                       
   end                                                                                                                       
  else                                          
   begin                                                                                
    AddMsg('Alfa Brera 2007 93C86');                                              
                       
    Code_1:=inttohex(GetByteHexEdit($01),2)+inttohex(GetByteHexEdit($00),2);
    KM:=(( 65535  -strtoint('$'+Code_1))*16); 
    AddMsg('Actual value' + ' = '+ IntToStr(KM)+' km');  
    MsgBox(IntToStr(KM) +' km', 'Actual value', MB_OK);          
   end;                                                                                             
end;                                                                        
                                                           
procedure NewKM;                                                                                                
var                                       
n,new_KM: integer;
n1: String;
                                           
begin                                 
  SetProgramModifiedOnly( true );            
  if InBox( 'Enter KM', 'New KM', code ) then begin
                 
    end;
    if  StrToInt( code )> 999999 then begin
      MsgBox( 'Enter 000001-999999 Code', 'Error', MB_OK );    
      exit;                    
    end;                         

    n:= StrToInt( code )/16;
    new_KM:= $FFFF-(n);          
    n1:= IntToHex(new_KM,4);
 

   SetByteHexEdit( $00,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $01,(strtoint ('$'+n1[1]+n1[2]))); 
   SetByteHexEdit( $02,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $03,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $04,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $05,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $06,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $07,(strtoint ('$'+n1[1]+n1[2])));  
   SetByteHexEdit( $08,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $09,(strtoint ('$'+n1[1]+n1[2]))); 
   SetByteHexEdit( $0A,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $0B,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $0C,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $0D,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $0E,(strtoint ('$'+n1[3]+n1[4])));    
   SetByteHexEdit( $0F,(strtoint ('$'+n1[1]+n1[2]))); 
   SetByteHexEdit( $10,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $11,(strtoint ('$'+n1[1]+n1[2]))); 
   SetByteHexEdit( $12,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $13,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $14,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $15,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $16,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $17,(strtoint ('$'+n1[1]+n1[2])));  
   SetByteHexEdit( $18,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $19,(strtoint ('$'+n1[1]+n1[2]))); 
   SetByteHexEdit( $1A,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $1B,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $1C,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $1D,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $1E,(strtoint ('$'+n1[3]+n1[4])));    
   SetByteHexEdit( $1F,(strtoint ('$'+n1[1]+n1[2]))); 
    RefreshHexEdit;                                        
    if MsgBox( 'Program?', 'Confirm', MB_YESNO ) = IDYES then begin    
    SetProgramModifiedOnly( true );                                                   
    ProgramDevice;
    VerifyDevice;                                                                                 
    ReadKM;                                                                                                         
    end;                                                                                  
 
end;
                                              

                                                                                                     
                                                                                                 
begin
  AddDeviceGroup( 'Alfa Romeo', 'Select a car' );                                     
  AddDevice('Alfa Brera 2007 93C86', '93C86', 'Alfa Romeo', '93C86' ); 
  AddAction( 'Read KM', 'ReadKM', 'Alfa Brera 2007 93C86' );                                                            
  AddAction( 'New KM', 'NewKM',  'Alfa Brera 2007 93C86' );                                                       
  //AddOpenFileAction( 'Connection', 'Mini 2004 2005 BCU 93C76', 'mini3.jpg' );  
  //AddOpenFileAction( 'Pinout', 'Mini 2004 2005 BCU 93C76', 'mini2.jpg' );
  //AddOpenFileAction( 'Front dash', 'Mini 2004 2005 BCU 93C76', 'mini.jpg' ); 
  AddMsg('Script (Alfa_Brera_2007_93C86.uds) Installed '#169' 2010 DRPETER ');          
  SetProductInfo( 'Alfa Brera 2007 93C86', 'Device Script'#$D'Copyright'#169' DRPETER 2010' );
end.                                                                           
                                                                              
