⚠️ This library has been updated to match the Python implementation. It now includes both the baseModbusMachineclass and thePumpclass for better structure and functionality.
<script src="https://cdn.jsdelivr.net/gh/m-tec-com/mtecconnect3dcp/JavaScript/mtecconnect3dcp.js"></script>
<script>
// Using the legacy class (backward compatible)
var pump = new mtecConnectModbus();
// Or using the new structure
var pump = new Pump();
async function connect() {
var connected = await pump.connect();
if (!connected) {
console.log("Connection failed");
return;
}
}
async function start(){
await pump.setSpeed(20);
}
async function stop(){
await pump.setSpeed(0);
}
</script>
<button onclick="connect()">Connect</button>
<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
⚠️ Keep in mind:
.connect()has to be triggered by user gesture (e.g. onclick)- communication takes some time → use async functions and await
The library now provides two main classes:
Provides low-level Modbus communication methods:
connect()- Connect to serial portdisconnect()- Disconnect from serial portread(command)- Read from Modbus devicewrite(command, value)- Write to Modbus devicekeepalive(callback, interval)- Start keepalive loopstopKeepalive()- Stop keepalive loop
Provides pump-specific functionality with both new methods and backward-compatible properties.
Creates the Object to use the library.
// Legacy constructor (backward compatible)
var pump = new mtecConnectModbus(inverterNumber);
// New constructors
var pump = new Pump(inverterNumber, baudRate, log);
var machine = new ModbusMachine(inverterNumber, baudRate, log);parameters:
- string (length: 2), inverter number (parameter F802), optional (default: "01")
- number, serial baudrate, optional (default: 19200)
- boolean, enable logging, optional (default: false)
result:
- mtecConnectModbus/Pump/ModbusMachine, contains everything used to communicate via modbus
Connects to the serial converter.
📝
.connect()has to be triggered by user gesture (e.g. onclick)
var connected = await pump.connect(); // has to be triggered by user gesture (e.g. onclick)result:
- bool, connection to serial interface successful?
Disconnects from the serial converter.
await pump.disconnect();
📝 While the pump is running, a valid command has to be sent at least every second
// New method
pump.keepalive(callback, interval); // Start keepalive
pump.stopKeepalive(); // Stop keepalive
// Legacy method (still supported)
pump.settings.keepAlive.active = true;
pump.keepAlive();The legacy settings structure is still supported:
- bool
pump.settings.keepAlive.active, if keep alive feature is enabled, default: false - int
pump.settings.keepAlive.interval, interval after which the command is sent (in ms), default: 250 - string
pump.settings.keepAlive.command, default: "03FD000001" - function
pump.settings.keepAlive.callback, gets called with return value as parameter, optional
Starts or stops the pump (target frequency has to be set)
// New properties (recommended)
await (pump.run = true); // Start the pump
await (pump.run = false); // Stop the pump
await (pump.reverse = true); // Set reverse direction
await (pump.reverse = false);// Set forward direction
// Legacy methods (still supported)
await pump.start(); // starts the pump
await pump.startReverse(); // starts the pump in reverse
await pump.stop(); // stops the pump
Sets the target frequency
// Property syntax (recommended)
await (pump.frequency = frequency);
// Legacy syntax (also supported)
await (pump._frequency = frequency);parameters:
- float, positive (resolution: 0.01), target frequency in Hz
Gets the actual frequency
// Property syntax (recommended)
var frequency = await pump.frequency;
// Legacy syntax (also supported)
var frequency = await pump._frequency;result:
- float, positive (resolution: 0.01), actual frequency in Hz
Starts (or stops) the pump in the desired direction
⚠️ Do not switch betweenset frequencyandset speed
// Property syntax (recommended)
await (pump.speed = speed);parameters:
- float, negative to reverse (resolution: 0.01), target frequency
Gets the readiness of the machine (on)
// Property syntax (recommended)
var ready = await pump.s_ready;
// Legacy property (also supported)
var ready = await pump.ready;result:
- bool, machine is ready for operation
Gets the actual output voltage
// Property syntax (recommended)
var voltage = await pump.m_voltage;
// Legacy property (also supported)
var voltage = await pump.voltage;result:
- float, positive (resolution: 0.01), actual voltage in % of voltage rating
Gets the actual output current
// Property syntax (recommended)
var current = await pump.m_current;
// Legacy property (also supported)
var current = await pump.current;result:
- float, positive (resolution: 0.01), actual current in % of current rating
Gets the actual torque
// Property syntax (recommended)
var torque = await pump.m_torque;
// Legacy property (also supported)
var torque = await pump.torque;result:
- float, positive (resolution: 0.01), actual torque in % of torque rating
New status properties available in the Pump class:
var pumping = await pump.s_pumping; // Is pump running?
var forward = await pump.s_pumping_forward; // Is pump running forward?
var reverse = await pump.s_pumping_reverse; // Is pump running in reverse?
var running = await pump.run; // Is pump set to run?
var isReverse = await pump.reverse; // Is reverse direction set?
var realSpeed = await pump.m_speed; // Real speed (negative if reverse)
var setSpeed = pump.speed; // Get set speed (synchronous)
Sends custom command to inverter
// New methods (recommended)
var answer = await pump.read(parameter); // Read command
var answer = await pump.write(parameter, value); // Write command
// Legacy method (still supported)
var answer = await pump.sendCommand(parameter, value);parameters:
- string (length: 4-6), parameter number for read, or action + parameter for legacy method
- int, value (for write commands)
result:
- int, answer value (equals input value if write command)
The library now uses getter/setter properties like the Python implementation:
// Property-based API (recommended - matches Python structure)
await (pump.run = true);
await (pump.reverse = false);
await (pump.frequency = 50);
var speed = await pump.m_speed;
var voltage = await pump.m_voltage;
// Legacy property syntax (still supported)
await (pump.speed = 50);
var freq = await pump.frequency;The property names now match the Python implementation:
s_ready- machine ready statusrun- pump running statereverse- reverse direction states_pumping- pump is runnings_pumping_forward- pump running forwards_pumping_reverse- pump running reverse_frequency- internal frequency controlm_voltage,m_current,m_torque- measured valuesm_speed- measured speedspeed- set speed (getter only returns last set value)