From ring-buffer to string with out ","

Hi all .
My Flow read data from serial port node , then sed it to ring-buffer node
then can see it like Arduino serial port terminal .
Here is part of my data :

,Append spreadsheet values...
,----------------------------
,05/05/2022 19:10:33
,
,{
,	"spreadsheetId": "1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI",
,	"tableRange": "NewSheet!A1:D79176",
,	"updates": {
,		"spreadsheetId": "1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI",
,		"updatedRange": "NewSheet!A79177:D79177",
,		"updatedRows": 1,
,		"updatedColumns": 4,
,		"updatedCells": 4
,	}
,}
,

but every line is start with , character.
How I can remove this character form all lines ?

regards.

Try spliting and joining on "\n,", then slice the first few elements off. Then you can run it through a json node to create an JS Object if you wish to.

[{"id":"da182adb95eba196","type":"inject","z":"ba105eae32da85ae","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":160,"wires":[["f127c168.14115"]]},{"id":"f127c168.14115","type":"template","z":"ba105eae32da85ae","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":",Append spreadsheet values...\n,----------------------------\n,05/05/2022 19:10:33\n,\n,{\n,\t\"spreadsheetId\": \"1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI\",\n,\t\"tableRange\": \"NewSheet!A1:D79176\",\n,\t\"updates\": {\n,\t\t\"spreadsheetId\": \"1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI\",\n,\t\t\"updatedRange\": \"NewSheet!A79177:D79177\",\n,\t\t\"updatedRows\": 1,\n,\t\t\"updatedColumns\": 4,\n,\t\t\"updatedCells\": 4\n,\t}\n,}\n,","output":"str","x":270,"y":160,"wires":[["add5649e81836764"]]},{"id":"add5649e81836764","type":"function","z":"ba105eae32da85ae","name":"","func":"msg.payload = msg.payload.split(/\\n,/).slice(3).join(\"\\n\")\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":160,"wires":[["dd691b70.dd8548","01efa64e381505ef"]]},{"id":"dd691b70.dd8548","type":"json","z":"ba105eae32da85ae","name":"","property":"payload","action":"","pretty":false,"x":550,"y":160,"wires":[["01efa64e381505ef"]]},{"id":"01efa64e381505ef","type":"debug","z":"ba105eae32da85ae","name":"Function result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":220,"wires":[]}]

Hi.
Thanks for help... but not working ..

{
	"spreadsheetId": "1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI",
	"tableRange": "NewSheet!A1:D79176",
	"updates": {
		"spreadsheetId": "1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI",
		"updatedRange": "NewSheet!A79177:D79177",
		"updatedRows": 1,
		"updatedColumns": 4,
		"updatedCells": 4
	}
}

this is result .. but missing

Append spreadsheet values...
----------------------------
05/05/2022 19:10:33

If you require a string including the first 3 lines as well then remove the slice() and add a substr to end.
eg

[{"id":"da182adb95eba196","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":100,"wires":[["f127c168.14115"]]},{"id":"f127c168.14115","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":",Append spreadsheet values...\n,----------------------------\n,05/05/2022 19:10:33\n,\n,{\n,\t\"spreadsheetId\": \"1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI\",\n,\t\"tableRange\": \"NewSheet!A1:D79176\",\n,\t\"updates\": {\n,\t\t\"spreadsheetId\": \"1NO1FDWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI\",\n,\t\t\"updatedRange\": \"NewSheet!A79177:D79177\",\n,\t\t\"updatedRows\": 1,\n,\t\t\"updatedColumns\": 4,\n,\t\t\"updatedCells\": 4\n,\t}\n,}\n,","output":"str","x":310,"y":100,"wires":[["add5649e81836764"]]},{"id":"add5649e81836764","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = msg.payload.split(/\\n,/).join(\"\\n\").substr(1)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":100,"wires":[["01efa64e381505ef"]]},{"id":"01efa64e381505ef","type":"debug","z":"bf9e1e33.030598","name":"Function result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":160,"wires":[]}]

