注释
|
我正在使用这个脚本将文件发送到 arduino。 import serial
import time
import argparse
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 stream_gcode():
# with contect opens file/connection and closes it if function(with) scope is left
with open(args.file, "r") as file, serial.Serial(args.port, BAUD_RATE) as ser:
send_wake_up(ser)
for line in file:
# cleaning up gcode from file
gcode = remove_eol_chars(remove_comment(line))
if gcode: # checks if string is empty
print(f"Sending gcode: {gcode}")
# converts string to byte encoded string and append newline
command = str.encode(line + '\n')
ser.write(command) # Send g-code
grbl_out = ser.readline() # Wait for response with carriage return
print(f" : {grbl_out.strip().decode('utf-8')}")
time.sleep(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='This is a basic gcode sender. http://crcibernetica.com')
parser.add_argument('-p', '--port', help='Input USB port', required=True)
parser.add_argument('-f', '--file', help='Gcode file name', required=True)
args = parser.parse_args()
## show values ##
print("USB Port: %s" % args.port)
print("Gcode file: %s" % args.file)
stream_gcode()
|
作者
|
@statist32非常感谢你帮助我。我现在还在想办法。 |
|
@SHAO-1120 我现在不生成 gcode。我目前正在开发一个 python 接口来发送 gcode 以进行分配。 |


大家好,
我在做一个小项目,需要用python3来控制项目。我在互联网上发现的大多数情况和问题都是由软件控制的,例如 Grbl 控制器。有一个名为“stream.py”的控制代码。我不熟悉 Python。因此我不确定它是否可以用于通过 G 代码控制电机。
问题是有两个文件名为“gcode_file”和“device_file”。关于gcode_file,我认为它是一个包含G代码顺序的文件。但我不知道它的格式。另一个文件是device_file,它似乎与arduino uno的COM端口有关。我通过删除第 66 行更改了 stream.py 中的代码,
并将第 88 行更改为
但是后来我在第 173 行遇到了 TypeError。
显示不支持 unicode 字符串,请编码为 bytes: ‘%\n’。
我想是不是因为我改了device_file导致的。
细节:
我是 python 和这种控制程序的初学者。我真的需要大家的帮助。
感谢您阅读本期杂志。