0

I have a python script that creates a serial connection to my Arduino-Mega (ramps1. 4). I am using the pyserial library, with which I send G-Code commands via the COM. There "Marlin 2.0x" reads the input and acts on any G-Code.

So far everything works. I can write any G-Code via serial.write() and Marlin understands it. But unfortunately I had to add a time delay if I want to act on multiple commands. Is there a nice way of circumventing that?

Here is an example code of Extruding 1mm of Filament twice.

import serial
import time  

ser1 = serial.Serial('COM3', 250000)
time.sleep(1)
ser1.write(('G92 E1\n').encode())
time.sleep(1)
ser1.write(('G92 E1\n').encode())

Ideally it'd look like this without the delays:

import serial
import time  

ser1 = serial.Serial('COM3', 250000)
ser1.write(('G92 E1\n').encode())
ser1.write(('G92 E1\n').encode())

But then commands get skipped.

CNord
  • 1
  • 1
    *"I send G-Code commands ... I want to act on multiple commands"* -- That's contradictory. *"Act on"* usually means "receive and/or process"* rather than transmit. *"I had to add a time delay"* -- Apparently you've written your code to *hose* the device with commands, and not bother to read any responses that would throttle transmission. RTM! https://github.com/MarlinFirmware/Marlin/blob/2.0.x/docs/Queue.md#synchronization ***"Marlin replies "ok" ... well-behaved hosts will wait for this message"*** – sawdust Feb 23 '22 at 00:02
  • *"Is there a nice way of circumventing that?"* -- Can't tell if you're properly leery of using fixed delays, or simply trying to keep your code short & simple. Perhaps you need to learn how to compose a [procedure](http://www.easypythondocs.com/procedures.html) (e.g. **send_gcode(...)**) that would perform both the write and then read the response for each command. – sawdust Feb 23 '22 at 00:33
  • hmm. well your example of "hosing the device with commands" is somewhat true. It seems I have a lot to learn with communication. But your link is a start. Thanks – CNord Feb 25 '22 at 13:23

0 Answers0