How to send one gcode command over USB?
Asked Answered
A

2

8

I am trying to write a simple python script that sends a gcode command to my wanhao D9 motherboard printer, running Marlin. I am running the python script on a raspberry pi that is connected to the printer via USB.

import serial

ser = serial.Serial("/dev/ttyUSB0", 115200)
ser.write("G28\n")

I have read over 20 forum pages with simular problems and tried their answers such as changing the baud rate to 250000 and the following changes to the write function parameter:

ser.write("G28\r\n")
ser.write(b'G28\r\n')
ser.write(b'G28\n')
ser.write(b'G28')
ser.write("G28")

I have tried all these combinations, and I have also added:

time.sleep(5)

along with the relevant import statement for the time module at the top of my file. I added this line of code between my ser declaration and my ser.write function call.

I have also tried adding:

ser.close()

to see if that would make a difference but it has not, as I know this is best practice anyway.

No matter what combination of this code I used, when I run my python script my printer seems to restart (the screen changes from the home page to the opening wanhao logo and back to the home page)

I look forward to any help anyone can give me in regards to my code and what I may be doing wrong.

Aerodyne answered 28/1, 2020 at 21:10 Comment(3)
Do you actually connect to the device? Or do you just open the serial connection and start writing to it? Also, have you tried reading the response of the machine on the serial? Maybe it is telling you somethingPestilent
The first three lines of code in my question are my entire file, so as far as I know I am opening the serial connection and then writing to it. However I tried this code and connected to an Arduino Uno instead of the printer and I was able to read the code from the Arduino serial monitor. I have tried reading from ser also and it received nothing.Aerodyne
I'd recommend using a program like Putty (free) to initially talk to the device. This takes out a lot of the complexity of being sure your Python program is setup correctly. Once to can send commands and get the responses you expect in Putty, then I script up the sequence that worked in Python.Caricature
A
5

Adding an extra sleep period after my command fixed my issue. I can also now read back the initial set up feedback from the printer.

My final code without this is:

import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 115200)

time.sleep(2)
ser.write("G28\r\n")
time.sleep(1)
ser.close()

Thank you, to the users in this post for guiding me in the right direction Full examples of using pySerial package

This code works for me, I hope someone else finds it useful in the future too.

Aerodyne answered 29/1, 2020 at 11:27 Comment(0)
T
0

I ran into a similar problem on my Ender 3 Pro and fixed it by adding a timeout to the initialization of the serial connection:

import serial

ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=2)

g_code = 'G1 Z30.15\n'

ser.write(g_code.encode())
response = ser.readline()
print(response.decode())
ser.close()
Teshatesla answered 21/7, 2023 at 2:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.