Strategy to stream large gcode file from serial port 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())
Well, after some research I found something about Buffering on the RepRap documentation. I didn't see it before.

Works like this:


You send a command. An "ok" response it's received immediatly, that means that the command was successsfuly added to the buffer.
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 on the RepRap forum was also useful.