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

Ask the MakerGear community for assistance...
Post Reply
sorna
Posts: 3
Joined: Thu Feb 01, 2018 12:37 pm

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

Post by sorna » Thu Feb 01, 2018 12:52 pm

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!

User avatar
insta
Posts: 2018
Joined: Tue Sep 16, 2014 3:59 am

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

Post by insta » Thu Feb 01, 2018 5:47 pm

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?)
Custom 3D printing for you or your business -- quote [at] pingring.org

sorna
Posts: 3
Joined: Thu Feb 01, 2018 12:37 pm

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

Post by sorna » Thu Feb 01, 2018 6:25 pm

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!

User avatar
ednisley
Posts: 1020
Joined: Fri Apr 11, 2014 5:34 pm
Location: Halfway up the Hudson
Contact:

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

Post by ednisley » Thu Feb 01, 2018 7:32 pm

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.

Post Reply