Giter Site home page Giter Site logo

Documentation about stream HOT 2 OPEN

Venerons avatar Venerons commented on June 24, 2024 2
Documentation

from stream.

Comments (2)

Venerons avatar Venerons commented on June 24, 2024 3

After many attempts, I managed to make a server that is working. This is the template I used (I removed all the code that actually gets the correct values)

// npm modules
const modbus = require('modbus-stream');

// kill logging
process.on('SIGINT', function () {
	console.error('Process killed by SIGINT');
	process.exit();
});

// exception logging
process.on('uncaughtException', function (error) {
	console.error('Uncaught Exception thrown', error);
	process.exit(1);
});

// env variables
const SERVER_PORT = process.env.SERVER_PORT || 502;

modbus.tcp.server({ debug: 'server' }, (connection) => {
	console.log('connected');
	connection.on('close', () => {
		console.log('close');
	});
	connection.on('error', (error) => {
		console.log('error', error);
	});
	connection.on('read-coils', (request, reply) => {
		console.log('\tread-coils', request.request);
		const values = [];
		for (let i = 0; i < request.request.quantity; ++i) {
			const address = request.request.address + i;
			const value = 0; // replace this with actual value
			values.push(value);
		}
		reply(null, values);
	});
	connection.on('read-discrete-inputs', (request, reply) => {
		console.log('\tread-discrete-inputs', request.request);
	});
	connection.on('read-holding-registers', (request, reply) => {
		console.log('\tread-holding-registers', request.request);
		const values = [];
		for (let i = 0; i < request.request.quantity; ++i) {
			const address = request.request.address + i;
			const value = Buffer.from([ 0x00, 0x00 ]); // replace this with actual value
			values.push(value);
		}
		reply(null, values);
	});
	connection.on('read-input-registers', (request, reply) => {
		console.log('\tread-input-registers', request.request);
	});
	connection.on('write-single-coil', (request, reply) => {
		console.log('\twrite-single-coil', request.request);
	});
	connection.on('write-single-register', (request, reply) => {
		console.log('\twrite-single-register', request.request);
	});
	connection.on('write-multiple-coils', (request, reply) => {
		console.log('\twrite-multiple-coils', request.request);
	});
	connection.on('write-multiple-registers', (request, reply) => {
		console.log('\twrite-multiple-registers', request.request);
	});
	connection.on('data', (request, reply) => {
		console.log('\tdata', request.request);
	});
}).listen(SERVER_PORT, () => {
	console.log(`Server listening on ${SERVER_PORT}`);
});

from stream.

stif avatar stif commented on June 24, 2024

It was not obvious for me as well, but i managed to write a simple modbus server script with this library.
Here is the link for the code if anybody is interested in another example how to use the library: https://github.com/nett-media/modbus-server/blob/master/modbus-server.js

from stream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.