How to get the printer's response to a command via the API/in a plugin/a feedback control?



You'll have to try to parse it yourself from the stream of lines received from the printer.

Due to a firmware bug that is still present in vendor forks it's not possible for OctoPrint to reliably associate sent commands with their output. This is because commands are not necessarily being acknowledged in the same order as they were sent in, making it impossible to match them up with their produced output.

Hence OctoPrint has no way to identify the output of a given command reliably. To do its own response parsing it looks for known response formats in the received lines and matches them via string matching or regular expressions. That also means that if a line cannot 100% reliably identified as a specific output format (e.g. it doesn't have some leading prefix or otherwise identifiable features), parsing gets very tricky.

Additionally, OctoPrint always operates on a line by line base. Multi line outputs are therefore a challenge. They are easy to do if all lines in a multi line response are prefixed in a certain way (e.g. the Cap:... output in the firmware's M115 capability report), or if they are bracketed by clearly identifiable start and end lines (e.g. the Begin file list/End file list in the M20 file list). But if you have a command that simply throws a bunch of lines back that are indistinguishable from others, things get tricky.

So, long story short: To retrieve output from the firmware, you'll need to process the received lines one by one and look for the patterns of lines you are interested in. From a plugin, you can do that for example via the octoprint.comm.protocol.gcode.received hook which will provide you with all lines that are received from the firmware. Combine it with the octoprint.comm.protocol.gcode.sent hook and you can also react to lines sent to the firmware if necessary. Via the API, you'll need to subscribe to push updates and look for the included log lines (which include both directions of the printer communication). You might want to take a look at the included python client on how to access that API.

For parsing single lines just to display their output somewhere you can also use custom controls with regex and template set (which basically are a way to tell OctoPrint to send a defined command and then look for specific patterns for you), but due to the line-by-line-processing those will not work on multi line responses.