Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const SerialPort = require('serialport');
- const Readline = require('@serialport/parser-readline')
- // Set the com port to use
- const ARDUINO_COM_PORT = "COM3";
- // Setup the serial port (port will auto open)
- const arduinoSerialPort = new SerialPort(ARDUINO_COM_PORT, { baudRate: 9600 });
- const arduinoParser = arduinoSerialPort.pipe(new Readline({ delimiter: '\r\n', encoding: 'utf8' }))
- // Tells you when the port is open
- arduinoSerialPort.on('open', function() {
- console.log('Serial Port ' + ARDUINO_COM_PORT + ' is opened.');
- });
- // Tells you when data arrives from the parser (so a line at a time)
- arduinoParser.on('data', function(data) {
- // Assume we sent data as a comma seperated string
- // like this, so that data == "123,456,789"
- // Split the numbers based on a comma delimeter into an array
- const values = data.split(',');
- // Make sure we have the number of values we expect
- if (values.length === 3)
- {
- // Get data
- const sensorA = values[0];
- const sensorB = values[1];
- const sensorC = values[2];
- // Do something...
- console.log(`A: ${sensorA}, B: ${sensorB}, C: ${sensorC}`);
- }
- else
- {
- // Not the right number of values - panic!
- console.warn("Panic!")
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement