In this article
Categories

Milesight UC100 payload decoder on TTNv3 for AlphaX

Print

Example fixed length LoRaWAN payload format for Milesight UC100 on TTN V3 console

Also can be found in the UC100 folder in product information

/**
* Payload Decoder for The Things Network and AlphaX
*
* @product UC100 series
*/
function Decoder(bytes, port) {
var decoded = {};
for (i = 0; i < bytes.length; ) { var channel_id = bytes[i++]; var channel_type = bytes[i++]; // POWER STATE if (channel_id === 0xff && channel_type === 0x0b) { //decoded.power = "on"; i += 1; } // IPSO VERSION else if (channel_id === 0xff && channel_type === 0x01) { //decoded.ipso_version = readProtocolVersion(bytes[i]); i += 1; } // PRODUCT SERIAL NUMBER else if (channel_id === 0xff && channel_type === 0x16) { //decoded.sn = readSerialNumber(bytes.slice(i, i + 8)); i += 8; } // HARDWARE VERSION else if (channel_id === 0xff && channel_type === 0x09) { //decoded.hardware_version = readHardwareVersion(bytes.slice(i, i + 2)); i += 2; } // SOFTWARE VERSION else if (channel_id === 0xff && channel_type === 0x0a) { //decoded.software_version = readSoftwareVersion(bytes.slice(i, i + 2)); i += 2; } // METRICS DATA REPORT else if (channel_id === 0xff && channel_type === 0x19) { var modbus_chn_id = bytes[i++] + 1; var data_length = bytes[i++]; var data_type = bytes[i++]; var chn = "chn" + modbus_chn_id; switch (data_type) { case 0: decoded.push({ channelId:modbus_chn_id, value:bytes[i] ? 1 : 0 }); i += 1; break; case 1: decoded.push({ channelId:modbus_chn_id, value:bytes[i] }); i += 1; break; case 2: case 3: decoded.push({ channelId:modbus_chn_id, value:readUInt16LE(bytes.slice(i, i + 2)) }); i += 2; break; case 4: case 6: case 8: case 9: case 10: case 11: decoded.push({ channelId:modbus_chn_id, value:readUInt32LE(bytes.slice(i, i + 4)) }); i += 4; break; case 5: case 7: decoded.push({ channelId:modbus_chn_id, value:readFloatLE(bytes.slice(i, i + 4)) }); i += 4; break; } } // MODBUS READ ERROR else if (channel_id === 0xff && channel_type === 0x15) { var modbus_chn_id = bytes[i] + 1; decoded.push({channelId:253,value:modbus_chn_id}); //decoded[channel_name] = true; i += 1; } else { break; } } return decoded; } /* ****************************************** * bytes to number ********************************************/ function readUInt8LE(bytes) { return bytes & 0xff; } function readInt8LE(bytes) { var ref = readUInt8LE(bytes); return ref > 0x7f ? ref - 0x100 : ref;
}

function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0]; return value & 0xffff; } function readInt16LE(bytes) { var ref = readUInt16LE(bytes); return ref > 0x7fff ? ref – 0x10000 : ref;
}

function readUInt32LE(bytes) {
var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0]; return value & 0xffffffff; } function readInt32LE(bytes) { var ref = readUInt32LE(bytes); return ref > 0x7fffffff ? ref – 0x100000000 : ref;
}

function readFloatLE(bytes) {
// JavaScript bitwise operators yield a 32 bits integer, not a float.
// Assume LSB (least significant byte first).
var bits = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; var sign = bits >>> 31 === 0 ? 1.0 : -1.0;
var e = (bits >>> 23) & 0xff;
var m = e === 0 ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000; var f = sign * m * Math.pow(2, e – 150); return f; } function readProtocolVersion(bytes) { var major = (bytes & 0xf0) >> 4;
var minor = bytes & 0x0f;
return “v” + major + “.” + minor;
}

function readHardwareVersion(bytes) {
var major = bytes[0] & 0xff;
var minor = (bytes[1] & 0xff) >> 4;
return “v” + major + “.” + minor;
}

function readSoftwareVersion(bytes) {
var major = bytes[0] & 0xff;
var minor = bytes[1] & 0xff;
return “v” + major + “.” + minor;
}

function readSerialNumber(bytes) {
var temp = [];
for (var idx = 0; idx < bytes.length; idx++) {
temp.push((“0” + (bytes[idx] & 0xff).toString(16)).slice(-2));
}
return temp.join(“”);
}