Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var serialport = require("serialport");
- var _ = require('lodash');
- const port = "/dev/tty.usbserial-14420";
- let f3 = new serialport(port, {
- baudRate: 9600,
- databits: 8,
- parity: 'none',
- stopBits: 1,
- flowControl: false
- })
- const Readline = new serialport.parsers.Readline();
- f3.pipe(Readline);
- f3.on('open', (error) => {
- console.log('port open');
- //init
- if(error) {
- console.log(error);
- } else {
- f3.write(hexStringToBinaryBuffer('F2 00 00 03 43 32 32 03 b1'));
- }
- });
- Readline.on('data', (data) => {
- console.log("data received "+data);
- });
- f3.on('error', (error) => {
- console.log('error: ' + error);
- })
- const hexStringToBinaryBuffer = (string) => {
- const subStrings = Array.from(string);
- let previous = null;
- const bytes = [];
- _.each(subStrings, (val) => {
- if (previous === null) { // Converting every 2 chars as binary data
- previous = val;
- } else {
- const value = parseInt(`0x${previous}${val}`, 16);
- bytes.push(value);
- previous = null;
- }
- });
- let returnValue = Buffer.from(bytes);
- console.log(returnValue);
- return returnValue;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement