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('Peugeot 308 95020 dash');                                              
                       
    Code_1:=inttohex(GetByteHexEdit($02),2)+inttohex(GetByteHexEdit($01),2)+inttohex(GetByteHexEdit($00),2)
      KM:= (strtoint('$'+Code_1)/10);
    //KM:= ($ffffffff-strtoint('$'+Code_1)/10);
    AddMsg('Actual value' + ' = '+ IntToStr(KM)+' km'); 
    MsgBox(IntToStr(KM) +' km', 'Actual value', MB_OK);  
   end;                                                                                             
end;                                                                        
                                                           
procedure NewKM;                                                                                                
var 
n,new_KM, new_KM2: integer;
n1,n2,n3,n4: String;

begin
  SetProgramModifiedOnly( true );
  if InBox( 'Enter KM', 'New KM', code ) then begin
    if  length( code ) < 3 then begin
      MsgBox( 'Enter 6 numbers', 'Error', MB_OK );    
      exit;
    end;
    if  StrToInt( code )> 999999 then begin
      MsgBox( 'Enter 000001-999999 Code', 'Error', MB_OK );    
      exit;
    end;

     n := StrToInt( code );
    new_KM:= $FFFFFF-(n)*10;
    new_KM2:= (n)*10;                                 
    n2:= IntToHex(new_KM2,6); 
    n1:= IntToHex(new_KM,6);
 //   AddMsg(IntToStr(new_KM)); 
 //   AddMsg(n1);  

   SetByteHexEdit( $02,(strtoint ('$'+n2[1]+n2[2])));
   SetByteHexEdit( $01,(strtoint ('$'+n2[3]+n2[4])));
   SetByteHexEdit( $00,(strtoint ('$'+n2[5]+n2[6])));
   
   SetByteHexEdit( $06,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $05,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $04,(strtoint ('$'+n1[5]+n1[6])));
   
   SetByteHexEdit( $0A,(strtoint ('$'+n2[1]+n2[2])));
   SetByteHexEdit( $09,(strtoint ('$'+n2[3]+n2[4])));
   SetByteHexEdit( $08,(strtoint ('$'+n2[5]+n2[6])));  
   
   SetByteHexEdit( $0E,(strtoint ('$'+n1[1]+n1[2])));
   SetByteHexEdit( $0D,(strtoint ('$'+n1[3]+n1[4])));
   SetByteHexEdit( $0C,(strtoint ('$'+n1[5]+n1[6]))); 

                             
 
                           
                                                                    
    RefreshHexEdit;
    if MsgBox( 'Program?', 'Confirm', MB_YESNO ) = IDYES then begin    
    SetProgramModifiedOnly( true );                                                   
    ProgramDevice;
    VerifyDevice;                                                                                 
    ReadKM;                                                                                                         
    end;                                                                                  
 end;
end;
                                              

                                                                                                     
                                                                                                 
begin                                                                     
  AddDeviceGroup( 'DASH', 'Select a car' );                                     
  AddDevice('Peugeot 308 95020 dash', 'ST95020', 'DASH', 'ST95020' ); 
  AddAction( 'Read KM', 'ReadKM', 'Peugeot 308 95020 dash' );                                                            
  AddAction( 'New KM', 'NewKM',  'Peugeot 308 95020 dash' );                                                       
  //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 (Peugeot_308_2008_2009_95020_dash.uds) Installed '#169' 2010 DRPETER ');          
  SetProductInfo( 'Peugeot 308 95020 dash', 'Device Script'#$D'Copyright'#169' DRPETER 2010' );
end.                                                                           
                                                                              
