node-serialport throws “File not found” error for a certain device when using USB-COM interface

eye-catchJavaScript/TypeScript

I needed to use node-serialport to use a scanner for my project. I have two scanners. One of them works fine but another scanner doesn’t work for some reason.

Sponsored links

Test code

The test code for the scanner is simple. This is Node.js + TypeScript.

import { SerialPort } from "serialport";

SerialPort.list()
    .then((portInfo) => {
        console.log(JSON.stringify(portInfo, null, 2));
    })
    .then(() => {
        const port = new SerialPort({ path: "COM8", baudRate: 9600, }, (err) => {
            if (err) {
                console.error("ERROR!!!");
                console.error(err);
                console.error("EXIT");
            } else {
                console.log("DATA---");
                port.on("data", console.log);
            }
        });
    });

Firstly, it shows all available COM ports. Then, it tries to open one of them. In my case, COM8 is the target COM port to use the scanner. It automatically opens the COM port in the constructor.

If it can open it, the console shows the scanned data. Otherwise, it exits.

Sponsored links

Error: Opening COM: File not found

When the scanner is connected to my PC, the COM port appears on the Device Manager. The scanner’s driver seems to work but the following error occurs when the code above runs.

[Error: Opening COM8: File not found]

It seems that the device doesn’t immediately get ready when it’s connected.

Sponsored links

Error: Open (SetCommState): Unknown error code 31

A couple of minutes later after the scanner is connected to my PC, it seems that the device gets ready. It takes about 2 minutes on my PC to be ready. Then, I ran the code above and it showed the following error.

[Error: Open (SetCommState): Unknown error code 31]

It takes 2 minutes again until the error is shown.

The overview of the steps is the following.

  1. Connect the scanner
  2. Wait for 1 – 2 minutes until the device gets ready
  3. Try to open the COM port
  4. Wait for 2.5 min
  5. Can sometimes be open

When the code runs before the device gets ready, it shows "File not found" error. When it’s ready, it can throw an unknown error.

What I did to know this behavior

It’s important to know the difference between the two devices. We don’t know which device or module causes this problem. To clarify it, I used Tera Term.

Tera Term is a terminal emulator and we can connect to a device via TCP/IP or using Serial Port.

The COM port immediately appears on the Device Manager when connected. However, the COM port doesn’t appear in the list of the COM ports in Tera Term while the code above shows the target COM port when the scanner is connected. So there is a difference to determine available COM ports. I could find that it takes a while until the device really gets ready.

But unfortunately, I still haven’t found a workaround to be able to open and read the scanned data immediately.

It seems to be something wrong with the scanner’s driver.

Edit: When I updated the driver for the scanner, it worked as expected.

If you face a similar problem, wait for a couple of minutes and try again. It might work then. If it doesn’t fulfill your requirement, you should create an inquiry to the product maker.

Comments

Copied title and URL