-
Notifications
You must be signed in to change notification settings - Fork 10
/
simple_stream.py
102 lines (72 loc) · 2.79 KB
/
simple_stream.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
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
"""
This is a simple script that attempts to connect to the GRBL controller at
> /dev/tty.usbserial-A906L14X
It then reads the grbl_test.gcode and sends it to the controller
The script waits for the completion of the sent line of gcode before moving onto the next line
tested on
> MacOs Monterey arm64
> Python 3.9.5 | packaged by conda-forge | (default, Jun 19 2021, 00:24:55)
[Clang 11.1.0 ] on darwin
> Vscode 1.62.3
> Openbuilds BlackBox GRBL controller
> GRBL 1.1
"""
import serial
import time
from threading import Event
BAUD_RATE = 115200
def remove_comment(string):
if (string.find(';') == -1):
return string
else:
return string[:string.index(';')]
def remove_eol_chars(string):
# removed \n or traling spaces
return string.strip()
def send_wake_up(ser):
# Wake up
# Hit enter a few times to wake the Printrbot
ser.write(str.encode("\r\n\r\n"))
time.sleep(2) # Wait for Printrbot to initialize
ser.flushInput() # Flush startup text in serial input
def wait_for_movement_completion(ser,cleaned_line):
Event().wait(1)
if cleaned_line != '$X' or '$$':
idle_counter = 0
while True:
# Event().wait(0.01)
ser.reset_input_buffer()
command = str.encode('?' + '\n')
ser.write(command)
grbl_out = ser.readline()
grbl_response = grbl_out.strip().decode('utf-8')
if grbl_response != 'ok':
if grbl_response.find('Idle') > 0:
idle_counter += 1
if idle_counter > 10:
break
return
def stream_gcode(GRBL_port_path,gcode_path):
# with contect opens file/connection and closes it if function(with) scope is left
with open(gcode_path, "r") as file, serial.Serial(GRBL_port_path, BAUD_RATE) as ser:
send_wake_up(ser)
for line in file:
# cleaning up gcode from file
cleaned_line = remove_eol_chars(remove_comment(line))
if cleaned_line: # checks if string is empty
print("Sending gcode:" + str(cleaned_line))
# converts string to byte encoded string and append newline
command = str.encode(line + '\n')
ser.write(command) # Send g-code
wait_for_movement_completion(ser,cleaned_line)
grbl_out = ser.readline() # Wait for response with carriage return
print(" : " , grbl_out.strip().decode('utf-8'))
print('End of gcode')
if __name__ == "__main__":
# GRBL_port_path = '/dev/tty.usbserial-A906L14X'
GRBL_port_path = 'COM1'
gcode_path = 'grbl_test.gcode'
print("USB Port: ", GRBL_port_path)
print("Gcode file: ", gcode_path)
stream_gcode(GRBL_port_path,gcode_path)
print('EOF')