[B
  'PackInst' import  
 
  /LogMemo Create:LogMemo def
  /Log [B LogMemo.Log ] def
  10 LogMemo.SetFontSize
  LogMemo.Show

  % Define constants
  /BaudRate 500 def
  /InterfaceName 'USB-CAN converter' def
  /MACID 1 def
  /InitIdent $555 def
  /AckIdent $2AA def
  /LoaderSize 255 def   % size of Loader in number of messages (255 * 8 bytes) 

  /LoaderName 'CL_B1100.dat' def
  /MonitorFileName 'BootB1100_OS.h86' def
  
  /WriteEPROM [P
    [A /Addr /DataStr ]              
      Addr DataStr FALSE BlockWr.WriteBlock
      BlockWr.GetResult 0 ne [B 
    	  'TalkError: '  BlockRd.GetResult cvs   add   raisese
      ] if                                     
    [A ]
  ] def



  /LoadBinFile [P 
    [A /FileName ]
      FileName fileexist [B
        /LogFile Create:LogFile def
        % Open the file with the mode = 0 (read only)
        FileName 0 LogFile.Open
        % Create the data container        
        /Data 0 carr def  
        [B
          LogFile.Eof [B ExitLoop ] if	
          % Read 96 characters from LogFile (or less if end of file occurs).   
          Data 96 LogFile.ReadChars add                    
        ] loop  
        LogFile.Close                         
      ]
      [B
        FileName ' does not exist.' add raisese	        
      ]	 ifelse             
    [A /Data ]
  ] def

  /LoadTextFile [P 
    [A /FileName ]
      FileName fileexist [B
        /LogFile Create:LogFile def
        % Open the file with the mode = 0 (read only)
        FileName 0 LogFile.Open
        % Create the data container        
        /Data 0 carr def  
        [B
          LogFile.Eof [B ExitLoop ] if	
          % Read line   
          Data LogFile.ReadLine add         
        ] loop  
        LogFile.Close                         
      ]             
      [B
        FileName ' does not exist.' add raisese	        
      ]	 ifelse             
    [A /Data ]
  ] def         


  [B      		
    /CANComPort Create:CANComPort def
    /OpenBoardFailed CANComPort.Open not def
    
    OpenBoardFailed [B
      CANComPort.Close    
      'Error: Init board error.' raisese
    ] if      
          
    /Data $03 chr def								% bit timing register BTR low byte	
    /Data Data $6B chr add def						% bit timing register BTR high byte
    /Data Data AckIdent $FF and chr add def			% acknowledge message low byte
    /Data Data AckIdent $F00 and 8 shr chr add def	% acknowledge message identifier high byte 
    /Data Data LoaderSize chr add def				% loader size low byte (max = $FF)
    /Data Data $0 chr add def						% loader size high byte
      
    InitIdent 6 Data CANComPort.SendData 
      
    'Waiting response to initialization frame ...' log
    100 CANComPort.WaitResponse   % wait 100 ms for the answer from the controller
      
    CANComPort.GetExpired [B
      CANComPort.Close    
      'Error: processor is not in bootstrap mode.' raisese        
    ] if      
      
    % Check the answer!    
    CANComPort.GetIdent cvs log
    CANComPort.GetDataStr log      

    LoaderName LoadBinFile 
    /Data exch def
    'Sending loader...' log            
    Data [B 
      /DataLine exch def        
      [B
        DataLine Length 8 gt [B        
          InitIdent 8 DataLine CANComPort.SendData 
          /DataLine DataLine 8 DataLine Length getsubstr def
        ]
        [B
          InitIdent DataLine Length DataLine CANComPort.SendData
          Exitloop 
        ] ifelse  
      ] loop  
    ] forall	
    
    500 wait
            	 
    CANComPort.Close
    [B
	    /Device Create:Device def
	    /DeviceName 'LinMotInstall' def      	
	    'PortName' 'CAN' 'DeviceMACID' '0' 2 DeviceName 'LinMotInstall' Device.CreateDevice
	    DeviceName /PackInst:OnMessageCallback PackInst:Init [B
	      % Set ID and BaudRate to default (1, 500 Mbaud)
	      /BlockWr DeviceName Create:BlockWriter def
		  $300802 '0100' WriteEPROM % set "MACID Parameter Value" to "1"
		  $300860 '0000' WriteEPROM % set "MACID Source Select" to "By Parameter"
	      $300864 '0000' WriteEPROM % set "Baud Rate Source Select" to "By Parameter"
 	      $300866 'F401' WriteEPROM % set "Baud Rate Parameter Definition" to "500"
	      % load monitor file
	      /Data MonitorFileName LoadTextFile def        	
	      'Sending monitor software...' log 
	      Data MonitorFileName PackInst:DownloadH86File        
	      PackInst:ResetDevice            
	      'Monitor software loaded successfully.' log
	      LogMemo.Hide
		  ] if
      Device.DestroyDevice
    ]
    [B
      Device.DestroyDevice
      raisese	     
    ] tryexcept  
  ] 
  [B
    /Dict exch def
    Dict.Msg ' Installation aborted!' add log
  ] tryexcept	  
	    
	LogMemo.Remain  
]