UNPKG

mercury-meter

Version:

A library for communicating with Mercury energy meters over TCP or serial port

44 lines (37 loc) 1.35 kB
const { MercuryBus } = require('./src/MercuryMeter'); const bus = new MercuryBus({ connectionType: 'tcp', host: '176.193.75.143', port: 4001, baudRate: 9600 }); // Добавление счетчиков с индивидуальными паролями и уровнями доступа bus.addMeter(0x4B, { accessLevel: 1, password: Buffer.from([1, 1, 1, 1, 1, 1]) }); bus.addMeter(0x4C, { accessLevel: 2, password: Buffer.from([2, 2, 2, 2, 2, 2]) }); const paramCodes = [0x00, 0x04, 0x08, 0x11, 0x12, 0x13, 0x21, 0x22, 0x23, 0x30, 0x40]; const options = { intervalBetweenParams: 100, intervalBetweenMeters: 200, maxRetries: 5, retryDelay: 100, responseTimeout: 500 }; // Подписка на события bus.on('data', (result) => { console.log(`Meter ${result.address} (0x${result.address.toString(16)}):`); console.log(' Parameters:', result.parameters); if (result.errors.length > 0) { console.log(' Errors:', result.errors); } }); bus.on('cycleComplete', (results) => { console.log('Polling cycle completed:', results); }); bus.on('error', (error) => { console.error('Polling error:', error); }); // Запуск циклического опроса bus.pollAllMetersCyclically(paramCodes, options, 5000).catch(async (error) => { console.error('Critical error:', error); await bus.close(); });