Skip to content

Commit

Permalink
Add serial-to-tcp example.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwippermann committed Sep 26, 2020
1 parent 2b671f2 commit 472f335
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/serial-to-tcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.js
98 changes: 98 additions & 0 deletions examples/serial-to-tcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# serial-to-tcp

Allows TCP access to serial port based VBus connections.


## Overview

There are two types of VBus adapters:

- TCP based: DL2, DL3, KM2, VBus/LAN
- serial port based: VBus/USB, USB port of DeltaSol SLT and other controllers

This tool connects to one or more serial port based adapters and exposes them over TCP. This allows:

- transmitting VBus data over longer distances than USB or serial ports would normally permit
- accessing serial port based adapters from applications that only support TCP based ones


## Initial setup

```
$ cd <workspace>
$ git clone https://github.com/danielwippermann/resol-vbus
$ cd resol-vbus
$ npm install --production
$ cd examples/serial-to-tcp
$ cp config.js.example config.js
```


## Configuration

This tool needs a configuration file called `config.js`. The repository already contains a `config.js.example` file that was copied during the initial setup above as a starting point.

```javascript
module.exports = {
// A list of serial ports to connect to
serialPorts: [{
channel: 1,
path: '/dev/tty.usbmodem141301',
baudrate: 9600,
}, {
channel: 2,
path: '/dev/tty.SLAB...',
baudrate: 9600,
}],

// The TCP port on which the service is listening for incoming connections
port: 7053,
};
```

If you only want to connect to a single serial port it is recommended to configure that to use channel 0, since most applications will by default try and connect that channel 0.


## Using the tool

After adjusting the configuration, just run the `main.js`:

```
$ cd <workspace>
$ cd resol-vbus/examples/serial-to-tcp
$ node main.js
Opening serial port...
Opening TCP endpoint...
Waiting for connections...
```


## Known issues

- Selecting a non-existing channel using the `CHANNEL` command returns `+OK` instead of an error.
- Sending the `DATA` command with a non-existing channel selected returns `+OK`, but immediately closes the connection afterwards.


## License

The MIT License (MIT)

Copyright (c) 2013-2020, Daniel Wippermann.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
15 changes: 15 additions & 0 deletions examples/serial-to-tcp/config.js.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
// A list of serial ports to connect to
serialPorts: [{
channel: 1,
path: '/dev/tty.usbmodem141301',
baudrate: 9600,
}, {
channel: 2,
path: '/dev/tty.SLAB...',
baudrate: 9600,
}],

// The TCP port on which the service is listening for incoming connections
port: 7053,
};
166 changes: 166 additions & 0 deletions examples/serial-to-tcp/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
const SerialPort = require('serialport');


const vbus = require('../resol-vbus');

const config = require('./config');



const {
Connection,
TcpConnectionEndpoint,
} = vbus;


const serialPorts = [];
const connections = [];
let logging = null;


function errorLog(...args) {
console.log(...args);
}


function debugLog(...args) {
console.log(...args);
}


function acceptConnection(port, origin) {
debugLog('Accepting connection');

connections.push(origin);

function remove() {
const idx = connections.indexOf(origin);
if (idx >= 0) {
connections.splice(idx, 1);
}
}

origin.on('error', err => {
errorLog(err);

remove();
});

origin.on('end', () => {
debugLog('Closing connection');

remove();
});

origin.on('readable', () => {
let chunk;
while ((chunk = origin.read())) {
port.write(chunk);
}
});
}


async function createTcpEndpoint(serialPort) {
debugLog('Opening TCP endpoint...');

const channels = config.serialPorts.reduce((memo, serialPort) => {
memo [serialPort.channel] = `VBus ${serialPort.channel}: ${serialPort.path}`;
return memo;
}, []);

console.log(channels);

const endpoint = new TcpConnectionEndpoint({
port: config.port,
channels,
});

endpoint.on('connection', connectionInfo => {
const channel = +connectionInfo.channel;
const serialPort = serialPorts.find(port => port.channel === channel);

if (serialPort) {
acceptConnection(serialPort.port, connectionInfo.socket);
} else {
connectionInfo.socket.end();
}
});

await endpoint.start();
}


async function openSerialPort(config) {
debugLog('Opening serial port...');

const port = await new Promise((resolve, reject) => {
const port = new SerialPort(config.path, {
baudRate: config.baudrate,
}, (err) => {
if (err) {
reject(err);
} else {
resolve(port);
}
});
});

port.on('error', err => {
errorLog(err);
process.exit(1);
});

port.on('end', () => {
debugLog('Serial port EOF');
process.exit(0);
});

port.on('readable', () => {
let chunk;
while ((chunk = port.read())) {
for (const connection of connections) {
connection.write(chunk);
}

if (logging) {
logging.write(chunk);
}
}
});

serialPorts.push({
channel: config.channel,
port,
});
}


async function createLogging() {
const connection = new Connection();

connection.on('packet', packet => {
// console.log(packet.getId());
});

return connection;
}


async function main() {
for (const serialPortConfig of config.serialPorts) {
await openSerialPort(serialPortConfig);
}

logging = await createLogging();

await createTcpEndpoint();

debugLog('Waiting for connections...');
}


main().then(null, err => {
errorLog(err);
process.exit(1);
});

0 comments on commit 472f335

Please sign in to comment.