-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send_to_Printer.py
35 lines (27 loc) · 915 Bytes
/
Send_to_Printer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys
import serial
import time
def send_to_3D_printer(file_path):
port = 'COM3' #COM3 is for Windows port; change to a valid port on another os
baudrate = 115200
# Open serial connection to the printer
try:
ser = serial.Serial(port, baudrate, timeout=2)
print("Connected to", ser.name)
time.sleep(2)
except serial.SerialException as e:
print("Failed to connect:", e)
exit()
# Read the G-code file
with open(file_path, 'r') as file:
gcode_commands = file.readlines()
# Send G-code commands to the printer
for command in gcode_commands:
ser.write(str.encode(command))
time.sleep(0.5)
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Usage: python script.py <file_path>')
sys.exit(1)
g_code_file_path = sys.argv[1]
send_to_3D_printer(g_code_file_path)