Relpy to TCP -- Trouble responding with a "1"

Hello,
I am trying to setup a simple flow, but having some issues and can't seem to find enough documentation.

I have a device that sends TCP data to my Node Red flow. Immediately after I receive the data, I need to respond with a number 1.

image

My function is as follows:

msg = 1
return msg;

Any thoughts on how to get Node Red to respond with the number 1 after data is received?

msg.payload = 1;

1 Like

Thank you... If I needed to send the reply in binary, would it just be a simple as:

msg.payload = 0x01;
return msg;

Specifically, here is what I am trying to accomplish:

First, when the module connects to the server, the module sends its IMEI. First comes a short identifying the number of bytes written and then goes IMEI as text (bytes).
For example, IMEI 356307042441013 would be sent as 000F333536333037303432343431303133.
First two bytes denote IMEI length. In this case 0x000F means, that IMEI is 15 bytes long.
After receiving IMEI, server should determine if it would accept data from this module. If yes, server will reply to module 01, if not - 00. Note that confirmation should be sent as binary packet. I.e. 1 byte 0x01 or 0x00.
Then the module starts to send the first AVL data packet. After the server receives a packet and parses it, the server must report to the module number of data received as integer (four bytes).
If sent data number and reported by the server doesn’t match module resends sent data.

    Example:

Module connects to server and sends IMEI:
000F333536333037303432343431303133
Server accepts the module:
01
Module sends data packet

I am receiving the IMEI and trying to respond with a 01, which should tell the AVL Module to send the remaining data.

Here is my updated flow with the current debugging outputs:

Use a buffer..

Function node...

msg.paylaod = Buffer.from([1]);
return msg;

Attach a debug after the function/before the TCP out node. You will see the payload is a binary buffer containing 1 byte with a value of 1.

Ps, 1 is the same as 0x1 :slight_smile:

Thank you for the feedback. However, I must still have something wrong...

The first debug window (on the top) seems to be dumping the IMEI, which is correct. But I would have assumed the second debug window should be dumping my message payload, which should be a value 01. However, it is also dumping the IMEI.

My new function node has the following:

msg.paylaod = Buffer.from([1]);
return msg;

misspelling
check again!

1 Like

Hahahah, Awesome! Working great now!

1 Like

One more question... Anyone have any idea why this payload is not in clear text?

Where is it coming from?

Thats your data from the tcp in node, I assume you have to convert it from a buffer to make it readable

EDIT: please be careful with the following steps. Your flow could eventually go into a loop. The function node will continue to send "1" every time it receives a message on the input. Maybe this does not harm your device...or it will go banas

The data is coming from a Teltonika GPS tracking device.

Thank you for the feedback. I tried converting from a buffer to a string, but the data is still not possible to read.

My function:

msg.payload = msg.payload.toString();
return msg;

Your Payload is already a string so calling toString will make no difference. Set your TCP in node to return a buffer.

You will probably need a manual to decipher the values.

Perhaps node-red-contrib-buffer-parser will help you decode the values.

Thank you.. I have changed the TCP IN node to return a buffer, and I am now seeing the following data:

I do have the manual, and when reviewing it, it suggests that I should be receiving a string of data:

Received data in hexadecimal stream:
000000000000004A8E010000016B412CEE000100000000000000000000000000000000010005000100010100010011001D00010010015E2C880002000B000000003544C87 A000E000000001DD7E06A00000100002994

The manual then discusses decoding:

Any thoughts on this? Basically, how do I get the data into a format as described in the manual (a hexadecimal stream)

Use node-red-contrib-buffer-parser.

demo flow...

[{"id":"e8641c6b.236eb","type":"buffer-parser","z":"4b3f21a3.ba434","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32be","name":"data_field_length","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"codec","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"dataRecordcount","offset":9,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"timestamp","offset":10,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"priority","offset":18,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"longitude","offset":19,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"latitude","offset":23,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"altitude","offset":27,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"angle","offset":29,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":930,"y":2220,"wires":[["ee99ffa3.08f7a"]]},{"id":"621b24f8.a25abc","type":"inject","z":"4b3f21a3.ba434","name":"your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"000000000000004A8E010000016B412CEE000100000000000000000000000000000000010005000100010100010011001D00010010015E2C880002000B000000003544C87","payloadType":"str","x":780,"y":2220,"wires":[["e8641c6b.236eb"]]},{"id":"69b74bc5.10de64","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":2280,"wires":[]},{"id":"ee99ffa3.08f7a","type":"function","z":"4b3f21a3.ba434","name":"convert timestamp","func":"var ts = (BigInt(msg.payload.timestamp[0])*BigInt(0xffffffff) ) + BigInt(msg.payload.timestamp[1])\nmsg.payload.timestamp = new Date(Number(ts));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":2280,"wires":[["69b74bc5.10de64"]]}]
1 Like

Thank you for the working example. This was perfect and exactly what I needed to see in order to understand. Thank you.

1 Like

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