python实现树莓派usb串口发送gcode

技术 · 2023-03-16 · 39 人浏览

python实现树莓派usb串口发送gcode

# encoding: utf-8
import serial


def post_gcode(x,com_x):
# x:gcode路径 com_x:串口我的是/dev/ttyUSB0
    ser = serial.Serial(com_x, 115200,timeout = 0.1)  # 打开COM8波特率115200 超时2秒
    f = open('./image/'+x+'.gcode', 'r')  # 只读模式打开文档打开Gcode文档
    ser.timeout = 40  # 读超时设置
    ser.writeTimeout = 2  # 写超时
    while True:
        # 获取数据
        line = f.readline()  # 按行读取
        if not line:  # 读到最后文档结尾
            f.close()  # 关闭文档
            break  # 退出循环
        # print(line)  # 打印数据
        cmd = line.encode()  # 对数据unicode编码成bytes
        # print(cmd)
        ser.write(cmd)  # 发送命令
        data = ser.readline()  # 读取返回数据 10s超时
        # print(data)  # 打印返回数据
    print('发送完成')
raspberry python Gcode 串口
Theme Jasmine by Kent Liao