<p>The process is pretty straightforward, you open a serial connection and send the instructions over the serial connection. The printer reports &quot;<code>OK</code>&quot; when <strong>received</strong> (<em>not</em> when the command has been executed).</p>
<p>You can use pySerial to connect to a USB port with the correct speed, then send the command you want to execute (include the newline char).</p>
<p>You need to be aware that commands are <a href="https://reprap.org/wiki/G-code#Buffering" rel="nofollow noreferrer">buffered</a>, if the buffer is full, the printer sends &quot;OK&quot; when there is a spot free in the buffer.</p>
<p>See <a href="https://stackoverflow.com/questions/59957089/how-to-send-one-gcode-command-over-usb">this question on a sister SE</a>.</p>

CC BY-SA 4.0
2020-10-27T14:58:37.273Z
2021-04-07T11:57:47.740Z
2021-04-07T11:57:47.740Z
0scar
<p>I'm working on a project for my masters where I'm trying to detect print errors using object detection and I want to be able to pause the printer when a potential fault is detected. However I can't seem to figure out the correct way to send G-code to my printer from a Python script. Does anyone happen to know how this works?</p>

CC BY-SA 4.0
2020-12-18T18:24:20.913Z
I'm working on an identical project, I couldn't find any contact in your profile. If you want to discuss/share approaches let me know. Via SO chat https://chat.stackoverflow.com/users/6859185/nickdgreg or my github is in my profile
NickDGreg
<g-code><usb><python>
How do you send G-code from a USB port with Python?
Nick
# How do you send G-code from a USB port with Python?

(Asked by: username_0 on 2020-10-27)

I'm working on a project for my masters where I'm trying to detect print errors using object detection and I want to be able to pause the printer when a potential fault is detected. However I can't seem to figure out the correct way to send G-code to my printer from a Python script. Does anyone happen to know how this works?

**Tags:** g-code, usb, python

> **Comments:**
> **username_1**: I'm working on an identical project, I couldn't find any contact in your profile. If you want to discuss/share approaches let me know. Via SO chat https://chat.stackoverflow.com/users/6859185/username_1 or my github is in my profile  

---

## 1 answers

### Answer by username_2 on 2020-10-27. Score: 1 | Accepted answer

The process is pretty straightforward, you open a serial connection and send the instructions over the serial connection. The printer reports "`OK`" when **received** (*not* when the command has been executed).

You can use pySerial to connect to a USB port with the correct speed, then send the command you want to execute (include the newline char).

You need to be aware that commands are [buffered](https://reprap.org/wiki/G-code#Buffering), if the buffer is full, the printer sends "OK" when there is a spot free in the buffer.

See [this question on a sister SE](https://stackoverflow.com/questions/59957089/how-to-send-one-gcode-command-over-usb).


---

