In nodered how to send data integer (four bytes) to tcp device

I was integrating with the teltonica GPS device while integrating codec8 GPS device. So, as per document I have to send number of data received as integer (four bytes), means the number of data in the packet in integer(four bytes)

Forexample: Like if data is(according to document): 000000000000008c02010000013feb55ff74000f0ea850209a6900009400d6120000....

Then we have reply like(according to document): Server acknowledges data reception (2 data elements): 00000002

But in node-red I am sending like this msg.payload=00000008 to the same tcp socket which listened. The device is disconnecting so, according to document I have to send number of data in the packet in integer(four bytes) but I am not getting whether I am sending correct data or not.

doument reference:
https://wiki.teltonika-networks.com/view/VCode

[{"id":"88561e59.89ab28","type":"tcp in","z":"87b67951.66a368","name":"","server":"server","host":"","port":"5000","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":80,"y":280,"wires":[["5d1bfe86.a4f6","ea18753d.16a81"]]},{"id":"cf22fdb2.09f25","type":"tcp out","z":"87b67951.66a368","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"","x":650,"y":200,"wires":[]},{"id":"5d1bfe86.a4f6","type":"function","z":"87b67951.66a368","name":"","func":"var session=msg._session.id;\nvar temp=msg.payload;\nvar time=3600;\nvar redis = global.get('redis');\nvar client = redis.createClient(6379,'192.168.8.213'); //creates a new client\n client.on('connect', function() {\n console.log('redis connected');\n });\nclient.get(session, function (err, obj) {\n console.dir(obj);\n if(obj==null)\n{\nlet imei = Buffer(temp, 'ascii').toString('hex');\nimei=imei.slice(4);\nconsole.log(\"imei:::::\"+imei)\nclient.set(session,imei,function(err, reply){\n node.send({payload:\"\",reply:new Buffer([0x01]),status:\"imei\"});\n});\n\n}\nelse \n{\n client.get(session, function (err, obj) {\n console.dir(obj);\n var str=temp;\nlet val = Buffer(str, 'ascii').toString('hex');\nvar input=obj;\nvar output = new Buffer(input, 'hex');\nvar mac=output.toString();\n node.send({payload:val+\",\"+mac,reply:00000008,status:\"data\"});\n });\n}\n\n});\nclient.expire(session, parseInt(time));\n","outputs":1,"noerr":0,"x":270,"y":340,"wires":[["47265bf5.6ed424","a94be8d1.88637"]]},{"id":"4a7a1383.85c44c","type":"kafka","z":"87b67951.66a368","zkquorum":"ubuntu1:2181","topics":"itr_gps_in","debug":false,"x":690,"y":340,"wires":[]},{"id":"13511b15.c0f615","type":"debug","z":"87b67951.66a368","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":400,"wires":[]},{"id":"47265bf5.6ed424","type":"function","z":"87b67951.66a368","name":"","func":"msg.payload=msg.reply\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":260,"wires":[["cf22fdb2.09f25","5dcc2f78.8788a"]]},{"id":"5dcc2f78.8788a","type":"debug","z":"87b67951.66a368","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":650,"y":280,"wires":[]},{"id":"ea18753d.16a81","type":"debug","z":"87b67951.66a368","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":290,"y":440,"wires":[]},{"id":"a94be8d1.88637","type":"switch","z":"87b67951.66a368","name":"","property":"status","propertyType":"msg","rules":[{"t":"eq","v":"data","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":340,"wires":[["4a7a1383.85c44c","13511b15.c0f615"]]}]

Hi firstly, in order to make code more readable and importable it is important to post it between two sets of three backticks - ``` - see this post for more details - How to share code or flow json

You should go back and edit your post.

Secondly, setting msg.payload=00000008 is just setting the number 8 - e.g. you might as well have written msg.payload=8 - in other words, this in not correct.

It is more likely you need to send a Buffer

An example would be - to send 000000000000008c02010000013feb55ff74000f0ea850209a6900009400d6120000 you need to convert it to a bytes (a Buffer) which can be either hard coded or converted in a function node like this...

msg.payload = Buffer.from("000000000000008c02010000013feb55ff74000f0ea850209a6900009400d6120000","hex");
return msg;

Here is some proof...

Lastly, there is also a CRC to calculate to append to the end. This is not so straight forward.

In this post here an example where I calculated CRC in a function node for another user - you might be able to adapt it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.