File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ const parseLine = (() => {
72
72
const re3 = new RegExp ( / \s + / g) ;
73
73
return ( line => line . replace ( re1 , '' ) . replace ( re2 , '' ) . replace ( re3 , '' ) ) ;
74
74
} ) ( ) ;
75
- const re = / ( % .* ) | ( (?: \$ \$ ) | (?: \$ [ a - z A - Z 0 - 9 # ] * ) ) | ( [ a - z A - Z ] [ 0 - 9 \+ \- \. ] * ) | ( \* [ 0 - 9 ] + ) / igm;
75
+ const re = / ( % .* ) | ( { . * ) | ( (?: \$ \$ ) | (?: \$ [ a - z A - Z 0 - 9 # ] * ) ) | ( [ a - z A - Z ] [ 0 - 9 \+ \- \. ] * ) | ( \* [ 0 - 9 ] + ) / igm;
76
76
77
77
return ( line , options ) => {
78
78
options = options || { } ;
@@ -93,6 +93,8 @@ const parseLine = (() => {
93
93
let cs ; // Checksum
94
94
const words = stripComments ( line ) . match ( re ) || [ ] ;
95
95
96
+ console . log ( words ) ;
97
+
96
98
for ( let i = 0 ; i < words . length ; ++ i ) {
97
99
const word = words [ i ] ;
98
100
const letter = word [ 0 ] . toUpperCase ( ) ;
@@ -105,6 +107,12 @@ const parseLine = (() => {
105
107
continue ;
106
108
}
107
109
110
+ // Parser JSON commands for TinyG and g2core
111
+ if ( letter === '{' ) {
112
+ result . cmds = ( result . cmds || [ ] ) . concat ( line . trim ( ) ) ;
113
+ continue ;
114
+ }
115
+
108
116
// Parse $ commands for Grbl
109
117
// - $C Check gcode mode
110
118
// - $H Run homing cycle
Original file line number Diff line number Diff line change @@ -72,6 +72,24 @@ describe('gcode-parser', () => {
72
72
expect ( data . cmds ) . to . deep . equal ( [ '$H' , '$C' ] ) ;
73
73
done ( ) ;
74
74
} ) ;
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
+ } ) ;
75
93
it ( 'should be able to parse % command (e.g. bCNC, CNCjs).' , ( done ) => {
76
94
{ // %wait
77
95
const data = parseLine ( '%wait' ) ;
You can’t perform that action at this time.
0 commit comments