If connect function what write you to node Ring-buffer then shot this error :

"TypeError: msg.payload.split is not a function"

what is wrong ?
from ring-buffer is this :

7. 5. 2022, 13:51:16node: 063acdcefef2a1c4
msg.payload : array[15]
array[15]
[0 … 9]
0: "↵"
1: "Append spreadsheet values...↵"
2: "----------------------------↵"
3: "07/05/2022 13:51:10↵"
4: "↵"
5: "{↵"
6: "→"spreadsheetId": "1NO1FFWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI",↵"
7: "→"tableRange": "NewSheet!A1:D94385",↵"
8: "→"updates": {↵"
9: "→→"spreadsheetId": "1NO1FFWAwbXCryi9bSbD1h99oszagPmJ7g2S3pSWSTWI",↵"
[10 … 14]
10: "→→"updatedRange": "NewSheet!A94386:D94386",↵"
11: "→→"updatedRows": 1,↵"
12: "→→"updatedColumns": 4,↵"
13: "→→"updatedCells": 4↵"
14: "→}↵"

then ...
sorry my mistake , I don't have string .. I have array ..
in template node is this code :

<!--<pre><code id="shell" class="language-bash bash" ng-bind="msg.payload"></code></pre>-->
<pre><code id="shell" class="language-bash bash"></code></pre>

<script>
    $(document).ready(function() {
      $('#shell').each(function(i, e) {hljs.highlightElement(e)});
    }); 
</script>


<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg) {
        debugger
      $("#shell").text(msg.payload);
      $('#shell').each(function(i, e) {hljs.highlightElement(e)});
      //$("#shell").scrollTop(function() { return this.scrollHeight; });
      $("#shell").closest(".nr-dashboard-template").scrollTop(function() { return this.scrollHeight; });
    }
  });
})(scope);
</script>

I don't know what exactly do this code .. but all working only first char is '
regards.

Hi.
I find solution.
I use this in function:

for (let i = 0; i < 64; i++) {
    msg.payload += msg.payload[i];
};
msg.payload = msg.payload.split(/\n,/).join("\n").substr(1)
return msg;

but if you can help me pls .. now I defined buffer form 0 to 63 , how I can check how many lines are in array ? for (let i = 0; i < 64; i++) for payload[i] , but if change in ring-buffer value to 128 for example , must redefine code , how I can read value from ring-buffer how many part stored in array ?

all arrays have a length property.
so

for (let i = 0; i < msg.payload.length; i++) {
    msg.payload += msg.payload[i];
};

should work.

hehe... my orangepi zero stop working .. must solve it first..

but must working ..

regards.

ehm I don't know if start node-red then start serial port in node , and then halt server , and only restart my orange pi zero help ..

ehm : pls explain to me what exactly do this code : msg.payload.split(/\n,/).join("\n").substr(1); step by step pls.

msg.payload - is a string.
.split(/\n,/) - splits the string on newline and comma match, into an array of lines . String.prototype.split() - JavaScript | MDN

.join("\n") - rejoins the array with a newline delimiter, back to a string now missing all starting commas, aprt from the first. Array.prototype.join() - JavaScript | MDN

.substr(1) - returns the string form character 2 till end,. So removing the first comma of the string. This is depreciated so might be better to use slice() String.prototype.slice() - JavaScript | MDN

now I try it and have problem ..
if use

for (let i = 0; i < msg.payload.length; i++) {
    msg.payload += msg.payload[i];
};
msg.payload.length;

then halt , and must restart orangepi zero.
if use ...let i = 0; i < 64; ... then working .
then I Debug what value return msg.payload.length show this :

7. 5. 2022, 16:46:09node: 9be4ad186aad6f11
msg.payload : undefined
undefined

this msg.payload.length not return any number value .. then halt node-red

PS: E1cid now complete understand what this code do ..
easy solution .. , thanks ..

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