I want to save the addresses for an i2c device into a JSON file, but I have no idea how to parse the strings I get back into the addresses in hex notation.
const dev_address = this.parameters['DEVICE_ADDR']; // 0x04
First I tried .parseInt(this.parameters['DEVICE_ADDR'], 16)
, then I thought the addresses might be some kind of byte[]
and tried multiple things using buffer.from(str)
and .toString('hex')
without success.
How is this done?
Reference
'use strict';
const logger = require('../controller/LogController');
const i2c = require('i2c-bus');
class ArduinoPlug_on {
constructor(parameters) {
this.parameters = parameters;
}
run(env) {
logger.debug('try connection', this.parameters);
const dev_address = this.parameters['DEVICE_ADDR']; // 0x04
const opt_address = this.parameters['OPTION_ADDR']; // 0x00
const i2c1 = i2c.openSync(1);
const bytesWritten = i2c1.i2cWriteSync(dev_address, 2, Buffer.from([opt_address,0x01]));
if( bytesWritten == 0 ) {
logger.error("could not write data", err);
}
i2c1.closeSync();
}
release(env) {
}
}
module.exports = ArduinoPlug_on;