Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. var serialport = require("serialport");
  2. var _ = require('lodash');
  3.  
  4. const port = "/dev/tty.usbserial-14420";
  5.  
  6. let f3 = new serialport(port, {
  7. baudRate: 9600,
  8. databits: 8,
  9. parity: 'none',
  10. stopBits: 1,
  11. flowControl: false
  12. })
  13.  
  14. const Readline = new serialport.parsers.Readline();
  15. f3.pipe(Readline);
  16.  
  17. f3.on('open', (error) => {
  18. console.log('port open');
  19. //init
  20. if(error) {
  21. console.log(error);
  22. } else {
  23. f3.write(hexStringToBinaryBuffer('F2 00 00 03 43 32 32 03 b1'));
  24.  
  25. }
  26. });
  27.  
  28. Readline.on('data', (data) => {
  29. console.log("data received "+data);
  30. });
  31.  
  32. f3.on('error', (error) => {
  33. console.log('error: ' + error);
  34. })
  35.  
  36. const hexStringToBinaryBuffer = (string) => {
  37. const subStrings = Array.from(string);
  38. let previous = null;
  39. const bytes = [];
  40. _.each(subStrings, (val) => {
  41. if (previous === null) { // Converting every 2 chars as binary data
  42. previous = val;
  43. } else {
  44. const value = parseInt(`0x${previous}${val}`, 16);
  45. bytes.push(value);
  46. previous = null;
  47. }
  48. });
  49.  
  50. let returnValue = Buffer.from(bytes);
  51. console.log(returnValue);
  52. return returnValue;
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement