modbus-serial
Advanced tools
Comparing version 8.0.16 to 8.0.17
import {ModbusRTU} from "./ModbusRTU"; | ||
export * from "./ServerTCP"; | ||
export * from "./ServerSerial"; | ||
export default ModbusRTU; |
10
index.js
@@ -44,3 +44,8 @@ "use strict"; | ||
"Acknowledge (requested data will be available later)", | ||
"Slave device busy (retry request again later)" | ||
"Slave device busy (retry request again later)", | ||
"Negative acknowledge (slave device cannot perform programming functions)", | ||
"Memory parity error (slave device detected a parity error in memory)", | ||
"Unknown error", | ||
"Gateway path unavailable (misconfigured gateway)", | ||
"Gateway target device failed to respond (retry request again later)" | ||
]; | ||
@@ -685,3 +690,4 @@ | ||
this._port.removeAllListeners("data"); | ||
this._port.destroy(callback); | ||
this._port.destroy(); | ||
callback(); | ||
} else { | ||
@@ -688,0 +694,0 @@ // nothing needed to be done |
{ | ||
"name": "modbus-serial", | ||
"version": "8.0.16", | ||
"version": "8.0.17", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -318,4 +318,92 @@ # modbus-serial | ||
---- | ||
##### "modbusdb" is an elegant wrapper over the Modbus protocol | ||
###### Check modbusdb github repo at https://github.com/yarosdev/modbusdb for more information, feedback is welcome! | ||
Here is an example of using `modbusdb` wrapper over `modbus-serial`: | ||
``` typescript | ||
import Modbus from 'modbus-serial'; | ||
import { Modbusdb, ModbusSerialDriver, Datamap, createRegisterKey, TypeEnum, ScopeEnum } from "modbusdb"; | ||
// create an empty modbus client | ||
const client = new Modbus(); | ||
client.connectTcpRTUBuffered("127.0.0.1", { port: 8502 }, app) // or use any other available connection methods | ||
function app() { | ||
// First you need to define a schema for a database: | ||
// createRegisterKey(UNIT_ADDRESS, MODBUS_TABLE, REGISTER_ADDRESS, BIT_INDEX) | ||
const schema = [ | ||
{ key: createRegisterKey(1, ScopeEnum.InternalRegister, 10), type: TypeEnum.Int16 }, | ||
{ key: createRegisterKey(1, ScopeEnum.InternalRegister, 11), type: TypeEnum.Int32 }, | ||
{ key: createRegisterKey(1, ScopeEnum.PhysicalRegister, 99), type: TypeEnum.UInt16 }, | ||
{ key: createRegisterKey(1, ScopeEnum.InternalRegister, 15, 2), type: TypeEnum.Bit }, | ||
]; | ||
// Define unit-scoped config if needed: | ||
const units = [ | ||
{ | ||
address: 1, // This is unit address | ||
forceWriteMany: true, // Use 15(0x0f) and 16(0x10) functions for single register, default: false | ||
bigEndian: true, // You can use BigEndian for byte order, default: false | ||
swapWords: false, // This is applicable only for multi-registers types such as int32, float etc, default: false | ||
requestWithGaps: true, // If you are requesting addresses 10 and 13, allow to send one request to the device, default: true | ||
maxRequestSize: 32, // How many registers to be requested in one round-trip with device, default: 1 | ||
} | ||
]; | ||
// Let`s create an instance of a database: | ||
const db = new Modbusdb({ | ||
driver: new ModbusSerialDriver(client), | ||
datamap: new Datamap(schema, units) | ||
}); | ||
// Now we can request three registers: | ||
// NOTICE: Modbusdb under the hood will make all needed requests for you in using an optimal plan | ||
// If registers can be requested using a single request, be sure it will | ||
db.mget([ | ||
createRegisterKey(1, ScopeEnum.InternalRegister, 10), | ||
createRegisterKey(1, ScopeEnum.InternalRegister, 11), | ||
createRegisterKey(1, ScopeEnum.PhysicalRegister, 99) | ||
]).then(result => { | ||
console.log('mget', result) | ||
}) | ||
// You can store register`s key to be used later: | ||
const speedSetPoint = createRegisterKey(1, ScopeEnum.InternalRegister, 10); | ||
const workingMode = createRegisterKey(1, ScopeEnum.InternalRegister, 11); | ||
// Write values directly into modbus device as easy as possible: | ||
// NOTICE: Modbusdb under the hood will make all needed requests for you | ||
// Write operations have higher priority over the read operations | ||
db.mset([ | ||
[speedSetPoint, 60], | ||
[workingMode, 10], | ||
]).then(result => { | ||
console.log('mset', result) | ||
}) | ||
} | ||
``` | ||
Enums: | ||
``` | ||
ScopeEnum: (Modbus Table) | ||
1 = PhysicalState = Discrete Input | ||
2 = InternalState = Coil Status | ||
3 = PhysicalRegister = Input Register | ||
4 = InternalRegister = Holding Register | ||
TypeEnum: (Available Data Types) | ||
1 = Bit, | ||
4 = Int16, | ||
5 = UInt16, | ||
6 = Int32, | ||
7 = UInt32, | ||
8 = Float | ||
``` | ||
---- | ||
to get more see [Examples](https://github.com/yaacov/node-modbus-serial/wiki) | ||
@@ -322,0 +410,0 @@ |
@@ -240,3 +240,3 @@ "use strict"; | ||
// create a serial server | ||
modbus._serverPath = new SerialPort(optionsWithBindingandSerialport); | ||
modbus._serverPath = new SerialPort(optionsWithBindingandSerialport, options.openCallback); | ||
@@ -257,3 +257,3 @@ // create a serial server with a timeout parser | ||
modbus._server.on("open", function() { | ||
modbus._serverPath.on("open", function() { | ||
modbus.socks.set(modbus._server, 0); | ||
@@ -275,2 +275,3 @@ | ||
modbus.emit("initialized"); | ||
}); | ||
@@ -277,0 +278,0 @@ |
@@ -299,3 +299,9 @@ /* eslint-disable no-var */ | ||
} else { | ||
const values = vector.getMultipleHoldingRegisters(address, length, unitID); | ||
let values; | ||
try { | ||
values = vector.getMultipleHoldingRegisters(address, length, unitID); | ||
} catch (error) { | ||
callback(error); | ||
return; | ||
} | ||
if (values.length === length) { | ||
@@ -438,3 +444,9 @@ for (i = 0; i < length; i++) { | ||
} else { | ||
const values = vector.getMultipleHoldingRegisters(address, length, unitID); | ||
let values; | ||
try { | ||
values = vector.getMultipleHoldingRegisters(address, length, unitID); | ||
} catch (error) { | ||
callback(error); | ||
return; | ||
} | ||
if (values.length === length) { | ||
@@ -441,0 +453,0 @@ for (i = 0; i < length; i++) { |
@@ -220,2 +220,5 @@ "use strict"; | ||
modbus._server = net.createServer(); | ||
modbus._server.on("error", function(error) { | ||
modbus.emit("serverError", error); | ||
}); | ||
modbus._server.listen({ | ||
@@ -222,0 +225,0 @@ port: options.port || MODBUS_PORT, |
@@ -8,3 +8,3 @@ import * as events from 'events'; | ||
interface IServiceVector { | ||
export interface IServiceVector { | ||
getCoil?: | ||
@@ -57,2 +57,3 @@ ((addr: number, unitID: number, cb: FCallbackVal<boolean>) => void) | | ||
on(event: 'socketError', listener: FCallback): this; | ||
on(event: 'serverError', listener: FCallback): this; | ||
on(event: 'error', listener: FCallback): this; | ||
@@ -62,3 +63,3 @@ on(event: 'initialized', listener: FCallback): this; | ||
type FCallbackVal<T> = (err: Error | null, value: T) => void; | ||
type FCallback = (err: Error | null) => void; | ||
export type FCallbackVal<T> = (err: Error | null, value: T) => void; | ||
export type FCallback = (err: Error | null) => void; |
@@ -0,1 +1,7 @@ | ||
#### See also modbusdb | ||
The worker class is a simple class that makes it easy to grab some data from device, for a more complete module see modbusdb https://github.com/yarosdev/modbusdb | ||
------------ | ||
#### What can I do with this module ? | ||
@@ -2,0 +8,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
394450
79
10098
419