~hamner/Dice

Dice/gcode_send -rwxr-xr-x 2.9 KiB
ec09d624David Hamner Added support for GPX/Creator Pro 17 days ago
                                                                                
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
#GPL3 by David Hamner


import subprocess
import sys
from threading import Thread
import serial
import time
import os
import argparse

parser = argparse.ArgumentParser(description="Send G-code to a 3d-printer")
parser.add_argument("-d","--device",help="Serial device",required=True)
parser.add_argument("-b", "--baudrate", help="Baudrate of serial device (9600,115200,250000)", required=True)
parser.add_argument("-f","--file",help="G-code file path",required=True)

args = parser.parse_args()


crunch = serial.Serial(args.device, args.baudrate)
cmd_done = False

#CMDs without an retrun ok
no_ok = ["M105", "M82", "G90"]
time_out_at_some_point = ["G1"]
last_ok = time.time()

def process_output():
    global cmd_done
    global crunch
    global last_ok
    
    while True:
        output = crunch.readline().decode()
        output = output.strip("\r\n")
        if output.strip() == "":
            continue
        if output.startswith("ok") or output == "wait":
            if not cmd_done:
                print("CMD DONE!")
                cmd_done = True
            else:
                last_ok = time.time()
        print(f"'{output}'")
        

def gcode_cmd(command):
    global cmd_done
    global crunch
    global last_ok
    
    command = command.strip(" ")
    
    print(command)
    if ";" in command:
        command = command.split(";")[0]
    if not command.endswith("\n"):
        command = command + "\r\n"
    
    crunch.write(command.encode())
    crunch.flush()
    
    sub_cmd = command.strip("\r\n")
    if " " in sub_cmd:
        sub_cmd = sub_cmd.split(" ")[0]
    
    print(sub_cmd)
    if sub_cmd in no_ok:
        cmd_done = True
    else:
        cmd_done = False
    while not cmd_done:
        #print(time.time() - last_ok)
        if time.time() - last_ok < .01:
            print("Warning! OK processed already")
            last_ok = time.time()
            cmd_done = True
        if time.time() - last_ok > 90:
            if sub_cmd in time_out_at_some_point:
                print(f"Warning! Fake OK injected, TIMEOUT for {command}")
                last_ok = time.time()
                cmd_done = True
        time.sleep(.01)
        
    return()

# creating bg processing Thread
bg_output_processing = Thread(target = process_output)
bg_output_processing.setDaemon(True)                  
bg_output_processing.start()                
time.sleep(3)

#loading
#gcode_cmd("M109 S200")
#time.sleep(60000)

with open(args.file) as fh:
    for cmd_in_file in fh.readlines():
        if cmd_in_file.startswith(";"):
            continue
        if not os.path.isfile(args.file):
            print("Print file removed! Stopping")
            gcode_cmd("G91")
            gcode_cmd("G1 Z5:; Move up")
            gcode_cmd("M104 S0; tool off")
            gcode_cmd("M18; steppers off")
            break
        gcode_cmd(cmd_in_file)

print("Print done!")
crunch.close()