node-modbusemu
The modbusemu
package is Node.js module for Modbus RTU devices emulation.
It requires Node.js to run and npm to be installed.
This project now supported only GNU/Linux environment and following Modbus functions:
0x01
- read coils0x02
- read discrete inputs0x03
- read holding registers0x04
- read input registers0x05
- write single coil0x06
- write single register0x0f
- write multiple coils0x10
- write multiple registers
Installing
-
Latest packaged version:
npm install modbusemu
-
Latest version on GitHub:
npm install https://github.com/Serge78rus/node-modbusemu/tarball/master
Usage
Example of usage in test.js
//var Emulator = require("../lib/emulator").Emulator; //relative path from test directoryvar Emulator = Emulator; //if module installed to default location //emulation datavar data = coils: minaddr: 0 maxaddr: 10 vals: discrInps: minaddr: 0 maxaddr: 10 vals: holdRegs: minaddr: 0 maxaddr: 10 vals: inpRegs: minaddr: 0 maxaddr: 10 vals: ; //data init for var i = datacoilsminaddr; i <= datacoilsmaxaddr; ++i datacoilsvalsi = i & 0x1 ? true : false;for var i = datadiscrInpsminaddr; i <= datadiscrInpsmaxaddr; ++i datadiscrInpsvalsi = i & 0x1 ? true : false;for var i = dataholdRegsminaddr; i <= dataholdRegsmaxaddr; ++i dataholdRegsvalsi = i;for var i = datainpRegsminaddr; i <= datainpRegsmaxaddr; ++i datainpRegsvalsi = i; //start emulationvar emulator = "/dev/ttyUSB0" //communication device 1 //slave address {} //communication options (default) //user functions { //user function for read coils if addr >= datacoilsminaddr && addr + cnt <= datacoilsmaxaddr return datacoilsvals; } { //user function for read discrete inputs if addr >= datadiscrInpsminaddr && addr + cnt <= datadiscrInpsmaxaddr return datadiscrInpsvals; } { //user function for read holding registers if addr >= dataholdRegsminaddr && addr + cnt <= dataholdRegsmaxaddr return dataholdRegsvals; } { //user function for read input registers if addr >= datainpRegsminaddr && addr + cnt <= datainpRegsmaxaddr return datainpRegsvals; } { //user function for write coils var cnt = valslength; if addr >= datacoilsminaddr && addr + cnt <= datacoilsmaxaddr for var i = 0; i < cnt; ++i datacoilsvalsaddr + i = valsi; return true; } { //user function for write holding registers var cnt = valslength; if addr >= dataholdRegsminaddr && addr + cnt <= dataholdRegsmaxaddr for var i = 0; i < cnt; ++i dataholdRegsvalsaddr + i = valsi; return true; } { //done callback function if err console; };
API
Modbus(dev, slave, opt, functions, done)
Constructor of emulator object
dev
- communication device name (for example: /dev/ttyUSB0)slave
- slave addressopt
- communication optionsfunctions
- hash array of user functionsdone
- callback function
Object opt
may contain some of the following fields:
baud: 9600 //communication speed fmt: "8n2" //data bits, parity and stop bits
If some fields are omitted, they take default values as described above
Object functions
may contain some of the following user functions:
{ /* user function for read coils */ } { /* user function for read discrete inputs */ } { /* user function for read holding registers */ } { /* user function for read input registers */ } { /* user function for write coils */ } { / *user
If some of the user functions are omitted, the corresponding Modbus protocol functions will return an exception code 1 (illegal function).
Function done
take one argument:
where err
is null
if success or Error object if an error occurred
User functions
readCoils(addr, cnt)
User function for reading coils (need for Modbus function 0x01)
addr
- first coil addresscnt
- number of coils to read
Return value - an array of boolean values, that represent the status of coils
readDiscrInps(addr, cnt)
User function for reading discrete inputs (need for Modbus function 0x02)
addr
- first discrete input addresscnt
- number of discrete inputs to read
Return value - an array of boolean values, that represent the status of discrete inputs
readHoldRegs(addr, cnt)
User function for reading holding registers (need for Modbus function 0x03)
addr
- first holding register addresscnt
- number of holding registers to read
Return value - an array of unsigned integer values, that represent the status of holding registers
readInpRegs(addr, cnt)
User function for reading input registers (need for Modbus function 0x04)
addr
- first input register addresscnt
- number of input registers to read
Return value - an array of unsigned integer values, that represent the status of input registers
writeCoils(addr, vals)
User function for writing coils status (need for Modbus functions 0x05 and 0x0f)
addr
- first coil addressvals
- an array of boolean values, that represent the status of coils
Returns a boolean value: true
if successful or false
when attempting to go beyond the range of valid coils addresses
writeRegs(addr, vals)
User function for writing holding registers status (need for Modbus functions 0x06 and 0x10)
addr
- first register addressvals
- an array of unsigned integer values, that represent the status of registers
Returns a boolean value: true
if successful or false
when attempting to go beyond the range of valid holding registers addresses
License
MIT license (see the LICENSE
file).