6

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.

C.Gibbons
  • 183
  • 1
  • 11
  • 1
    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 something – lsabi Jan 28 '20 at 22:00
  • 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. – C.Gibbons Jan 29 '20 at 10:09
  • 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. – Michael Molter Apr 10 '23 at 16:02

2 Answers2

4

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.

C.Gibbons
  • 183
  • 1
  • 11
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()
grant-n
  • 74
  • 6