import serial import time # Define the serial port and baud rate for communication ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1) # Function to send G-code commands def send_gcode(ser, command): # Split the command into chunks of 120 characters or less chunks = [command[i:i + 120] for i in range(0, len(command), 120)] for chunk in chunks: ser.write((chunk + '\n').encode()) response = ser.readline().decode().strip() if response != 'ok': # Handle errors or unexpected responses here print(f"GRBL response: {response}") # Function to wait until the buffer is empty def wait_for_buffer_empty(): while True: status = send_gcode('?') if status.startswith('