title: How to send one gcode command over USB?
body: <p>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.</p>
  
  <pre><code>import serial
  
  ser = serial.Serial("/dev/ttyUSB0", 115200)
  ser.write("G28\n")
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>ser.write("G28\r\n")
  ser.write(b'G28\r\n')
  ser.write(b'G28\n')
  ser.write(b'G28')
  ser.write("G28")
  </code></pre>
  
  <p>I have tried all these combinations, and I have also added:</p>
  
  <pre><code>time.sleep(5)
  </code></pre>
  
  <p>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.</p>
  
  <p>I have also tried adding:</p>
  
  <pre><code>ser.close()
  </code></pre>
  
  <p>to see if that would make a difference but it has not, as I know this is best practice anyway.</p>
  
  <p>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)</p>
  
  <p>I look forward to any help anyone can give me in regards to my code and what I may be doing wrong.</p>