Welcome to Our Community

Some features disabled for guests. Register Today.

Python Connection to Blackbox grbl and sending G-Code

Discussion in 'Control Software' started by domio, Aug 28, 2023.

  1. domio

    domio New
    Builder

    Joined:
    Aug 28, 2023
    Messages:
    2
    Likes Received:
    0
    Hi there,

    I'm trying to establish a connection to grbl on the X32 blackbox through the serial port. I haven't found anything that works out of the box. I'm not sure what I am missing. Maybe you could help me debug this, so I can have a working sample on GitHub that we can refer users too.

    Right now, it's stuck at "Sending: b'\r\n\r\n'"

    My basic understanding of initialization would be something like this:
    1. Wake up grbl → '\r\n\r\n'
    2. Reset errors → '$X'
    3. Homing → '$H'
    4. Send G-Code → 'G1 X100 Y100 Z0.0 F5000'

    The project is on GitHub: grbl-python/demo.py at main · dottenbr/grbl-python (github.com)

    Code:
    #!/usr/bin/env python
    
    import serial
    import time
    
    # Open grbl serial port
    s = serial.Serial('/dev/tty.usbserial-AQ02Z8ND', 11520) # put your serial port here
    print('Connected to: ' + s.name)
    print('Serial port open: ' + str(s.is_open))
    
    # Serial write and readline
    def send_gcode(gcode):
        s.write(str.encode(gcode))
        print('Sending: ' + str(str.encode(gcode)))
        print(s.readline().strip().decode('utf-8'))
    
    # Wake up grbl
    send_gcode('\r\n\r\n')
    time.sleep(2)   # Wait for grbl to initialize
    s.flushInput()  # Flush startup text in serial input
    send_gcode('$X') # Reset errors
    send_gcode('$H') # Homing
    send_gcode('G1 X100 Y100 Z0.0 F5000') # Go to X:100, Y:100, Z:0.0 at 5000 mm/min
    
    # Close serial port
    s.close()
     
  2. Peter Van Der Walt

    Peter Van Der Walt OpenBuilds Team
    Staff Member Moderator Builder Resident Builder

    Joined:
    Mar 1, 2017
    Messages:
    14,701
    Likes Received:
    4,249
    domio and David the swarfer like this.
  3. domio

    domio New
    Builder

    Joined:
    Aug 28, 2023
    Messages:
    2
    Likes Received:
    0
    Thank you for the hint, the baud rate was obviously wrong.

    I fixed that, and I also realized that I was missing the carriage return '\n'. I have updated the code on GitHub and I have now included a jog mode with which you can use the arrow keys to move the head as well as homing, centering and going to zero coordinate.

    I hope this helps other people who are trying to communicate with Black Box through Python.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice