Library SerialPort integrated into OTDT to support serial connections

  • Home
  • Blog
  • Library SerialPort integrated into OTDT to support serial connections

Library SerialPort integrated into OTDT to support serial connections

Using this new library makes the serial port available as Trigger and as Action for OTDT. This means that for example in embedded environments special bus devices could be used.

General

2025-04-02 21:27

The following code snippet shows how to use the new SerialPort integration for storing incoming values or fire an appropriate Trigger which then could be used to define the according Actions.

vars.serial = [];

const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');

const port = new SerialPort("/dev/ttyUSB0", {
    baudRate: 9600,
    dataBits: 8,
    stopBits: 1,
    parity: "none",
    autoOpen: false
});

port.on('error', function(err) {       
  console.error("Error", err.message)
});

const parser = port.pipe(new Readline({delimiter: '\r'}));

parser.on('data', function (data) {
  var s = data.toString();
  
  console.log("Data", s);
  
  vars.serial.push(s);
  // THIS WAS TO STORE
  // ALTERNATIVELY YOU COULD FIRE A TRIGGER
  // 
  //await app.call(null, '/system/serial/data', {s});
});

Certainly transformations to the incoming data could be made within the data event.