Skip to content

Commit e255421

Browse files
committed
Fixed a bug that cannot properly parse JSON commands
1 parent 77a6b7f commit e255421

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const parseLine = (() => {
7272
const re3 = new RegExp(/\s+/g);
7373
return (line => line.replace(re1, '').replace(re2, '').replace(re3, ''));
7474
})();
75-
const re = /(%.*)|((?:\$\$)|(?:\$[a-zA-Z0-9#]*))|([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm;
75+
const re = /(%.*)|({.*)|((?:\$\$)|(?:\$[a-zA-Z0-9#]*))|([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm;
7676

7777
return (line, options) => {
7878
options = options || {};
@@ -93,6 +93,8 @@ const parseLine = (() => {
9393
let cs; // Checksum
9494
const words = stripComments(line).match(re) || [];
9595

96+
console.log(words);
97+
9698
for (let i = 0; i < words.length; ++i) {
9799
const word = words[i];
98100
const letter = word[0].toUpperCase();
@@ -105,6 +107,12 @@ const parseLine = (() => {
105107
continue;
106108
}
107109

110+
// Parser JSON commands for TinyG and g2core
111+
if (letter === '{') {
112+
result.cmds = (result.cmds || []).concat(line.trim());
113+
continue;
114+
}
115+
108116
// Parse $ commands for Grbl
109117
// - $C Check gcode mode
110118
// - $H Run homing cycle

test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ describe('gcode-parser', () => {
7272
expect(data.cmds).to.deep.equal(['$H', '$C']);
7373
done();
7474
});
75+
it('should be able to parse JSON command (e.g. TinyG, g2core).', (done) => {
76+
{ // {sr:{spe:t,spd,sps:t}}
77+
const data = parseLine('{sr:{spe:t,spd:t,sps:t}}');
78+
expect(data).to.be.an('object');
79+
expect(data.line).to.be.an('string');
80+
expect(data.words).to.be.empty;
81+
expect(data.cmds).to.deep.equal(['{sr:{spe:t,spd:t,sps:t}}']);
82+
}
83+
{ // Request Motor Timeout: {mt:n}
84+
const data = parseLine('{mt:n}');
85+
expect(data).to.be.an('object');
86+
expect(data.line).to.be.an('string');
87+
expect(data.words).to.be.empty;
88+
expect(data.cmds).to.deep.equal(['{mt:n}']);
89+
}
90+
91+
done();
92+
});
7593
it('should be able to parse % command (e.g. bCNC, CNCjs).', (done) => {
7694
{ // %wait
7795
const data = parseLine('%wait');

0 commit comments

Comments
 (0)