@@ -51,66 +51,67 @@ export const parseSerialMessages = (
51
51
messages : string [ ] ,
52
52
separator = "\r\n"
53
53
) : { [ key : string ] : number [ ] } => {
54
+ var re = new RegExp ( `(${ separator } )` , "g" ) ;
54
55
//add any leftover from the buffer to the first line
55
- messages [ 0 ] = buffer + messages [ 0 ] ;
56
-
57
- // add the last message to the buffer if incomplete
58
- if (
59
- messages [ messages . length - 1 ] . substr (
60
- messages [ messages . length - 1 ] . length - separator . length
61
- ) !== separator
62
- ) {
63
- buffer = messages [ messages . length - 1 ] ;
64
- messages . splice ( - 1 ) ;
65
- } else {
66
- buffer = "" ;
56
+ const messagesAndBuffer = ( buffer + messages . join ( "" ) )
57
+ . split ( re )
58
+ . filter ( ( message ) => message . length > 0 ) ;
59
+
60
+ // remove the previous buffer
61
+ buffer = "" ;
62
+ // check if the last message contains the delimiter, if not, it's an incomplete string that needs to be added to the buffer
63
+ if ( messagesAndBuffer [ messagesAndBuffer . length - 1 ] !== separator ) {
64
+ buffer = messagesAndBuffer [ messagesAndBuffer . length - 1 ] ;
65
+ messagesAndBuffer . splice ( - 1 ) ;
67
66
}
68
67
69
68
const newVars : { [ key : string ] : number [ ] } = { } ;
70
69
71
70
// for each line, explode variables
72
- messages . forEach ( ( message ) => {
73
- //there are two supported formats:
74
- // format1: <value1> <value2> <value3>
75
- // format2: name1:<value1>,name2:<value2>,name3:<value3>
76
-
77
- // if we find a comma, we assume the latter is being used
78
- let tokens : string [ ] = [ ] ;
79
- if ( message . indexOf ( "," ) > 0 ) {
80
- message . split ( / \s / ) . forEach ( ( keyValue : string ) => {
81
- let [ key , value ] = keyValue . split ( ":" ) ;
82
- key = key . trim ( ) ;
83
- value = value . trim ( ) ;
84
- if ( key . length > 0 && value . length > 0 ) {
85
- tokens . push ( ...[ key , value ] ) ;
86
- }
87
- } ) ;
88
- } else {
89
- // otherwise they are spaces
90
- const values = message . split ( / \s / ) ;
91
- values . forEach ( ( value , i ) => {
92
- if ( value . length ) {
93
- tokens . push ( ...[ `value ${ i } ` , value ] ) ;
94
- }
95
- } ) ;
96
- }
71
+ messagesAndBuffer
72
+ . filter ( ( message ) => message !== separator )
73
+ . forEach ( ( message ) => {
74
+ //there are two supported formats:
75
+ // format1: <value1> <value2> <value3>
76
+ // format2: name1:<value1>,name2:<value2>,name3:<value3>
77
+
78
+ // if we find a comma, we assume the latter is being used
79
+ let tokens : string [ ] = [ ] ;
80
+ if ( message . indexOf ( "," ) > 0 ) {
81
+ message . split ( "," ) . forEach ( ( keyValue : string ) => {
82
+ let [ key , value ] = keyValue . split ( ":" ) ;
83
+ key = key . trim ( ) ;
84
+ value = value . trim ( ) ;
85
+ if ( key . length > 0 && value . length > 0 ) {
86
+ tokens . push ( ...[ key , value ] ) ;
87
+ }
88
+ } ) ;
89
+ } else {
90
+ // otherwise they are spaces
91
+ const values = message . split ( / \s / ) ;
92
+ values . forEach ( ( value , i ) => {
93
+ if ( value . length ) {
94
+ tokens . push ( ...[ `value ${ i + 1 } ` , value ] ) ;
95
+ }
96
+ } ) ;
97
+ }
97
98
98
- for ( let i = 0 ; i < tokens . length - 1 ; i = i + 2 ) {
99
- const varName = tokens [ i ] ;
100
- const varValue = parseFloat ( tokens [ i + 1 ] ) ;
99
+ for ( let i = 0 ; i < tokens . length - 1 ; i = i + 2 ) {
100
+ const varName = tokens [ i ] ;
101
+ const varValue = parseFloat ( tokens [ i + 1 ] ) ;
101
102
102
- //skip line endings
103
- if ( varName . length === 0 ) {
104
- continue ;
105
- }
103
+ //skip line endings
104
+ if ( varName . length === 0 ) {
105
+ continue ;
106
+ }
106
107
107
- if ( ! newVars [ varName ] ) {
108
- newVars [ varName ] = [ ] ;
109
- }
108
+ if ( ! newVars [ varName ] ) {
109
+ newVars [ varName ] = [ ] ;
110
+ }
110
111
111
- newVars [ varName ] . push ( varValue ) ;
112
- }
113
- } ) ;
112
+ newVars [ varName ] . push ( varValue ) ;
113
+ }
114
+ } ) ;
114
115
115
116
return newVars ;
116
117
} ;
0 commit comments