Skip to content

Commit

Permalink
ModbusServer vector with SetRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
bolliy committed Oct 18, 2024
1 parent 1aa95e9 commit 7076142
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/modbus/modbus_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ModbusServer {
this.adapter = adapterInstance;
this.log = this.adapter.logger;
this._isConnected = false;
//this._modbusClient = null;
this._stat = {};

this.vector = {
Expand All @@ -31,9 +30,10 @@ class ModbusServer {
this._addInfoStat('#getCoil',addr, 1 , unitId);
callback({ modbusErrorCode: 0x01, msg: 'Illegal function (device does not support this read/write function)' });
},
setRegister: (addr, value,unitId, callback) => {
this._addInfoStat('#readDeviceIdentification',addr, value, unitId);
callback({ modbusErrorCode: 0x01, msg: 'Illegal function (device does not support this read/write function)' });
setRegister: async (addr, value,unitId, callback) => {
//this._addInfoStat('#readDeviceIdentification',addr, value, unitId);
await this._handleSetReg(addr,value,unitId, callback);
//callback({ modbusErrorCode: 0x01, msg: 'Illegal function (device does not support this read/write function)' });
},
setCoil: (addr, value, unitId, callback) => {
this._addInfoStat('#setCoil',addr, value, unitId);
Expand Down Expand Up @@ -171,12 +171,29 @@ class ModbusServer {
callback({ modbusErrorCode: 0x04, msg: 'Slave device failure (device reports internal error)' });
}
}
/*
async process(modbusClient) {
this._modbusClient = modbusClient;
//

async _handleSetReg(address,data, unitId, callback) {
try {
this.log.debug('Modbus-proxy: Try to write data to id/address ' + unitId + '/' + address+'/'+data);
if (!this.adapter.isConnected) {
callback({ modbusErrorCode: 0x05, msg: 'Acknowledge (requested data will be available later)' });
return;
}
if (!this.adapter.modbusClient) {
this.log.error('No modbus-client is registered!');
callback({ modbusErrorCode: 0x04, msg: 'Slave device failure (device reports internal error)' });
return;
}

this.adapter.modbusClient.setID(unitId);
await this.adapter.modbusClient.writeRegisters(address,[data]);
callback();
} catch (err) {
this.log.warn('Modbus-proxy: '+err.modbusCode+' '+err.message);
//callback(JSON.stringify(err));
callback({ modbusErrorCode: err?.modbusCode, msg: err?.message });
}
}
*/

wait(ms){
return new Promise(resolve => setTimeout(resolve, ms));
Expand Down

0 comments on commit 7076142

Please sign in to comment.