Recent

Author Topic: How to run AVRDUDE in commandline FPC application  (Read 6131 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 255
How to run AVRDUDE in commandline FPC application
« on: October 09, 2021, 05:38:32 pm »
Hey Brothers,

Does anybody know how to start AVRDUDE in a command line application?

First: the text lines from avrdude do have to show up in console itself, but then with a #10#13 at the end. With does not work, see attachment.

Second: If the AVRDUDE application ends my own FPC console executable has to continue.

Anybody knows a small code example with right options ?

Greets, Wouter van Wegen

or a SSH command line parameter?

or try a FPC command to the console it is running in, to try?

where do i have to google on? #10 #13 is called linefeed? or wat is the original term
« Last Edit: October 09, 2021, 07:42:09 pm by pascalbythree »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: How to run AVRDUDE in commandline FPC application
« Reply #1 on: October 09, 2021, 07:59:18 pm »
So what are you actually doing: invoking AvrDude from inside a custom program that you've written (I'm trying to distinguish this case from your simply bundling them up in a shell script)? If so how are you invoking it: show your code. Assuming Linux rather than something really weird, what happens if instead of running AvrDude you run something like ls? Is it an off-the-shelf AvrDude, or have you built it from scratch?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

prof7bit

  • Full Member
  • ***
  • Posts: 161
Re: How to run AVRDUDE in commandline FPC application
« Reply #2 on: October 11, 2021, 03:49:36 pm »
This might serve as inspiration.

(copy-pasted from own code for similar purpose, stripped down to the relevant parts)

Code: Pascal  [Select][+][-]
  1. var
  2.   B: Byte;
  3.   Line: String = '';
  4.  
  5.   // snip
  6.  
  7. begin
  8.  
  9.   // snip
  10.  
  11.   FProc.Options := [poUsePipes, poStderrToOutPut, poNoConsole];
  12.   try
  13.     FProc.Execute;
  14.     TimeOut := Now + TimeOutSeconds * SECOND;
  15.  
  16.     repeat
  17.       while FProc.Output.NumBytesAvailable > 0 do begin
  18.         FProc.Output.Read(B{%H-}, 1);
  19.         if B = 8 then  // the JLink commander sometimes sending backspace
  20.           Setlength(Line, (Length(Line) - 1))
  21.         else begin
  22.           Line += chr(B);
  23.           if RightStr(Line, Length(LineEnding)) = LineEnding then begin;
  24.             Line := Trim(Line);
  25.             Print(Line);
  26.             Line := '';
  27.           end;
  28.         end;
  29.       end;
  30.       if Assigned(IdleMethod) then
  31.          IdleMethod;
  32.     until not FProc.Running or (Now > TimeOut);
  33.  
  34.     if FProc.Running then begin
  35.       Print(rsTimeout);
  36.       FProc.Terminate(1);
  37.       Result := 1;
  38.     end
  39.     else
  40.       Result := FProc.ExitCode;
  41.  
  42. // snip
  43.  
  44.  

As IdleMethod I have Application.ProcessMessages to not freeze the UI during these 5 seconds the process takes to run and instead of Print() (which in my program is a method to add the lines to a memo so I can watch them scrolling by in realtime) you can add them to a string list.

Basically I read from the Pipe byte by byte until a line ending occurs, then it knows it has a line complete, processes it and empties the line again.

The above code is part of a component I once wrote whose sole purpose is to run this tool, I use it sometimes in GUI apps, sometimes in console apps, so I can assign the idle method (if it is GUI) or just leave it away if it is a command line app that is allowed to block its main thread.

The Print() method will also call a callback that I can supply or otherwise it will just writeln to stdout.
« Last Edit: October 11, 2021, 04:00:25 pm by prof7bit »

pascalbythree

  • Sr. Member
  • ****
  • Posts: 255
Re: How to run AVRDUDE in commandline FPC application
« Reply #3 on: October 11, 2021, 06:28:41 pm »
Is this code for UnTerminal-1.0 or is it default FPC linux code?
I am now working to compile it.

pascalbythree

  • Sr. Member
  • ****
  • Posts: 255
