5

For this part of code for my node.js server

var SerialPort = require("serialport");
var port = new SerialPort("/dev/tty-usbserial1", {});

which use the https://github.com/EmergingTechnologyAdvisors/node-serialport library.

How I can know this path to my serial port?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
nuser
  • 83
  • 1
  • 6
  • What your OS? Linux - http://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them , on Windows you can look device manager and path will be like `COM3` – Aikon Mogwai Jul 27 '16 at 08:53

3 Answers3

4

I tried the previous answers in this question but they did not work in latest version of serial port (Sept 2020). We need to use serialport.list() function in promise approach like this:

import serialport package

const SerialPort = require('serialport')

Promise approach

SerialPort.list().then(ports => {
    ports.forEach(function(port) {
        console.log(port.path)
    })
})

I hope this will help you to solve this problem

3

To find the paths of available COM ports on the system, you can use the list method.

I'd advise omitting any that have an undefined manufacturer property as these normally seem to be things like built-in bluetooth etc.

Example:

const SerialPort = require('serialport') 
SerialPort.list((err, ports) => {
    console.log(ports)                
})

Documentation: https://node-serialport.github.io/node-serialport/SerialPort.html#.list

totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
3

As some things have changed in the last 3 years of JS I'd like to update totallyNotLizards' answer. As the documentations says in order to run this code you need to use Promises.

const SerialPort = require('serialport')
SerialPort.list().then(
  ports => ports.forEach(console.log),
  err => console.error(err)
)

Page where I grabbed this code from: https://serialport.io/docs/api-stream#serialportlist

Pedro Henrique
  • 131
  • 1
  • 4