Skip to main content

Automating tasks

Python g-code sender

Automating tasks can be done with Python script. Below is script example to send commands provided in text file:

import time
import os
from tqdm import tqdm
import serial
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-p','--port', help='COM port', required=True)
parser.add_argument('-f','--file', help='File name', required=True)
args = parser.parse_args()

ser = serial.Serial()
ser.port = str(args.port)
ser.baudrate = 115200
ser.timeout = 10
ser.open()
ser.flushInput()
ser.flushOutput()

print("Reading file...")
lines = []
with open(args.file) as fp:
   for cnt, line in enumerate(fp):
       lines.append(line)

print("Sleeping 1s...")
time.sleep(1)

for line in tqdm(lines):
    l = bytes(line.strip()+ '\n', 'utf8')
    ser.write(l)
    grbl_out = ser.readline()

Text file example:

G91

M7

M8
G1 X45 F20000
M9
G1 X45 F20000

M8
G1 X45 F20000
M9
G1 X45 F20000

M8
G1 X45 F20000
M9
G1 X45 F20000

M8
G1 X45 F20000
M9
G1 X45 F20000

G90