Skip to content

LiveTransceiver using a serial port

The I/O-agnostic LiveTransceiver class can be combined with a third-party serial port library to transmit and receive data using something like a VBus/USB adapter.

Choosing a serial port library

The following guide uses Node.js as the execution environment. Since Node.js has no support for serial ports out of the box a third-party library is needed. A popular choice for that is serialport.

Let's setup a small test project:

shell
# create a new working directory and use it as a working directory for the remainder of the guide
mkdir serialport-vbus-test
cd serialport-vbus-test

# create a minimal `package.json` file
yarn init -y

# install dependencies using your package manager of choice (here: yarn)
yarn add resol-vbus-core serialport

# create an empty `src/main.js` for the actual code
mkdir src
touch src/main.js

Then start editing your src/main.js and import the required dependencies:

javascript
const { SerialPort } = require('serialport');
const { LiveTransceiver } = require('resol-vbus-core');

Open the serial port like shown in their documentation (https://serialport.io/docs/api-serialport/):

javascript
const serialport = new SerialPort({ path: '/dev/example', baudRate: 9600 });

Now create a LiveTransceiver and use its onTransmit callback to call write() of the serial port:

javascript
const tx = new LiveTransceiver({
  onTransmit(buffer) {
    serialport.write(Buffer.from(buffer));
  },

  // TODO: other callbacks here
});

And ensure that incoming data from the serial port is forwarded to the decode() instance function of the LiveTransceiver:

javascript
serialport.on('data', chunk => {
  tx.decode(chunk);
});

Good job, I/O handling is done at this point! Now use the LiveTransceiver to process data and answer requests.