78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
const HMUARTLGW_DST_OS = 0
|
|
const HMUARTLGW_DST_APP = '1'
|
|
|
|
const crc = function (message) {
|
|
var $crc = 0xd77f
|
|
var buf = Buffer.from(message, 'hex')
|
|
for (var byte of buf) {
|
|
$crc ^= (byte << 8) & 0xff00
|
|
for (var $i = 0; $i < 8; $i++) {
|
|
if ($crc & 0x8000) {
|
|
$crc = ($crc << 1) & 0xffff
|
|
$crc ^= 0x8005
|
|
} else {
|
|
$crc = ($crc << 1) & 0xffff
|
|
}
|
|
}
|
|
}
|
|
return $crc.toString(16)
|
|
}
|
|
|
|
let leading = 'fd00'
|
|
let dst = String('0' + String(HMUARTLGW_DST_APP))
|
|
let msgCountPerDevice = 0
|
|
let msgCountPerCoordinator = 19
|
|
let length = 0
|
|
let filler = '000000'
|
|
let payload = `A0 11 670A2A 19C4DB 0201 C8 0000`.replace(/ /g, '').toString('hex')
|
|
|
|
// increase counter
|
|
msgCountPerDevice = parseInt(msgCountPerDevice + 1, 10)
|
|
msgCountPerCoordinator = parseInt(msgCountPerCoordinator + 1, 10)
|
|
|
|
// calculate length
|
|
length = ((payload.length + 2) / 2)
|
|
|
|
// get hex values from counter
|
|
let msgCountPerDeviceHex = parseInt(msgCountPerDevice, 10).toString(16)
|
|
let msgCountPerCoordinatorHex = parseInt(msgCountPerCoordinator, 10).toString(16)
|
|
|
|
if (msgCountPerDeviceHex.length === 1) {
|
|
msgCountPerDeviceHex = '0' + msgCountPerDeviceHex
|
|
}
|
|
|
|
if (msgCountPerCoordinatorHex.length === 1) {
|
|
msgCountPerCoordinatorHex = '0' + msgCountPerCoordinatorHex
|
|
}
|
|
|
|
// build frame
|
|
let frame = `${leading} ${length} ${dst} ${msgCountPerCoordinatorHex} 02 ${filler} ${msgCountPerDeviceHex} ${payload}`.replace(/ /g, '')
|
|
|
|
// calculate crc
|
|
frame += crc(frame)
|
|
|
|
const net = require('net');
|
|
const client = net.createConnection({ port: 9000, host: '192.168.178.95' }, () => {
|
|
// 'connect' listener
|
|
console.log('connected to server!');
|
|
console.log(Buffer.from(frame, 'hex'))
|
|
client.write(Buffer.from(frame, 'hex'));
|
|
//client.write(Buffer.from('fd00030001009e03', 'hex'));
|
|
//client.write(Buffer.from('fd00030002039409', 'hex'));
|
|
});
|
|
client.on('data', (data) => {
|
|
console.log(data.toString())
|
|
});
|
|
client.on('end', () => {
|
|
console.log('disconnected from server');
|
|
})
|
|
|
|
// C: fd00 03 0001 009e03
|
|
// D: fd00 0e 0001 0402 436f5f4350555f417070
|
|
|
|
// C: fd00 03 0002 039409
|
|
// D: fd00 04 0002 04011916 -> ?
|
|
// D: fd00 0d 000000 436f -> Co
|
|
// D: 5f4350555f417070d831 -> CPU_APP
|
|
// D: fd00 12 0100 050000 -> ?
|
|
// D 26a186701ac33300000000ef3fa001
|