File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 - 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;
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
Original file line number Diff line number Diff 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' ) ;
You can’t perform that action at this time.
0 commit comments