Its easy to write some Gambas code to provide basic control of a 3D printer.
You just need a good G-code reference list.
These notes were born out of a few experiments I recently conducted with my Creality CR-10-V3My original idea was a stupid one; I wanted to be able to download G-code files to the printers SD card, then instruct it to print. The reason why this is stupid will become clear.
The starting point is to create a new Gambas GUI project, include the component gb.Net, and add a SerialPort object.
Then set the serial properties:-
Name: SerialPort3D ...or whatever
Then add a nice big TextArea and write some code to read the port;
Public Sub SerialPort3D_Read()
Dim strReceived As String
Try Read #SerialPort3D, strReceived, Lof(SerialPort3D)
If Error Then
strReceived = ""
TextArea1.Text = TextArea1.Text & strReceived
Me.Text = Error.Text
The next step is to add a routine to send data;
Public Sub SendSerialData(strMessage As String, blnLF As Boolean)
If blnLF Then
strMessage &= gb.Lf
Write #SerialPort3D, strMessage
Me.Text = Error.Text
To keep things simple, I'm going to open the serial port when the program loads and close it when the program closes;
Public Sub Form_Open()
Public Sub Form_Close()
With the printer connected via a suitable cable and having given the Linux user suitable permissions (http://captainbodgit.blogspot.com/2021/05/3d-printing-printer-to-linux-computer.html) running the program now will cause the printer to reset and send data.
Add a few constants to the Gambas code;
Const HOME_PRINT_HEAD As String = "G28"
Const LIST_SD_CARD As String = "M20"
Const INIT_SD_CARD As String = "M21"
Const FIRMWARE_VERSION As String = "M115"
Const LCD_MESSAGE As String = "M117 Controlled by Gambas!"
Then maybe add a few switches to enable the transmission of commands;
Public Sub btnHome_Click()
You can even write data to new files on the SD card. Here are a few more constants;
Const WRITE_TO_SD_START As String = "M28" '"M28 test.gco"
Const WRITE_TO_SD_STOP As String = "M29"
Const DELETE_SD_FILE As String = "M30"
Note that file names must be in the 8.3 format.
However, the problem is this; sliced files (g-code files) are large. The serial link only operates at a Baud rate of 115,200. So it takes a long time to download even the simplest of objects.
- even though I didn't managed to get optimised data transfer working, I decided that downloading files to the SD card would never be fast enough. Writing data to an Arduino via a simple serial link is never going to be as fast as writing to a USB port on (say) a RaspberryPi...one is a controller, the other is a mini computer
- for object files that I can print in a few hours, its better to use Cura to control the printer while it prints. The only downside is that it can make other applications sluggish...such as writing this blog
- for objects that take much longer (and for over-night printing) its better to load the file to the printer SD card and run from there.
Marlin G-code Reference: https://marlinfw.org/meta/gcode/
For more posts like this, click the 3D Printing tab at the top of the page
Was looking for some tips on writing to USB Serial with Gambas and stumbled upon your site. To my surprise, you're communicating with your 3D printer as well. This is exactly what I was looking for. I am only looking at writing something simple to assist with accurately tramming my bed (leveling it).ReplyDelete
I'm surprised by the file management capabilities of these printers. I wouldn't have expected it could be controlled by G-code commands. I find it a little funny how you found it unexceptionably slow to send via a baud of 115200. I have some machines at work that I often have to send large files over 9600 baud. We've essentially dedicated an entire PC for the purpose.
I'm glad this was of some use to you.Delete
Yep, I didn't think about how slow it was going to be before I set about trying out my idea! But that's the way I work...I like to try things.
If you come up with an interesting project, or just would like to share a few code snippets, post them on https://forum.gambas.one/ as I would be very interested.