Serial message parser
About
Communication on devices like Arduino and other microcontroller based devices is usually some sort of string messages with various delimiting techniques and parsing schemes.
One of the most common ones is using comma separated lists with each value in the list holding some measurement. For instance string "20.66,35,66" could mean temperature of 20.66, light level of 35% and soil humidity of 66%.
Having some utility to parse this data in an easy way and with minimal configuration would be very time saving. The above mentioned example string could be parsed with a config like this:
var config = parser : 'symbolSeparated' separator : ',' mappings : name: 'temperature' type: 'float' name: 'lightLevel' type: 'int' name: 'soilHumidity' type: 'int' ; //fixed size parsing configvar configFixedSize = parser : 'fixedSizes' mappings : name: 'temperature' size: 5 type: 'float' name: 'lightLevel' size: 2 type: 'int' name: 'soilHumidity' size: 2 type: 'int' ;
Using
var srmparse = ; var config = {}; // one of above configsvar translator = ; var inData = "20.66,35,66";var outData = translator; console;
Types
- int
- float
- str
Testing
This project uses jasmine-node for testing. It's easy to install:
npm install jasmine-node -g
Running the tests is also straight forward looks like. Jasmine locates the test js files in the spec folder automatically.
jasmine-node spec/*spec.js