I am using node-Red the function. I have got a string like this one:
{"txpk":{"imme":false,"tmst":145559484,"freq":869.525,"rfch":0,"powe":27,"modu":"LORA","datr":"SF9BW125","codr":"4/5","ipol":true,"size":17,"data":"YA0iASalAAADMf8AAbKwI3I="}}
This is the message. The payload is coming in the function block.
All I want is to add some Bytes in front of the string:
buf = new Buffer('buff');
buf1 = new Buffer ('0000');
buf[0]=0x02;
buf[1]=0xbd;
buf[2]=0x45;
buf[3]=0x03;
buf1= msg.payload;
msg.payload = buf+buf1;
return [null,msg];
The result is als follows writing in hex:
02 EF BF BD 45 03 7B 22 74 78 70 6B 22 3A 7B 22 69 6D 6D 65 22 3A 66 61 6C 73 65 2C 22 74 6D 73 74 22 3A 31 34 35 35 35 39 34 38 34 2C 22 66 72 65 71 22 3A 38 36 39 2E 35 32 35 2C 22 72 66 63 68 22 3A 30 2C 22 70 6F 77 65 22 3A 32 37 2C 22 6D 6F 64 75 22 3A 22 4C 4F 52 41 22 2C 22 64 61 74 72 22 3A 22 53 46 39 42 57 31 32 35 22 2C 22 63 6F 64 72 22 3A 22 34 2F 35 22 2C 22 69 70 6F 6C 22 3A 74 72 75 65 2C 22 73 69 7A 65 22 3A 31 37 2C 22 64 61 74 61 22 3A 22 59 41 30 69 41 53 61 6C 41 41 41 44 4D 66 38 41 41 62 4B 77 49 33 49 3D 22 7D 7D 22 3B 0A
Instead of :
02 BD 45 03
I see at the first bytes:
02 EF BF BD 45 03
Could anyone explain what I did wrong?
You are trying to join a Buffer object with a String - which is the cause of your problem. It is better to convert the String to a Buffer then use Buffer.concat()
to join them:
buf = new Buffer('buff');
buf1 = new Buffer (msg.payload);
buf[0]=0x02;
buf[1]=0xbd;
buf[2]=0x45;
buf[3]=0x03;
msg.payload = Buffer.concat([buf, buf1]);
return [null,msg];