Software Development - Grbl GUI

I’ve been doing a bit of researching an it looks like you just need to write to the serial port the arduino is on. I found this on microsoft’s site:

Sub SendSerialData(ByVal data As String)
    ' Send strings to a serial port.
    Using com1 As IO.Ports.SerialPort = 
            My.Computer.Ports.OpenSerialPort("COM1")
        com1.WriteLine(data)
    End Using
End Sub

There is also a nice little example program on the grbl wiki showing how to communicate written in python.

import serial
import time

# Open grbl serial port
s = serial.Serial('/dev/tty.usbmodem1811',115200)

# Open g-code file
f = open('grbl.gcode','r');

# Wake up grbl
s.write("\r\n\r\n")
time.sleep(2)   # Wait for grbl to initialize 
s.flushInput()  # Flush startup text in serial input

# Stream g-code to grbl
for line in f:
    l = line.strip() # Strip all EOL characters for consistency
    print 'Sending: ' + l,
    s.write(l + '\n') # Send g-code block to grbl
    grbl_out = s.readline() # Wait for grbl response with carriage return
    print ' : ' + grbl_out.strip()

# Wait here until grbl is finished to close serial port and file.
raw_input("  Press <Enter> to exit and disable grbl.") 

# Close file and serial port
f.close()
s.close()

What I’m failing to wrap my head around is how to implement an NC file. I think I’m going to move this to a new thread!

Thanks Rusty, that will get me started. It has been a while since I wrote any real code.

Ya let me know what you find out! I wouldn’t mind diving into this, but sadly time is a rare commodity these days.

It is actually a lot easier than that. Just drag this guy onto the form.

You will need to deal with threading issues, but a simple google search will show you how to deal with that. The serial data will come from a separate thread than the form controls.

Here is a way to text text from another thread.

Delegate Sub SetTextDelagate(ByVal ctrl As Control, ByVal Text As String)

Public Sub SetText(ByVal ctrl As Control, ByVal Text As String)
    If ctrl.InvokeRequired Then
        Dim del = New SetTextDelagate(AddressOf SetText)
        ctrl.Invoke(del, ctrl, Text)
    Else
        ctrl.Text = Text
    End If
End Sub
1 Like

Awesome, It’s been a little while since I’ve used Studio, looks like it might not be too bad.

When you are loading gcode in, are you sending your program an NC file? If so how do you allow your code to read it?

Yes I understand that. I’m curious if it just the same as sending a text file.

The one line of code in the example program reads this:

# Open g-code file
f = open('grbl.gcode','r');

Would it be just as simple as sending the NC file there? I may not be explaining my thought process well, so I apologize for that.

This is worth a read:

Perfect, thanks! This is what I was thinking would be the case, but I’m not entirely familiar with this process.

Yup, in my head I was thinking that it would be exactly that, the user would choose the file they wanted to load.

I’ve had some experience programming in Visual Basic, just some simple applications to aid in my day to day work at the company I work for. Basically I know just enough to be dangerous. Thanks for your help!

This is great! Exactly what I was looking for, a simple program just so I could wrap my head around the basic concepts! Thank you!

You guys are going pretty “old school” on everything. There are some pretty powerful objects to deal with most of this stuff. Here is how I read the file. (sorry about how Discuss is trying to color the lines.

 Dim reader As New System.IO.StreamReader(mFilename)

            Do While Not reader.EndOfStream
                readline = reader.ReadLine().Trim()  'trim of the white space (especially the line end stuff)
                If readline <> "" Then  'is there anythin left after the trim?
                    ' do something with readline.
                End If
            Loop
1 Like

This is perfect! Thanks. So during the if statement is that where you would print the line out to the arduino?

I find as I’m writing code, I’m usually learning. More times than not I’ll find out the huge block of code I just spent hours on can be summed up with some super simple built in function/method! A bit frustrating sometimes, but there always seems to be a better, simpler way out there!

No, that is just reading a file. Read the grbl wiki. Grbl can only handle a few lines at a time. Overly simplified…you send it a line, wait for an “OK”, then send the next.

My program rolls all this into one high level object called “GrblInterface”. It handles everything.

  • Serial port
  • Protocol (the "OK"s)
  • Timers
  • Parsing of responses
  • Real time Status (including where grbl is in 3D space)

All you need to do is design a local interface. My object generates events when status changes so you can update the interface. I’ll try to clean that up and publish it in the week or two.

1 Like

Awesome, thanks for all the info. Like I said I know just enough to be dangerous. Once I dive into it I’m sure I’ll get the hang of it. Seeing your code would greatly expand my understanding of this! I’m not too bad at following along code, as I generally am searching online for little snippets here and there when I’m coding!

Looking forward to seeing what you came up with!

I uploaded a demo of the GrblInterface Class I use to GitHub. I put a super simple GUI with it. The GUI is not meant for actual use. It just demos the use of the class.

It is not the Tablet Based GUI. I am still testing that. The GrblInterface Class might be updated due to needs of the Table GUI

There is no compiled version or setup program. The repo is targeted at VB.Net programmers.

3 Likes

Thank you for posting the demo code! It is really well commented and easy to follow. I never thought I would be writing VB code again, but this opens up a whole new world of possibilities of things we can make the X-Carve do.

1 Like

This is great thanks a lot! I will have to look through it more thoroughly, I did a quick skim and it does look very well commented! Good work!