2016年5月28日 星期六



#!/usr/bin/python3
import time
import serial # sudo apt-get install python3-serial
import sys

argc = len(sys.argv)

if argc < 3:
    print("Usage: " + sys.argv[0] + " <com_port> <gcode_file>")
    sys.exit()

print("Open COM port: " + sys.argv[1])
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port = sys.argv[1],
    baudrate = 115200,
    parity = serial.PARITY_NONE,
    stopbits = serial.STOPBITS_ONE,
    bytesize = serial.EIGHTBITS,
    timeout = None,
    rtscts = None,
    dsrdtr = None
)
# wait for welcome message and flush it
# when open device node, cnc controller will reset and print message
time.sleep(1)
ser.flushInput()

# load ngc file
print("Open Gcode file: " + sys.argv[2])
f = open(sys.argv[2])
lines = f.readlines()
f.close()

# feed g codes line by line
for line in lines:
    if line[0] == ';':
        # drop comment
        continue;
    line_for_print = line.strip()
    print("Send: " + line_for_print)
    ser.write(str.encode(line))
    line_bytes = bytearray()
    while 1:
        c = ser.read()
        if c == b'\n':
            print("Recv: " + line_bytes.decode("utf-8"))
            break
        elif c >= b' ':
            # drop other control character such as 0xD
            line_bytes += c

ser.close()

Reference : http://wukcsoft.blogspot.tw/2014/12/cnc-if.html

沒有留言:

張貼留言