Sample buffer Data for testing

For testing my project without hardware I need a sample buffer data of which is stream of bytes buffer, so I created

const sample_bufferData = Buffer.alloc(300);

then

the data I received is in this format, for ex: 1st four bytes has ascii format address from sample_bufferData[0-3], and then 2 bytes of data format of BCD i.e., address 4-5 bytes
.
for this first 6 buffer bytes 0 to 3 and 4to 5 How can I declare them?

Many thanks

You can do it in a function using the node Buffer functions like writeInt16BE etc or you can use a helper node.

e.g. node-red-contrib-buffer-parser has a companion node named buffer-maker

Here is an example based on a Balluff TCP Command (Command L, page 58 in this manual)...

image

[{"id":"1b3cc0f132ae6ca1","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":720,"wires":[["ad1d238796790dff"]]},{"id":"ad1d238796790dff","type":"buffer-maker","z":"c1ebfd3083f3904f","name":"Command 'L'","specification":"spec","specificationType":"ui","items":[{"name":"Command","type":"ascii","length":1,"dataType":"str","data":"L"},{"name":"StartAddr","type":"ascii","length":6,"dataType":"str","data":"000000"},{"name":"NoofBytes","type":"ascii","length":6,"dataType":"str","data":"001024"},{"name":"HeadNo","type":"ascii","length":1,"dataType":"str","data":"1"},{"name":"Reserve","type":"ascii","length":1,"dataType":"str","data":"R"},{"name":"BCC","type":"byte","length":1,"dataType":"num","data":"0"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":390,"y":720,"wires":[["6aca84b711a30260"]]},{"id":"6aca84b711a30260","type":"function","z":"c1ebfd3083f3904f","name":"Calc BCC","func":"function calculateBcc(messageBytes, countOfBytes) {\n    let loopCount = 0;\n    let bccValue = 0x00;\n    for (loopCount = 0; loopCount < countOfBytes; loopCount++) {\n        bccValue = bccValue ^ messageBytes[loopCount];\n    }\n    return (bccValue & 0xFF);\n}\nconst len = msg.payload.length;\nconst BCC = calculateBcc(msg.payload, len - 1)\n\n//write BCC to the buffer in last position\nmsg.payload.writeInt8(BCC, len - 1)\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":780,"wires":[["04a5343c01815dd0"]]},{"id":"04a5343c01815dd0","type":"debug","z":"c1ebfd3083f3904f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":780,"wires":[]}]
1 Like

Hallo @Steve-Mcl ,

Many thanks for your help. I opened a separate post because as I currently don't have hardware and trying to write function where I want to get sample buffer data by triggering this read process. But now with this buffer maker node I can be able to do sample buffer data what I can get from this Balluff unit.
Many thanks again man. You are awesome :smile:

By generating Fake data I have a BCD data type of 3 bytes length, Just say for example: 8000(decimal)
Len

In BCD I can give it as: 000000001000000000000000.
In BufferMaker node I gave type as BCD(le) and length of 3 bytes and in msg I gave 000000001000000000000000
Why the buffer is creating 6 bytes where I gave length as 3 ?

Because BCD is a 16bit operation.

What numeric value are you trying to send (as in decimal or hexadecimal)?

By generating fake data from rfid chip, I get BCD and ASCII format data types:
For ASCII I’m clear as: For ex: a length of 32 bytes: the unused bytes as 0 and used bytes with asciistring
By BCD kind of format data for ex I want to generate +102.8, (for + sign char B is used, - sign char D and for . E is used)--> B102E800 is the BCD value I get,
I gave in buffer maker : type of BCD length of 4 bytes and string as B102E800,

ok, so I'm sure you realise, BCD is Binary Coded Decimal and its hexadecimal value will never contain any letters (e.g. 16bit BCD is 0x0000 > 0x9999, 32bit BCD is 0x0000000 > 0x99999999). What you have there is a custom data type.

You will need to construct those ascii characters then use the buffer maker ASCII type.

e.g...

/*
for + sign char B is used
for - sign char D is used 
for . E is used

e.g. +102.8 == B102E800
*/

const DP = "E";
const val = msg.payload.toString().trim();
const parts = val.split(".");
let sign = "B";
let leftOfDP = parts[0];
let rightOfDP = (parts[1] || "0");
if (val.startsWith("-")) {
    sign = "D";
    leftOfDP = leftOfDP.substring(1);//drop "-"
}
if (leftOfDP.length > 3) {
    node.error(`Cannot convert ${val} to custom BCD format`, msg);
    return;
}

leftOfDP = leftOfDP.padStart(3, "0");
rightOfDP = rightOfDP.padEnd(3, "0").substring(0, 3);

msg.payload = `${sign}${leftOfDP}${DP}${rightOfDP}`

return msg;

chrome_wLc7C27HkJ

1 Like

Hallo Steve-Mcl,

got some doubts with tcp nodes: is it correct what I am doing.?
I start read or write process using tcp request node and then I get response from tcp/ip device, with function node I can get ACK or NAK, with ACK I need to reply STX(start of text) and then with tcp in node as listener with port no is it correct? :face_with_hand_over_mouth:

The best way to do this is to actually fake the device.

Example...

[{"id":"d0ed337.b37dbd","type":"tcp in","z":"62dd2f0711b4f336","name":"Fake TCP device:1025","server":"server","host":"","port":"1025","datamode":"stream","datatype":"buffer","newline":"\\n","topic":"text","base64":false,"tls":"","x":620,"y":1140,"wires":[["ef1f8f592f30c415"]]},{"id":"c4fce4d0.df1838","type":"tcp out","z":"62dd2f0711b4f336","name":"reply: all open connections will be messaged","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":1710,"y":1160,"wires":[]},{"id":"7de967f0.d50a08","type":"tcp request","z":"62dd2f0711b4f336","name":"Send command to TCP Device","server":"localhost","port":"1025","out":"time","ret":"buffer","splitc":"100","newline":"","tls":"","x":750,"y":1480,"wires":[["33b50ec6a7d19f8c","3172d13778f65792"]]},{"id":"ef1f8f592f30c415","type":"switch","z":"62dd2f0711b4f336","name":"Test Command is L or W","property":"payload[0]","propertyType":"msg","rules":[{"t":"eq","v":"76","vt":"num"},{"t":"eq","v":"78","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":690,"y":1080,"wires":[["0ff8a391ce298b2c"],["a647359b690ce1f2"],["1066a7a015c405ae"]]},{"id":"8f14c1c0a2f1dd14","type":"change","z":"62dd2f0711b4f336","name":"","rules":[{"t":"set","p":"lastCommand","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1280,"y":1040,"wires":[["80b2699cfc2ed208"]]},{"id":"1066a7a015c405ae","type":"switch","z":"62dd2f0711b4f336","name":"is STX?","property":"payload[0]","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":940,"y":1120,"wires":[["288fbabfce2bfaec"],["acc8c62738c99972"]]},{"id":"288fbabfce2bfaec","type":"function","z":"62dd2f0711b4f336","name":"Prepare response  (TODO)","func":"var lastCommand = flow.get(\"lastCommand\");\nvar len = Number(lastCommand.byteCount);\nvar buf = Buffer.alloc(len)\nif (lastCommand.command == \"L\") {\n    for (let i = 0; i < len; i++) {\n        const v = rand(65,90)\n        buf.writeUInt8(v,i);\n    }\n    msg.payload = buf;\n}\n\nreturn msg;\n\n\n\nfunction rand(min, max) {\n    return Math.floor(min + Math.random() * (max + 1 - min))\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1220,"y":1120,"wires":[["95d2e329b3fc8d7b"]]},{"id":"80b2699cfc2ed208","type":"change","z":"62dd2f0711b4f336","name":"Set ACK","rules":[{"t":"set","p":"payload","pt":"msg","to":"[6]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":1460,"y":1040,"wires":[["c4fce4d0.df1838"]]},{"id":"0ff8a391ce298b2c","type":"buffer-parser","z":"62dd2f0711b4f336","name":"Parse L command","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"ascii","name":"command","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"string","name":"startAddress","offset":1,"length":6,"offsetbit":0,"scale":"1","mask":""},{"type":"string","name":"byteCount","offset":7,"length":6,"offsetbit":0,"scale":"1","mask":""},{"type":"ascii","name":"headNo","offset":13,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"BCC","offset":15,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":970,"y":1020,"wires":[["8f14c1c0a2f1dd14","f7f5d12de11aeade"]]},{"id":"a647359b690ce1f2","type":"buffer-parser","z":"62dd2f0711b4f336","name":"Parse W command (TODO)","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"buffer","name":"item1","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1000,"y":1060,"wires":[["8f14c1c0a2f1dd14","1a5f4d450eb8d2ca"]]},{"id":"1b3cc0f132ae6ca1","type":"inject","z":"62dd2f0711b4f336","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":1340,"wires":[["ad1d238796790dff"]]},{"id":"ad1d238796790dff","type":"buffer-maker","z":"62dd2f0711b4f336","name":"Command 'L'","specification":"spec","specificationType":"ui","items":[{"name":"Command","type":"ascii","length":1,"dataType":"str","data":"L"},{"name":"StartAddr","type":"ascii","length":6,"dataType":"str","data":"000000"},{"name":"NoofBytes","type":"ascii","length":6,"dataType":"str","data":"001024"},{"name":"HeadNo","type":"ascii","length":1,"dataType":"str","data":"1"},{"name":"Reserve","type":"ascii","length":1,"dataType":"str","data":"R"},{"name":"BCC","type":"byte","length":1,"dataType":"num","data":"0"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":630,"y":1340,"wires":[["6aca84b711a30260"]]},{"id":"6aca84b711a30260","type":"function","z":"62dd2f0711b4f336","name":"Calc BCC","func":"function calculateBcc(messageBytes, countOfBytes) {\n    let loopCount = 0;\n    let bccValue = 0x00;\n    for (loopCount = 0; loopCount < countOfBytes; loopCount++) {\n        bccValue = bccValue ^ messageBytes[loopCount];\n    }\n    return (bccValue & 0xFF);\n}\nconst len = msg.payload.length;\nconst BCC = calculateBcc(msg.payload, len - 1)\n\n//write BCC to the buffer in last position\nmsg.payload.writeInt8(BCC, len - 1)\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":860,"y":1360,"wires":[["a87d67637ce96238"]]},{"id":"a0a58c5af2d2e845","type":"inject","z":"62dd2f0711b4f336","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":1400,"wires":[["383b078841f93c48"]]},{"id":"383b078841f93c48","type":"buffer-maker","z":"62dd2f0711b4f336","name":"Command 'W' (TODO)","specification":"spec","specificationType":"ui","items":[{"name":"Command","type":"ascii","length":1,"dataType":"str","data":"W"},{"name":"StartAddr","type":"ascii","length":6,"dataType":"str","data":"000000"},{"name":"NoofBytes","type":"ascii","length":6,"dataType":"str","data":"001024"},{"name":"HeadNo","type":"ascii","length":1,"dataType":"str","data":"1"},{"name":"Reserve","type":"ascii","length":1,"dataType":"str","data":"R"},{"name":"BCC","type":"byte","length":1,"dataType":"num","data":"0"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":660,"y":1400,"wires":[["6aca84b711a30260"]]},{"id":"33b50ec6a7d19f8c","type":"switch","z":"62dd2f0711b4f336","name":"is ACK, NAK, Other?","property":"payload[0]","propertyType":"msg","rules":[{"t":"eq","v":"6","vt":"num"},{"t":"eq","v":"21","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":1020,"y":1480,"wires":[["bdb523b99989e21f"],["8a3511d98cb0d51c"],["3b6b53b5e85512b2"]]},{"id":"af79b6f83fc7b11e","type":"tcp request","z":"62dd2f0711b4f336","name":"Send command to TCP Device","server":"localhost","port":"1025","out":"time","ret":"buffer","splitc":"100","newline":"","tls":"","x":1490,"y":1400,"wires":[["8d14c4116d47f497"]]},{"id":"bdb523b99989e21f","type":"change","z":"62dd2f0711b4f336","name":"Set STX","rules":[{"t":"set","p":"payload","pt":"msg","to":"[1]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":1260,"y":1400,"wires":[["af79b6f83fc7b11e","a6eb4228c7fe7b98"]]},{"id":"16a40457ee83d33b","type":"debug","z":"62dd2f0711b4f336","name":"4: Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1900,"y":1480,"wires":[]},{"id":"f7f5d12de11aeade","type":"debug","z":"62dd2f0711b4f336","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":1000,"wires":[]},{"id":"1a5f4d450eb8d2ca","type":"debug","z":"62dd2f0711b4f336","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":1080,"wires":[]},{"id":"acc8c62738c99972","type":"function","z":"62dd2f0711b4f336","name":"Prepare Error response  (TODO)","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1190,"y":1160,"wires":[["c4fce4d0.df1838"]]},{"id":"3b6b53b5e85512b2","type":"function","z":"62dd2f0711b4f336","name":"Prepare Error Log (TODO)","func":"node.error(\"Unexpected response from device\", msg);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1300,"y":1520,"wires":[[]]},{"id":"8a3511d98cb0d51c","type":"function","z":"62dd2f0711b4f336","name":"NAK - Prepare Error for Log (TODO)","func":"node.error(\"NAK response from device\", msg);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1330,"y":1480,"wires":[[]]},{"id":"dccda30af7322067","type":"debug","z":"62dd2f0711b4f336","name":"1: To Device","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":1320,"wires":[]},{"id":"7fd40b8c13dff1b3","type":"debug","z":"62dd2f0711b4f336","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":960,"wires":[]},{"id":"3172d13778f65792","type":"debug","z":"62dd2f0711b4f336","name":"2: From Device","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":1520,"wires":[]},{"id":"a6eb4228c7fe7b98","type":"debug","z":"62dd2f0711b4f336","name":"3: Send STX to device","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1440,"y":1360,"wires":[]},{"id":"95d2e329b3fc8d7b","type":"function","z":"62dd2f0711b4f336","name":"Calc BCC","func":"function calculateBcc(messageBytes, countOfBytes) {\n    let loopCount = 0;\n    let bccValue = 0x00;\n    for (loopCount = 0; loopCount < countOfBytes; loopCount++) {\n        bccValue = bccValue ^ messageBytes[loopCount];\n    }\n    return (bccValue & 0xFF);\n}\nconst len = msg.payload.length;\nconst BCC = calculateBcc(msg.payload, len - 1)\n\n//write BCC to the buffer in last position\nmsg.payload.writeInt8(BCC, len - 1)\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1440,"y":1120,"wires":[["c4fce4d0.df1838"]]},{"id":"8d14c4116d47f497","type":"switch","z":"62dd2f0711b4f336","name":"Test Command is L or W","property":"thisCommand[0]","propertyType":"msg","rules":[{"t":"eq","v":"76","vt":"num"},{"t":"eq","v":"78","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":1610,"y":1460,"wires":[["38d94462d8a0c58e"],[],[]]},{"id":"38d94462d8a0c58e","type":"buffer-parser","z":"62dd2f0711b4f336","name":"Parse L result","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"buffer","name":"item1","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1840,"y":1440,"wires":[["16a40457ee83d33b"]]},{"id":"a87d67637ce96238","type":"change","z":"62dd2f0711b4f336","name":"store command","rules":[{"t":"set","p":"thisCommand","pt":"msg","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":1360,"wires":[["dccda30af7322067","7de967f0.d50a08"]]}]
1 Like

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