In a project I’m working on, I needed a way to control some peripherals from a UART-connection, and I want to be able to set configuration-parameters as well as controlling stepper-motors and other equipment.
Having some prior experience with G-Code, and not needing a full set of example Marlin/RepRap-flavored G-Code, I decided to create my own implementation that can run on the microcontroller.
The syntax and format
The format and syntax for this simplified G-Code is quite easy to understand.
First of, the format.
Each command sent to the MCU will have to terminate the line with a line-feed (lf or \n), so that the command-buffer (more on that further down in this post) will know that it can parse the command received.
Each request to the MCU is structured in the following way:
[Command-set][command-code] <flag 0|1> <value>
The value is only needed when the flag is set to 1, omitting the flag will always set the flag to 0 as default behaviour.
The way this is formatted leads to clean and simple instructions, that are easy to follow along with, provided with a proper command-code table.
In my implementation, the command-sets are:
M - Configuration and machine-specific instructions. G - Movement and control of the connected equipment.
Each response is terminated by a line-feed, and the responses available are:
OK\n - configuration or movement received. OK <value>\n - Getting a configuration or movement feedback, separated by a space. ERROR\n - Unkown command-set. UNKNOWN\n - The command-set is recognized, but the instruction is unknown.
The code and how it works
Please see the embedded gist, I have written documentation for each step of the parsing in the code, so it should be easy to follow.
You can also try out the implementation over at Wokwi, or use the embed below.
(Click the green play-button, and a terminal will be shown in the simulation)
To test it, enter in M100, and you should receive OK 100 back into the console, to set the value, enter in M100 1 <value>.
Final words and thoughts
This implementation is quite simple, therefore I’ve chosen to call this implementation Simplified G-Code.
This code is written specifically for the Arduino UNO, but can easily be ported to other MCU’s and languages.
If you have any feedback, questions, or improvements, please leave a comment 🙂