Node serialport not reading response to command

25 Views Asked by At

I'm trying to use the Node serialport package to send commands to a serial device and read back the response. When I use a standard serial terminal to connect to my device (an ESP32), I can send commands and receive responses like this:

esp32> air-pump activate
enabled: 1

I've tried a few things to get a response from the device in Node. My current code looks something like this:

const serialPort = new SerialPort({
    path: '/dev/tty.usbserial-0001',
    baudRate: 115200,
});

serialPort.on("open", function() {
    console.log("-- Connection opened --");
    serialPort.on("data", function(data) {
      console.log("Received: " + data);
    });
});

serialPort.write("air-pump activate", function(err) {
    if (err) {
        return console.log('Error on write: ', err.message)
    }
    console.log('message written')
});

When the serial connection succeeds, I see the message -- Connection opened --, and then the loading scroll from an ESP32 like this (concatenated for space):

Received: Ҁ qd��NUѴښ�j%�(105) cpu_start: cpu freq: 160000000 Hz
I (105) cpu_start: Application information:
I (108) cpu_start: Project name:     console
I (113) cpu_start: App version:      7c9dba5-dirty
I (118) cpu_start: Compile time:     Jan  2 2024 01:30:03
I (124) cpu_start: ELF file SHA256:  05d3b10fe...
I (130) cpu_start: ESP-IDF:          v5.2-dev-3903-g66992a
Received: ca7a

Received: [0;32mI (136
Received: ) cpu_start
Received: : Min chip r
Received: ev:     v0.0
Received:

However, when my command executes I see this in response:

message written
Received: a
Received: ir
Received: -pump
Received:  activ
Received: ate

For some reason, my serial connection is just reading back to my the exact command I sent, rather than the response to the command which should be enabled: 1. Would love to understand why this is.

0

There are 0 best solutions below