Steve-Mcl / node-red-contrib-buffer-parser

A node-red node to convert values in a buffer, integer array or hex string into many different data type(s). Supports Big/Little Endian, BCD, byte swapping and much more.
MIT License
24 stars 8 forks source link

Swap issue with datatype float #10

Closed chiarafaes closed 2 years ago

chiarafaes commented 2 years ago

Hello,

We are using your node-red block "buffer-parser" and noticed something weird.

When we use your parser on integers; we get results which are correct. But whenever we use the parser for a float, the swapping doesn't return the values we expect ...

We need a value stored in 2 registers, each register is 1 word and the manual of the device says = <4 bytes>

image

image

Buffer we use to test: data: 0: 0 1: 0 2: 0x7f7 3: 0x42bb

buffer: 0: 0x0 1: 0x0 2: 0x0 3: 0x0 4: 0xf7 5: 0x7 6: 0xbb 7: 0x42

image

we expect 0x42bb07f7

but we get 0xf707bb42

so we started to flay around with your block and noticed the following:

type Swap Result floatbe none 0x07f742bb floatbe swap16 0xf707bb42 floatle none 0xbb42f707
floatle swap16 0x42bb07f7

which means we should use the parser with floatle and a swap of 16 to get the correct value while we expected to use a floatbe and no swap

However, The other types like UINT32BE are correct without setting a swap or setting the type to little endian

Steve-Mcl commented 2 years ago

Hi,

Often when issues like this arise, it is due to the implementation by the device manufacturer and how they decide to arrange values in the 16 bit registers.

Under the hood, Buffer-Parser uses the well defined nodejs Buffer and calls its well tested functions based on your choices so I am confident it is doing the right thing. Whether that means it will compute the correct output for an IEEE float as defined by your device is another matter.

However, I wont close this issue until I have double checked the code.

In the meantime, could you tell me...

Also, can you provide a demo flow with data captured from your device (use the "copy value" button on the debug output & paste as text not picture) in an inject node (so I can test it without needing access to your device)

Cheers.

On Wed, 15 Dec 2021 at 10:18, chiarafaes @.***> wrote:

Hello,

We are using your node-red block "buffer-parser" and noticed something weird.

When we use your parser on integers; we get results which are correct. But whenever we use the parser for a float, the swapping doesn't return the values we expect ...

We need a value stored in 2 registers, each register is 1 word and the manual of the device says = <4 bytes>

[image: image] https://user-images.githubusercontent.com/26067234/146168104-81b1e9ef-5781-4e18-95fe-7acf103adaae.png

[image: image] https://user-images.githubusercontent.com/26067234/146168051-8bdb8131-1a3b-421f-ad68-a3411ed53bce.png

Buffer we use to test: data: 0: 0 1: 0

2: 0x7f7 3: 0x42bb

buffer: 0: 0x0 1: 0x0 2: 0x0 3: 0x0

4: 0xf7 5: 0x7 6: 0xbb 7: 0x42

[image: image] https://user-images.githubusercontent.com/26067234/146164845-c39bdf06-6951-412c-be51-b3441d8d5dee.png

we expect 0x42bb07f7

but we get 0xf707bb42

so we started to flay around with your block and noticed the following:

type Swap Result floatbe none 0x07f742bb floatbe swap16 0xf707bb42 floatle none 0xbb42f707 floatle swap16 0x42bb07f7

which means we should use the parser with floatle and a swap of 16 to get the correct value while we expected to use a floatbe and no swap

However, The other types like UINT32BE are correct without setting a swap or setting the type to little endian

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Steve-Mcl/node-red-contrib-buffer-parser/issues/10, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKRPUGMWXYI3FN5HKOT64NDURBTO5ANCNFSM5KDHIXQA .

chiarafaes commented 2 years ago

Hi,

First; Thank you for the quick response!

We expect something between 93 and 96

This is what we get out of the registers : [0,0,2039,17083,0,0,16703,17099]

or in hex: [0x0,0x0,0x7f7,0x42bb,0x0,0x0,0x413f,0x42cb]

The value we need is our voltage and is made by the 3th and 4th value. So in order to get the value we need, we should use 2039 and 17083. which are 0x07f7 and 0x42bb in hex.

following the manual of the device, we need to swap 2039 and 17083 so we get 0x42bb first, followed by 0x07f7 those 2 , in this order should give us a value between 93 and 96

I've recreated the scenario in the flow below:

[{"id":"81a7c0b8.b877","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2105ecf4.1237e4","type":"inject","z":"81a7c0b8.b877","name":"Insert values read from register","props":[{"p":"payload"},{"p":"topic","v":"[0,0,2039,17083,0,0,57772,17097]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":440,"wires":[["993ec218.86e45","4bb8c2bb.b8672c","7562130c.a2cf9c"]]},{"id":"4bb8c2bb.b8672c","type":"buffer-parser","z":"81a7c0b8.b877","name":"Value it should be","data":"topic","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"Value It Should be","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","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":770,"y":500,"wires":[["5a9e6fa.756279"]]},{"id":"993ec218.86e45","type":"debug","z":"81a7c0b8.b877","name":"Array Of Values Entering parser","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":440,"wires":[]},{"id":"5a9e6fa.756279","type":"debug","z":"81a7c0b8.b877","name":"Value we need to get from parser","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1220,"y":500,"wires":[]},{"id":"7562130c.a2cf9c","type":"buffer-parser","z":"81a7c0b8.b877","name":"Settings it should be ","data":"topic","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatbe","name":"SettingWethinkItshouldbe","offset":4,"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":780,"y":820,"wires":[["37cf7f8b.3fdb9"]]},{"id":"37cf7f8b.3fdb9","type":"debug","z":"81a7c0b8.b877","name":"Value we get when we use the datatype we think we need","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1270,"y":820,"wires":[]},{"id":"49bd3ff1.48d2a","type":"comment","z":"81a7c0b8.b877","name":"We read 8 registers from the device each register is a word. The values we get from the device are pasted in the insert statement","info":"","x":510,"y":340,"wires":[]},{"id":"4f23591b.a5bb48","type":"comment","z":"81a7c0b8.b877","name":"this parser is set to float litlle endian with a swap16. Doing this, we get the value we should get","info":"","x":1000,"y":540,"wires":[]},{"id":"350872e3.891dee","type":"comment","z":"81a7c0b8.b877","name":"this parser is set to float big endian without a swap. and gives the wrong value","info":"","x":950,"y":860,"wires":[]},{"id":"603ebd1d.87b584","type":"comment","z":"81a7c0b8.b877","name":"We think we should use float big endian without swap16 because all our other values are big endian ass well (integers) and are correctly parsed","info":"","x":620,"y":1000,"wires":[]}]

thanks in advance!

chiarafaes commented 2 years ago

Hi Steve,

we found the mistake! You are correct, the mistake is made in the registers of our device.

they use high - low for 90% of the registers, and swapped them for the other 10%. This is why we need to swap in the parser as well!

We apologize for the usage of your time! Thank you for the help

kind regards

Steve-Mcl commented 2 years ago

Glad it is sorted.