var regex_tx = /(\d*)h(\d*)m(\d*)h/;
const m = msg.payload.match(regex_tx);
var res = [];
for (var n = 1; n < m.length; n++) {
res.push(Number(m[n]));
}
msg.payload = res
return msg;
Thank you for you answer. The problem is, i can receive negative ascii value like "-2", and i understand that only parseFloat() can do this convertion.
I add that i've tried also with parseInt() and Number(), it return me the same NaN at the same position
var o;
var raw = {};
var ascii = {};
o = msg.payload;
/* test if there is valid raw data */
if(o[0] === 2) //is a conf rawdata
{
raw.topic = "RAM";
raw.enabled = false; //Disable button
raw.payload = msg.payload;
return [raw,null];
}
else if(o[0] < 1 || 2 < o[0]) //is a ascii data ?
{
ascii.topic = "RAM";
ascii.enabled = false; //Disable button
ascii.payload = msg.payload;
//splited = String(o);
var regex_tx = /(\d*)h(\d*)m(\d*)h/;
const m = msg.payload.match(regex_tx);
var res = [];
for (var n = 1; n < m.length; n++)
{
res.push(Number(m));
}
msg.payload = res
return [null,msg];
}
var o;
var raw = {};
var ascii = {};
o = msg.payload;
/* test if there is valid raw data */
if(o[0] === 2) //is a conf rawdata
{
raw.topic = "RAM";
raw.enabled = false; //Disable button
raw.payload = msg.payload;
return [raw,null];
}
else if(o[0] < 1 || 2 < o[0]) //is a ascii data ?
{
ascii.topic = "RAM";
ascii.enabled = false; //Disable button
ascii.payload = msg.payload;
var regex_tx = /(\d*)h(\d*)m(\d*)h/;
var SomeString = String(o);
const m = SomeString.match(regex_tx);
var res = [];
for (var n = 1; n < m.length; n++)
{
res.push(Number(m[n]));
}
msg.payload = res
return [null,msg];
}
Assuming that it is that line that generating the error then it means that m is null.
Read the node-red docs page on Writing Functions and it will tell you how you can use node-warn to display values from within your function. You can add node.warn() statements at appropriate points to see what is going on.
Something with your string that you are trying to "match" is wrong, so there is no match and the array m is null. So output the string before the match and see what's wrong
Yesterday i have undersatnd the problem, thanks Simon01011 to shown me the way.
My electronics card send some data to red node, some time in ASCII or RAW data. I have designed a "terminal" to display commands sent or received with a text node. But, spaces characters sent by my IoT are ignored, because those value is 0x00 (not really a space char).
So by looking the input data, i've seen that IoT sent, for example if an ascii number 1 to send (has not hundred or ten), 001. Or another like -1 will viewed in debug node as - 1.
So, Number() can't process this last one and return un NaN. Same with parseFloat().
I keep preciously you code but it's more hard for me. So i have retryed my first code above, i have modified my firmware of my IoT and deleted excess 0x00 chars, and everythings is ok.