<p>Well, after some research I found something about Buffering <a href="https://reprap.org/wiki/G-code#Buffering" rel="nofollow noreferrer">on the RepRap documentation</a>. I didn't see it before.</p>

<p>Works like this:</p>

<ol>
<li>You send a command. An "ok" response it's received immediatly, that means that the command was successsfuly added to the buffer.</li>
<li>Then you can send another command. When the buffer is full, the "ok" response will be delayed until a new command it's received.</li>
</ol>

<p>Some commands, for example <strong>M190: Wait for bed temperature to reach target temp</strong> will cause that the "ok" response will be delayed until the command finish his execution. That means that the printer won't accept more commands in the meantime.</p>

<p>I still have to check how many commands the buffer can save approximately, and if it's convenient to fill it entirely. I guess not, because let's say you want to pause the print. If you added a lot of commands to the buffer, you'll not able to pause it until all of them finished.</p>

<p><a href="https://reprap.org/forum/read.php?1,819305,819482" rel="nofollow noreferrer">This thread</a> on the RepRap forum was also useful.</p>

CC BY-SA 4.0
2021-08-27T20:51:57.793Z
Have you figured out how to tell how many the buffer currently holds? I'm trying to do the same but I'm not sure how to synchronise this. Thanks!
nyxaria
CC BY-SA 4.0
2018-11-17T23:02:07.813Z
2018-11-17T23:02:07.813Z
fsinisi90
<p>I'm connecting to a printer that's using <strong>Marlin 1.0.3 dev</strong> firmware through the <a href="https://github.com/pyserial/pyserial" rel="nofollow noreferrer">pyserial</a> Python package.</p>

<p>I want to stream large gcode files into the printer and I guess that the buffer has a limited size. How do I know when the buffer is full, so I can wait until a command is processed to send a new one?</p>

<p>I've tried reading a big file, sending all the commands and checking for the printer responses in another thread. I didn't get an error or something that indicates that the buffer is full. I've also didn't found anything about this in the <a href="http://marlinfw.org/meta/gcode/" rel="nofollow noreferrer">official docs</a>.</p>

<p>It's my approach correct? Is there something I'm missing?</p>

<p>Some of the code:</p>

<pre><code>def check_for_responses():
    while(True):
        response = printer_connection.serial.readline()
        print(response)


async def stream_gcode():
    await printer_connection.open_connection('COM7')
    Thread(target=check_for_responses).start()
    asyncio.sleep(2) # wait to initialize
    file = open("test.gcode", 'r')
    for line in file:
        command= printer_connection.format_command(line)
        printer_connection.serial.write(command.encode())

asyncio.new_event_loop().run_until_complete(stream_gcode())
</code></pre>

CC BY-SA 4.0
2018-11-17T14:48:22.417Z
Please explain how this is relevant to 3D printing. Your code excerp is too tiny and your explanation does throw up more questions for me: What is the problem, what is your expected behavior, where does it throw errors?
Trish
CC BY-SA 4.0
2018-11-17T15:27:41.600Z
I simply need to figure out a strategy to stream commands to the printer. I've added the code just as an example, but my question is more general. I don't think that reading the entire file and just sending it to the printer would be a good idea. Neither sending the commands one by one waiting for the response of each.
fsinisi90
CC BY-SA 4.0
2018-11-17T15:36:05.067Z
that... seems like Stack Overflow stuff. Programming the interface from 0 I mean. we are usually not programming interfaces here, we just use them...
Trish
CC BY-SA 4.0
2018-11-17T16:23:55.153Z
Ok, I will remove the question and ask there then. I thought this was the correct place. Anyway, do you know how the printer buffer works or where to find information about it?
fsinisi90
CC BY-SA 4.0
2018-11-17T19:47:45.817Z
The question in itself is ok, but we need more heft to be able to answer it here. You could though get faster help on SO about programming
Trish
<marlin>
Strategy to stream large gcode file from serial port
fsinisi90
# Strategy to stream large gcode file from serial port

(Asked by: username_0 on 2018-11-16)

I'm connecting to a printer that's using Marlin 1.0.3 dev firmware through the pyserial Python package.
I want to stream large gcode files into the printer and I guess that the buffer has a limited size. How do I know when the buffer is full, so I can wait until a command is processed to send a new one?
I've tried reading a big file, sending all the commands and checking for the printer responses in another thread. I didn't get an error or something that indicates that the buffer is full. I've also didn't found anything about this in the official docs.
It's my approach correct? Is there something I'm missing?
Some of the code:
def check_for_responses():
    while(True):
        response = printer_connection.serial.readline()
        print(response)


async def stream_gcode():
    await printer_connection.open_connection('COM7')
    Thread(target=check_for_responses).start()
    asyncio.sleep(2) # wait to initialize
    file = open("test.gcode", 'r')
    for line in file:
        command= printer_connection.format_command(line)
        printer_connection.serial.write(command.encode())

asyncio.new_event_loop().run_until_complete(stream_gcode())

**Tags:** marlin
> **Comments:**
> **username_1**: Please explain how this is relevant to 3D printing. Your code excerp is too tiny and your explanation does throw up more questions for me: What is the problem, what is your expected behavior, where does it throw errors?  
> **username_0**: I simply need to figure out a strategy to stream commands to the printer. I've added the code just as an example, but my question is more general. I don't think that reading the entire file and just sending it to the printer would be a good idea. Neither sending the commands one by one waiting for the response of each.  
> **username_1**: that... seems like Stack Overflow stuff. Programming the interface from 0 I mean. we are usually not programming interfaces here, we just use them...  
> **username_0**: Ok, I will remove the question and ask there then. I thought this was the correct place. Anyway, do you know how the printer buffer works or where to find information about it?  
> **username_1**: The question in itself is ok, but we need more heft to be able to answer it here. You could though get faster help on SO about programming  

---

## 1 answers

### Answer by username_0 on 2018-11-17. Score: 3 | Accepted answer

Well, after some research I found something about Buffering [on the RepRap documentation](https://reprap.org/wiki/G-code#Buffering). I didn't see it before.

Works like this:

1. You send a command. An "ok" response it's received immediatly, that means that the command was successsfuly added to the buffer.
2. Then you can send another command. When the buffer is full, the "ok" response will be delayed until a new command it's received.

Some commands, for example **M190: Wait for bed temperature to reach target temp** will cause that the "ok" response will be delayed until the command finish his execution. That means that the printer won't accept more commands in the meantime.

I still have to check how many commands the buffer can save approximately, and if it's convenient to fill it entirely. I guess not, because let's say you want to pause the print. If you added a lot of commands to the buffer, you'll not able to pause it until all of them finished.

[This thread](https://reprap.org/forum/read.php?1,819305,819482) on the RepRap forum was also useful.

> **Comments:**
> **username_2**: Have you figured out how to tell how many the buffer currently holds? I'm trying to do the same but I'm not sure how to synchronise this. Thanks!  

---