Re: How to run AVRDUDE in commandline FPC application
« Reply #4 on: October 11, 2021, 06:32:13 pm »
yay! it got towork i think! thank you all! I am now going to implement the snippet code into my own console application! Greets Wouter

incase new problems, i will be back.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: How to run AVRDUDE in commandline FPC application
« Reply #5 on: October 11, 2021, 06:36:49 pm »
yay! it got towork i think! thank you all! I am now going to implement the snippet code into my own console application! Greets Wouter

incase new problems, i will be back.

Am I correct in thinking that FProc was an instance of TProcess?

The reason for some of my questions was that I've previously modified and rebuilt AVRDude, so if necessary could attack the problem from that side as well. I think though that I've also seen this staircasing in another context, and could probably investigate how I'd worked round it there as well.

I strongly suggest that if you do have problems you continue them *here*, having fragmented threads doesn't help anybody.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

prof7bit

  • Full Member
  • ***
  • Posts: 161
Re: How to run AVRDUDE in commandline FPC application
« Reply #6 on: October 11, 2021, 07:34:00 pm »
yay! it got towork i think! thank you all! I am now going to implement the snippet code into my own console application! Greets Wouter

incase new problems, i will be back.

Am I correct in thinking that FProc was an instance of TProcess?

The reason for some of my questions was that I've previously modified and rebuilt AVRDude, so if necessary could attack the problem from that side as well. I think though that I've also seen this staircasing in another context, and could probably investigate how I'd worked round it there as well.

This is indeed a TProcess.

The reason for this code to exist is there is a command line tool called JLinkCommander that can be used to flash a variety of ARM Cortex controllers (so quite similar in purpose to avrdude) and then I have a dozen small GUI Applications (one for each product) that assists in flashing, calibrating and testing the final product. So I have a TComponent that wraps this JLink Tool (and over the years I have written also a bunch of other components that wrap other tools, some software, some hardware) that I can quickly plug into my Application, set a bunch of properties and use it. I tried to make them all independent of LCL, so I am not calling Application.ProcessMessages directly, instead I call a callback that may or may not call ProcessMessages, depending on whether I make a GUI app or just a command line program, for example our automated test robot (once finished) will call a stripped down command line version of my GUI tools to flash and test the boards.

An improvement to the above code (but needlessly overkill for my applications) would be to have a separate thread doing the blocking read from the pipe and sync each line it reads onto the main thread, but since this tool never runs longer than for a few seconds I can put the entire call into a button method and use it blocking. For something longer running I would wrap it in its own thread.

BTW: All our production runs on small Linux PCs, administration is done mostly with Ansible, and every single self made application (every single one of them) is made with Lazarus, with the exception of some minor scripting here and there which is bash or python.
« Last Edit: October 11, 2021, 07:40:37 pm by prof7bit »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: How to run AVRDUDE in commandline FPC application
« Reply #7 on: October 11, 2021, 07:41:40 pm »
I've not used it but I recognised the name.

I've used TProcess on a couple of occasions: one was to wrap various APL implementations inside a terminal emulator (which could talk to them using either handles or a socket) which was obviously highly interactive. The other- and I think this is where I've seen the staircasing- was wrapping a shell inside a larger-scale program: obviously that was also interactive but I think I remember seeing staircased output from ls.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

prof7bit

  • Full Member
  • ***
  • Posts: 161
Re: How to run AVRDUDE in commandline FPC application
« Reply #8 on: October 11, 2021, 07:56:03 pm »
This is the first time I looked at this code again after a few years and I just noticed: This will use 100% CPU while the TProcess is running, so I guess I'll have to improve this a bit. A quick and dirty solution would be a sleep(10) before the until. I never noticed because it runs for such a short time only.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: How to run AVRDUDE in commandline FPC application
« Reply #9 on: October 11, 2021, 08:41:21 pm »
...which could obviously be an issue if the interface hardware uses tight loops for any of its timing.

...and I've just taken a look, and I've got a Sleep(10) near the end of the loop :-)

MarkMLl
« Last Edit: October 11, 2021, 09:10:29 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018