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):
print(response)

async def stream_gcode():
await printer_connection.open_connection('COM7')
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())

• 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 Nov 17 '18 at 14:48
• 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 Nov 17 '18 at 15:27
• 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 Nov 17 '18 at 15:36
• 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 Nov 17 '18 at 16:23
• 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 Nov 17 '18 at 19:47