0

I am attempting to send AT commands to a USB device in Node.js using shelljs and socat.

The following (2) commands executes successfully in Terminal:

$ sudo socat - /dev/ttyUSB0
AT+RESTART

Note: The first command connects to the device, and the second restarts it.

However, the following (2) commands do not execute successfully in Node.js:

shell.exec('sudo socat - /dev/ttyUSB0');
shell.exec('AT+RESTART');

How do I get the two commands to run in the same shell.exec instance?

2 Answers2

0

Check node-cmd. It has an example on how to interact with python console.

const cmd=require('../cmd.js');

const processRef=cmd.get('python -i');
let data_line = '';

//listen to the python terminal output 
processRef.stdout.on(
  'data',
  function(data) {
    data_line += data;
    if (data_line[data_line.length-1] == '\n') {
      console.log(data_line);
    }
  }
);

const pythonTerminalInput=`primes = [2, 3, 5, 7]
for prime in primes:
    print(prime)

`;

//show what we are doing 
console.log(`>>>${pythonTerminalInput}`);

//send it to the open python terminal 
processRef.stdin.write(pythonTerminalInput);

And the output would be,

>>>primes = [2, 3, 5, 7]
for prime in primes:
    print(prime)


2
3
5
7

You could use the same for Terminal interactions.

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
0

This is a perfect match use case for my atinout program:

shell.exec('echo AT+RESTART | atinout - /dev/ttyUSB0 -');
hlovdal
  • 26,565
  • 10
  • 94
  • 165