简体   繁体   中英

NodeJS not receiving data from serialport

I am able to get a list of serial ports, and successfully open my desired port. However, The data received event never fires. The port sends a small amount of data after being opened. I have verified that the port is sending data by using a usb monitor.

// Open event example
const SerialPort = require('serialport');
var port = new SerialPort('COM220', { autoOpen:true, baudRate: 921600 });


port.on('open', () => {
  console.log('Port Opened');

});

// Switches the port into "flowing mode"
port.on('data', function (data) {
  console.log('Data:', data);
});

// Read data that is available but keep the stream from entering "flowing mode"
port.on('readable', function () {
  console.log('Data:', port.read());
});

I have some ideas:

  1. That baud rate is very high and not in the standard for serial ports. I would start much lower to make sure both devices can match. 38,4? 9600? This is enough to prove your theory.
  2. You can log console.log(port) the port itself and start comparing behavior and see what you get.
  3. The readable mode is different than the normal. What are the results from this?

    port.on('data', function(data) { console.log(data.toString()); }

  4. You can make your own buffer by appending a string with the data char.

    var bfr = ""; port.on('data', function(data) { bfr += data.toString(); console.log(bfr); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM