Skip to content

Commit ce46553

Browse files
committed
Merge branch 'dev'
2 parents 38c8b86 + f6b180b commit ce46553

File tree

5 files changed

+69
-26
lines changed

5 files changed

+69
-26
lines changed

dist/index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ var removeSpaces = function removeSpaces(s) {
4444
return s.replace(/\s+/g, '');
4545
};
4646

47+
// http://reprap.org/wiki/G-code#Special_fields
48+
// The checksum "cs" for a GCode string "cmd" (including its line number) is computed
49+
// by exor-ing the bytes in the string up to and not including the * character.
50+
var computeChecksum = function computeChecksum(s) {
51+
var cs = 0;
52+
s = s || '';
53+
for (var i = 0; i < s.length; ++i) {
54+
var c = s[i].charCodeAt(0);
55+
cs = cs ^ c;
56+
}
57+
return cs;
58+
};
59+
4760
var GCodeParser = (function (_Transform) {
4861
_inherits(GCodeParser, _Transform);
4962

@@ -78,8 +91,8 @@ var GCodeParser = (function (_Transform) {
7891
return;
7992
}
8093

81-
var n = undefined;
82-
var checksum = undefined;
94+
var n = undefined; // Line number
95+
var cs = undefined; // Checksum
8396
var words = [];
8497
var list = removeSpaces(line).match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
8598

@@ -104,20 +117,28 @@ var GCodeParser = (function (_Transform) {
104117

105118
{
106119
// *: Checksum
107-
if (letter === '*' && _lodash2.default.isUndefined(checksum)) {
108-
checksum = Number(argument);
120+
if (letter === '*' && _lodash2.default.isUndefined(cs)) {
121+
cs = Number(argument);
109122
return;
110123
}
111124
}
112125

113126
words.push([letter, argument]);
114127
});
115128

129+
// Exclude * (Checksum) from the line
130+
if (line.lastIndexOf('*') >= 0) {
131+
line = line.substr(0, line.lastIndexOf('*'));
132+
}
133+
116134
var obj = {};
117135
obj.line = line;
118136
obj.words = words;
119-
typeof n !== 'undefined' && (obj.N = n); // N: Line number
120-
typeof checksum !== 'undefined' && (obj.checksum = checksum); // *: Checksum
137+
typeof n !== 'undefined' && (obj.N = n); // Line number
138+
typeof cs !== 'undefined' && (obj.cs = cs); // Checksum
139+
if (obj.cs && computeChecksum(line) !== obj.cs) {
140+
obj.err = true; // checksum failed
141+
}
121142

122143
_this2.push(obj);
123144
});

index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ const removeSpaces = (s) => {
1919
return s.replace(/\s+/g, '');
2020
};
2121

22+
// http://reprap.org/wiki/G-code#Special_fields
23+
// The checksum "cs" for a GCode string "cmd" (including its line number) is computed
24+
// by exor-ing the bytes in the string up to and not including the * character.
25+
const computeChecksum = (s) => {
26+
let cs = 0;
27+
s = s || '';
28+
for (let i = 0; i < s.length; ++i) {
29+
let c = s[i].charCodeAt(0);
30+
cs = cs ^ c;
31+
}
32+
return cs;
33+
};
34+
2235
class GCodeParser extends Transform {
2336
constructor(options) {
2437
super(_.extend({}, options, { objectMode: true }));
@@ -43,8 +56,8 @@ class GCodeParser extends Transform {
4356
return;
4457
}
4558

46-
let n;
47-
let checksum;
59+
let n; // Line number
60+
let cs; // Checksum
4861
let words = [];
4962
let list = removeSpaces(line)
5063
.match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
@@ -68,20 +81,28 @@ class GCodeParser extends Transform {
6881
}
6982

7083
{ // *: Checksum
71-
if (letter === '*' && _.isUndefined(checksum)) {
72-
checksum = Number(argument);
84+
if (letter === '*' && _.isUndefined(cs)) {
85+
cs = Number(argument);
7386
return;
7487
}
7588
}
7689

7790
words.push([letter, argument]);
7891
});
7992

93+
// Exclude * (Checksum) from the line
94+
if (line.lastIndexOf('*') >= 0) {
95+
line = line.substr(0, line.lastIndexOf('*'));
96+
}
97+
8098
let obj = {};
8199
obj.line = line;
82100
obj.words = words;
83-
(typeof(n) !== 'undefined') && (obj.N = n); // N: Line number
84-
(typeof(checksum) !== 'undefined') && (obj.checksum = checksum); // *: Checksum
101+
(typeof(n) !== 'undefined') && (obj.N = n); // Line number
102+
(typeof(cs) !== 'undefined') && (obj.cs = cs); // Checksum
103+
if (obj.cs && (computeChecksum(line) !== obj.cs)) {
104+
obj.err = true; // checksum failed
105+
}
85106

86107
this.push(obj);
87108
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gcode-parser",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "A G-code parser for Node.js",
55
"author": "Cheton Wu <[email protected]>",
66
"homepage": "https://github.com/cheton/gcode-parser",

test/fixtures/special-fields.gcode

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ N4 G92 E0*67
33
N5 G28*22
44
N6 G1 F1500.0*82
55
N7 G1 X2.0 Y2.0 F3000.0*85
6-
N8 G1 X3.0 Y3.0*33
6+
N8 G1 X3.0 Y3.0*30 ; checksum failed

test/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,38 +145,39 @@ console.log(sampleText, err, results);
145145
let expectedResults = [
146146
{
147147
N: 3,
148-
checksum: 57,
149-
line: 'N3 T0*57',
148+
cs: 57,
149+
line: 'N3 T0',
150150
words: [['T', 0]]
151151
},
152152
{
153153
N: 4,
154-
checksum: 67,
155-
line: 'N4 G92 E0*67',
154+
cs: 67,
155+
line: 'N4 G92 E0',
156156
words: [['G', 92], ['E', 0]]
157157
},
158158
{
159159
N: 5,
160-
checksum: 22,
161-
line: 'N5 G28*22',
160+
cs: 22,
161+
line: 'N5 G28',
162162
words: [['G', 28]]
163163
},
164164
{
165165
N: 6,
166-
checksum: 82,
167-
line: 'N6 G1 F1500.0*82',
166+
cs: 82,
167+
line: 'N6 G1 F1500.0',
168168
words: [['G', 1], ['F', 1500]]
169169
},
170170
{
171171
N: 7,
172-
checksum: 85,
173-
line: 'N7 G1 X2.0 Y2.0 F3000.0*85',
172+
cs: 85,
173+
line: 'N7 G1 X2.0 Y2.0 F3000.0',
174174
words: [['G', 1], ['X', 2], ['Y', 2], ['F', 3000]]
175175
},
176176
{
177+
err: true, // checksum failed
177178
N: 8,
178-
checksum: 33,
179-
line: 'N8 G1 X3.0 Y3.0*33',
179+
cs: 30, // invalid checksum
180+
line: 'N8 G1 X3.0 Y3.0',
180181
words: [['G', 1], ['X', 3], ['Y', 3]]
181182
}
182183
];

0 commit comments

Comments
 (0)