Page 1 of 1

Using Python (pyserial) to send gcode commands to M2 printer

Posted: Thu Feb 01, 2018 12:52 pm
by sorna
Hey everyone,

Running into a bit of trouble as I'm trying to send serial gcode commands from my Windows PC to my M2 printer via USB.
I'm doing this through Python's pyserial package: https://pythonhosted.org/pyserial/index.html

For example, I just wanted to send a simple command to home the X axis "G28 X"

In Python using pyserial, this process looks something like this:

Code: Select all

import pyserial
ser = serial.Serial('COM6', 115200) # I've verified that this is the proper COM port, second argument is baud rate, which I actually wasn't all that sure of, but I tried both 115200 and 9600 since they are both pretty common. I didn't bother tuning any other COM port settings.
ser.write(b'G28 X\n') # My second issue comes up when I'm not sure of the line ending character that the M2 expects (maybe \n, \r or even \r\n)
ser.close()
Altogether is seems like a very straightforward set of commands, so I'm a bit frustrated with the fact that is doesn't cause the M2 to actually do anything.

As of right now with the USB connected, when I send commands I hear the fans in the M2 momentarily slow down, which is typically a sign that the serial bus has received something (may or may not be formatted correctly), so I know that I have the right port and that my code is effectively sending something.

I just wanted to see if anyone has any experience with sending these gcode commands over serial using Python and if they have any other debug steps I could try.

Thanks!

Re: Using Python (pyserial) to send gcode commands to M2 pri

Posted: Thu Feb 01, 2018 5:47 pm
by insta
Baud rate on the Marlin firmware is most commonly 250000, followed by 115200.

Marlin expects a newline character. Make sure you provide ample time for the command to buffer and send (is there an explicit "flush" you can call?)

Re: Using Python (pyserial) to send gcode commands to M2 pri

Posted: Thu Feb 01, 2018 6:25 pm
by sorna
insta,

Thanks so much for getting back to me. I did a bit more tinkering around and discovered a few things. Turns out it's important to give your serial port time to initialize before shoving commands through it, haha!
I added a two second command prior to sending my first commands, and that paired with /r/n line ending characters did the trick. I kept the baudrate at 115200 and haven't had any issues yet.

Thanks!

Re: Using Python (pyserial) to send gcode commands to M2 pri

Posted: Thu Feb 01, 2018 7:32 pm
by ednisley
sorna wrote:ser.write(b'G28 X\n')
G-Code syntax is inscrutable, unpredictable, unforgiving, and depends on which version of Marlin you're firing commands into. In this case, I think the coordinate name must be followed by a numeric value, even when it's not semantically necessary. Perhaps G28 X0\n will produce better results.

Trying commands "by hand" can help flush out similar problems, using whatever terminal program you have available; the Arduino IDE's serial monitor will suffice. Make sure you know which line ending characters it automagically adds to your text.

Re: Using Python (pyserial) to send gcode commands to M2 printer

Posted: Mon Apr 27, 2020 11:15 pm
by jahnavikachhia
Hi Sorna,

Can you tell me how did you fixed it by adding wait time? I am still unable to connect my said smart ender-3 pro printer serially with my laptop. If anyone has idea please let me know.