<p>I'm writing a Python module that allows new tricks with printing - it already can produce G-code I can put into a file, send to the printer and print out. No problem with generating the correct codes to print.</p>
<p>What I want to add to it is interactive mode: connect to the printer over serial interface, then control the printer &quot;as you go&quot;, you enter a command, the printer responds. You run a procedure, the printer executes the sequence of G-codes, then the software returns to awaiting further commands. The Marlin website provides a comprehensive reference of all G-codes for me to use. What it lacks though is an intro about the protocol as a whole, especially the busy/wait and error responses, the timing of me sending the next command in sequence.</p>
<p>Can someone either explain that part in sufficient detail to allow me to write the code that knows when (and if) to send the next G-code command, and/or refer me to a resource that does just that?</p>

CC BY-SA 4.0
2023-09-11T06:23:02.967Z
I think this has been answered before. Does [this](https://3dprinting.stackexchange.com/a/14680/) or more specifically, [this](https://stackoverflow.com/a/59966088/) answer your question? If not, how does the question differ?
0scar
CC BY-SA 4.0
2023-09-11T15:29:55.470Z
@Oscar: The latter one is useless, it's how to do send a single fire-and-forget command, not a dialogue question. The earlier one has bare minimum about the buffer and 'ok' but the answer is so perfunctory it's nowhere near sufficient. I could post a bounty there for 'improve details' if I had the points, otherwise I'm stuck with way too little. In particular not a peep about all the possible answers that are not 'ok'. Also, came by M400 all by myself; I don't think any answer that doesn't mention it is comprehensive enough.
SF.
CC BY-SA 4.0
2023-09-11T18:17:37.587Z
What do you mean by `interactive mode: connect to the printer over serial interface, then control the printer "as you go"`, that is not clear in your question. You mean to control the printer from the printer itself when connected over serial and from code, or just interface with the printer through code only? A programming interface only is exactly as described in the latter answer I linked, only your program will be bigger with more user interaction, upon quitting you should close the serial connection. I have done this multiple times that way.
0scar
CC BY-SA 4.0
2023-09-11T18:41:49.683Z
@Oscar: A shell, but you give commands in the language I provide, not directly in g-code. One your command can be one g-code command, or can be a sequence of 500. So your input is frozen until the printer completes the 500 commands. The sequence can easily be longer than Marlin's buffer, or contain unbufferable commands. You control the printer from a PC connected over USB.
SF.
CC BY-SA 4.0
2023-09-11T18:48:14.720Z
Yes, I should close upon quitting the interactive shell, but not after executing one command - after a command I await further user input, with connection kept live.
SF.
<g-code><serial-connection><python>
G-code dialog sequence over serial interface
SF.
# G-code dialog sequence over serial interface

(Asked by: username_0 on 2023-09-08)

I'm writing a Python module that allows new tricks with printing - it already can produce G-code I can put into a file, send to the printer and print out. No problem with generating the correct codes to print.

What I want to add to it is interactive mode: connect to the printer over serial interface, then control the printer "as you go", you enter a command, the printer responds. You run a procedure, the printer executes the sequence of G-codes, then the software returns to awaiting further commands. The Marlin website provides a comprehensive reference of all G-codes for me to use. What it lacks though is an intro about the protocol as a whole, especially the busy/wait and error responses, the timing of me sending the next command in sequence.

Can someone either explain that part in sufficient detail to allow me to write the code that knows when (and if) to send the next G-code command, and/or refer me to a resource that does just that?

**Tags:** g-code, serial-connection, python

> **Comments:**
> **username_1**: I think this has been answered before. Does [this](https://3dprinting.stackexchange.com/a/14680/) or more specifically, [this](https://stackoverflow.com/a/59966088/) answer your question? If not, how does the question differ?  
> **username_0**: @Oscar: The latter one is useless, it's how to do send a single fire-and-forget command, not a dialogue question. The earlier one has bare minimum about the buffer and 'ok' but the answer is so perfunctory it's nowhere near sufficient. I could post a bounty there for 'improve details' if I had the points, otherwise I'm stuck with way too little. In particular not a peep about all the possible answers that are not 'ok'. Also, came by M400 all by myself; I don't think any answer that doesn't mention it is comprehensive enough.  
> **username_1**: What do you mean by `interactive mode: connect to the printer over serial interface, then control the printer "as you go"`, that is not clear in your question. You mean to control the printer from the printer itself when connected over serial and from code, or just interface with the printer through code only? A programming interface only is exactly as described in the latter answer I linked, only your program will be bigger with more user interaction, upon quitting you should close the serial connection. I have done this multiple times that way.  
> **username_0**: @Oscar: A shell, but you give commands in the language I provide, not directly in g-code. One your command can be one g-code command, or can be a sequence of 500. So your input is frozen until the printer completes the 500 commands. The sequence can easily be longer than Marlin's buffer, or contain unbufferable commands. You control the printer from a PC connected over USB.  
> **username_0**: Yes, I should close upon quitting the interactive shell, but not after executing one command - after a command I await further user input, with connection kept live.  

---

## 0 answers

