I working in my device and need to decode a array byte to ASCII stream, like you can see below:
I using let param1 = msg.payload[28], for example to get a byte.
I need to create a variable that will have within the word derived from the byte sequence of the buffer.
in my PIC above, as example, the array bytes becomes in the word RWL020.
0x52, 0x57, 0x4c, 0x30, 0x32, 0x30 => RWL020
Hi Andrei,
thanks.
now is working well to get one byte.
my code using buffer component:
let stream = msg.payload[32];
msg.payload = String.fromCharCode(stream);
return msg;
I would like to create a word, as example RWL020, from the specific buffer positions.I have a byte that content the byte stream length, Would you can advise me the best way to do it?
The only point is that you need to know the start and the end for the extraction. Only knowing how many bytes to extract from the buffer won't be useful, right?
I amended Paul´s solution to include your latest requirement. You can test code below inside a function node. Use different values for start and end for the testing. I am assuming you will input in the function a buffer inside msg.payload
Hi All, I saw this post and was wondering if you could help me build a function node that will convert a number in decimal format to its ASCII equivalent? I think it has something to do with "charCodeAt" but I am not sure.
I thought I would revive this topic rather than starting a new one. This topic has a number of similarities to the problem I am trying to solve, so I hope I can find a solution here. In a nutshell, I am querying a solar inverter via TCP and receiving a hex output.
Here's my issue... I have inserted the function provided by zenofmud and am receiving an ascii like output. Here's what zenofmud provided:
After inserting zenofmud code into a function I received the following via debug:-
{01;FB;29|64:KDY=5;KT0=52DC;PAC=24A|08E8}
Only issue, it seems to me some of the numbers are still in hex? These are the ones I am concerned about... KT0=52DC and PAC=24A which I am led to beleive should be numbers. Here's a post I used for reference. Domoticz Forum Post
So my question... is there a further function I can use to convert the KTO and PAC output to a number? Or is the conversion from hex to ascii correct?
None of the values you see are strictly Numbers, they are characters representing digits. For example the "01" on the front is actually two characters, "0" and "1". The string "52DC" presumably represents the hexadecimal value 0x52DC in exactly the same way as the "01" represents the decimal number 1 (presumable). It is just that one is in base 16 and one is in base 10.
So if you want any of them as Numbers you are first going to have to split up the string and then convert them to numbers. Whether you actually need to do that depends on what you want to do with the data, so if you give us some more information on that it will be easier to suggest the best route to take.
Thank you for the reply. Let me step back and give you a bigger picture of what I am trying to achieve.
As you know from my previous post I have a SolarMax Inverter, which right now utilises software called MaxTalk to display the inverter statistics. The MaxTalk software connects to the inverter via an ethernet network TCP connection.
The MaxTalk is an isolated solution and will not as far as I am aware interface with either Mosquitto MQTT or Node-Red, therefore I've chosen to try and connect directly to the inverter from Node-Red. As per my previous post I have successfully connected to the inverter from node-red but not sure the data is in the correct format?
Once I have the data in an understandable format I want to 'massage' into a format that can be shared with the Blynk application, which I currently use with other data I collect through Mosquitto and Node-Red. The interface with Blynk is working great, so the addition of the inverter data would be great.
Hope this helps explain the situation and my intent. If there's any additional information you require please let me know. I really appreciate any help/advice from the members of this forum.
What format do you want it in? Take the example data you posted and show us what you would like to get out. Presumably you want a javascript object in msg.payload, but what exactly do you want the example you posted to be converted to?
I am expecting Node-Red to send the following parameters to Blynk, that being the KDY and KT0 parameters, which I expect to be whole numbers... such as...
KDY = energy today (kWh) so a whole number representing the kilowatt hours, which 24A is not correct because it is not a whole number?
Likewise...
KT0 = energy total (kWh) I am expecting a number representing the kilowatt hours, which 52DC is not a whole number?
Here are several outputs from the debug in NodeRed...
You may notice that KT0 and KDY are sometimes numbers, and other times numbers and letters?
So, in a nutshell, I am expecting the output from the inverter to be 'massaged' somehow so it is an actual number, and not a combination of numbers and letters.
This LINK here shows how the inverter protocol was reverse engineered and provides a list of parameters (the output is shown as numbers... so not sure why I am seeing letters and numbers?)
The link above also provides a LINK to a PERL Script, but unfortunately I am not a Perl programmer, so not sure what is being done to the data from the inverter to change the output to pure numbers. I suspect a Perl programmer would probably solve the mystery easily.
From the link above they provide examples... which shows numbers without letters:-
Energy today [Wh] (KDY): 12900
Energy total [kWh] (KT0): 6356
Well it seems the first thing you need to do is to find out what the values like 52DC and 24A mean. To find that out you will presumably have to go back to the source of the data. Perhaps they are hexadecimal numbers, in which case are they just hexadecimal integers or what?
Once you know that then you can go about splitting the string up, probably by writing some javascript in a function node. You can start by using the javascript split() function to split it up on the semicolons.
I have been doing some digging and a generous gentleman George at Ardexa gave me the following python code...
# If its a voltage reading or frequency, divide by 10 to get Volts
# Same for "energy today"
elif key in ('UL2', 'UL1', 'UL3', 'KDY', 'UD01', 'UD02', 'UD03'):
value_int = int(value, 16)
value_fl = float(value_int)/10.0
retval = str(value_fl)
So it would appear from my limited knowledge the value needs to be converted from an integer to a float value, or multiplied by 10? Unfortunately I am no python coder, so perhaps someone on here is skilled in that area and can tell me what is happening in the code?
Your example has KDY=6C; KT0=52F3 Those are hexadecimal numbers which are the equivalent of 108 and 21235 in decimal. If those are 10 times the required answer then that would give 10.8 and 2123.5. Are those the correct values? I notice that KT0 is not mentioned in the code snippet you posted.