{"id":13650519,"url":"https://github.com/ryansturmer/node-gcode","last_synced_at":"2025-04-22T18:31:50.390Z","repository":{"id":25596364,"uuid":"29031174","full_name":"ryansturmer/node-gcode","owner":"ryansturmer","description":"GCode interpreter and simulator for node.js","archived":false,"fork":false,"pushed_at":"2016-02-04T19:51:01.000Z","size":25,"stargazers_count":32,"open_issues_count":2,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-03-25T00:21:01.726Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryansturmer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-09T19:15:26.000Z","updated_at":"2023-11-26T16:28:10.000Z","dependencies_parsed_at":"2022-07-12T16:08:06.323Z","dependency_job_id":null,"html_url":"https://github.com/ryansturmer/node-gcode","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryansturmer%2Fnode-gcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryansturmer%2Fnode-gcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryansturmer%2Fnode-gcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryansturmer%2Fnode-gcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryansturmer","download_url":"https://codeload.github.com/ryansturmer/node-gcode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223903112,"owners_count":17222491,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T02:00:37.487Z","updated_at":"2024-11-10T01:30:52.022Z","avatar_url":"https://github.com/ryansturmer.png","language":"JavaScript","readme":"# node-gcode\nGCode interpreter and simulator for node.js\n\nMost of the function of this interpreter is derived from the [NIST G-code standard.](http://www.nist.gov/customcf/get_pdf.cfm?pub_id=823374)\n\n\n\nInstallation\n------------\nInstall from npm\n```\nnpm install gcode\n```\n\nParsing a G-code File\n---------------------\n```js\ngcode = require('gcode')\ngcode.parseFile('example.nc', function(err, result) {\n    console.log(JSON.stringify(result));\n})\n```\n\nThe data returned by the `parseFile` callback is a list of G-code blocks, where each block is an object with a `num` property (the G-code line number) and a `words` property (the list of G-code words in that block) Each G-code word is a list of two items, the word letter (G, M, X,Y,Z, etc.) and the word argument.  Word arguments are typically numbers, but the parser supports full expressions, including parameter values, so in the event that an expression or parameter value is provided, an expression-tree-like object is returned that must be evaluated.  Currently, this is left as an exercise for the reader.\n```gcode\nG17\nG1 X1 Y2 Z3 F120\nG1 Z0\nG1 X-0.125 F240\nG0 X[3+5]\n```\n\nThe output of the above example might look like this _(note the last line that includes 3+5 in the X-axis word)_:\n\n```js\n[{\"N\":null,\"words\":[[\"G\",17]]},\n {\"N\":null,\"words\":[[\"G\",1],[\"X\",1],[\"Y\",2],[\"Z\",3],[\"F\",120]]},\n {\"N\":null,\"words\":[[\"G\",1],[\"Z\",0]]},\n {\"N\":null,\"words\":[[\"G\",1],[\"Z\",-0.125],[\"F\",240]]},\n {\"N\":null,\"words\":[[\"G\",0],[\"X\",{\"left\":3,\"right\":5,\"op\":\"+\"}]]}]\n```\n\nParsing a G-code String\n-----------------------\n```js\ngcode = require('gcode');\ngcode.parseString('G0 X1 Y2 Z3', function(err, result) {\n   console.log(JSON.stringify(result)); \n});\n```\n\nSee `parseFile` above for the output format.  Strings work exactly the same way.  Strings can contain any number of codes, separated by newlines per the standard.  The output of the above example would be:\n\n```js\n[{\"N\":null,\"words\":[[\"G\",0], ['X',1],['Y',2],['Z',3]]}]\n```\n\nG-code Interpreters\n-------------------\nWriting a custom interpreter for G-code is easy with the `Interpreter` object provided by the gcode library.  To create your own interpreter, use the following example:\n\n```js\nInterpreter = require('gcode').Interpreter;\n\nvar MyGCodeRunner = function() {\n    this.units = 'imperial';\n    Interpreter.call(this);\n}\nutil.inherits(MyGCodeRunner, Interpreter)\n\nMyGCodeRunner.prototype.G0 = function(args) {\n    console.log(\"Got a G0 code!\");\n    console.log(args);\n}\n\nMyGCodeRunner.prototype.G20 = function(args) {\n    console.log(\"Switching to inches.\");\n    this.units = 'imperial';\n}\n\nMyGCodeRunner.prototype.G21 = function(args) {\n    console.log(\"Switching to millimeters.\");\n    this.units = 'metric';\n}\n\nrunner = new MyGCodeRunner();\n\nrunner.interpretFile('example.nc');\nrunner.interpretString('G0 X1 Y2 Z3\\nG4 P3');\n```\n\nAny handlers attached to the interpreter whose names correspond to G or M codes are called in turn as those codes are parsed from the incoming file stream.\n\nG-codes that contain decimal points (G38.2, G59.1, etc.) are represented as handler functions by replacing the decimal point in the code with an underscore.  Thus, the handler for G38.2 would be:\n\n```js\nMyGCodeRunner.prototype.G38_2 = function(args) {\n    console.log(\"Initiating a straight probe: \" + args);\n}\n```\n\nThere is a special handler for any unhandled codes, which is just an underscore.  If you define this method on your interpeter, it will be called for every unknown G or M code that is encountered.  Because it is for handling unknown G/M codes, it takes two arguments, the command, followed by the remaining words in the code:\n\n```js\nMyGCodeRunner.prototype._ = function(cmd, args) {\n    console.log('Got an unknown G/M code: ',cmd);\n    console.log('With arguments: ', args);\n}\n```\n","funding_links":[],"categories":["Software"],"sub_categories":["G-code"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryansturmer%2Fnode-gcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryansturmer%2Fnode-gcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryansturmer%2Fnode-gcode/lists"}