YGC-FS wind speed sensor data: HEX or decimal?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

The YGC-FS wind speed sensor sends to user (via Modbus) the data in Hex format as msb and lsb bytes.
Example: lsb_byte = 26

To get the wind speed I have to convert 26 (in hex) to decimal (= 38) and divide it by 10 (result in m/sec).
I have found on internet some functions in C to convert the hex to decimal by using string.

But when I have tried the next:
byte = 0x26
wind_speed (as integer) = byte
wind_speed = 38.

Question 1
Does Flowcode accept the byte value as decimal, hex or binary without any problems?
Does Flowcode convert the hex to decimal automatically by shifting the byte in integer?


Question 2
How can I add 0x to the byte value 26?
If Flowcode accept the byte value as 0x26 than how can I add 0x to this value? Do I have to use the string?
Thanks.
Last edited by viktor_au on Fri Jan 24, 2020 9:23 pm, edited 1 time in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

The byte value is the same - 0x26 = 38 which are just the human readable forms of the underlying value.

So yes you can do x = 0x26 + 10 or x = 38 + 0x0a and the results will be as expected.

Martin

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

Do I understand correctly:
- Flowcode does change the hex or decimal to binary first and do the calculations second.

I still cannot figure out how to add 0x to byte in decimal as 26.
Last edited by viktor_au on Fri Jan 24, 2020 9:23 pm, edited 1 time in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

Yes. The values 0x26 or 38 are just representations of the number in base 16 or base 10. The number is held as 8,16 or 32 bits binary (base 2) and just converted to a readable form when displayed or converted to a string. Numbers are also converted to the machine form by the compiler...

What number do you want to add? 0x is just the prefix for a hex number (not a valid number) - you'd have, say, 0x01 (1) .. 0xFF (255) for an 8 bit value (byte or char)

Martin

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

Thank you Martin.
No number to add.
The Wind sensor presents the number in hex but in the range of 0 to 9 ( if I understood correctly).
To simplify the calculations I want to use the hex format.
If I use the modbus data as 26 I have to change this hex 26 to decimal.
If I use the modbus data as 0x26 I have the decimal as 38 without any extra code.
-----------
I am not sure what to do.
If I can add 0x to 26 I will have 0x26 (as an example) and can work with decimal 38 as Flowcode does a lot of work for me.

Otherwise I have to split 26 into two nibbles and do more work by converting nibbles to decimals (If I understood the procedure well...)
Last edited by viktor_au on Fri Jan 24, 2020 9:23 pm, edited 1 time in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

Need to check what the sensor is returning.

It is probably an 8 or 16 but number - and already in av format you can use. Ifv it is sending a string (unlikely) them you'll need to convert it to a number (a string it's a human form if the number using ascii )

The 0x is just a convention that tells the compiler a hex number follows...you don't need to add it - it would be a string manipulation if you did. - unless you want it for display purposes.

Martin

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

Re: Byte in HEX format

Post by viktor_au »

Sensor does return two bytes or one integer in hex.
I have checked the manual example.
In the manual example the sensor returns 00 26 in hex of 38 in decimal (the online converter does the same).
I am not sure how to convert the hex byte as 26 in flowcode to decimal.
However if I use 0x26 as the byte value and assign this value to the integer the flowcode does convert the hex to decimal 38.
----------
I think to convert 26 hex to decimal I have to index the 8 bits by using string or >>4 or some other way.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

If it returns 26 you don't need to do anything - it is the same as 38 decimal... Just use the value - and all will be well. You'll need to move the MSB to the upper 8 bits if it's 16 bits. This is just a human readable representation (in the manual too) of the number - it gets converted to this format when you display the number.

It will only make a difference if it returns a hex string (or BCD or other format (or a hex digit per nibble but this is very unlikely)) - they are just using hex as a representation of the underlying number.

Can you give us a pointer to the datasheet?

Martin

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

I understand you Martin.
Hex 26 = Dec 38.
---
Do you know how to convert hex byte to decimal byte in FC8?
Attachments
Manual.pdf
(307.9 KiB) Downloaded 212 times
Last edited by viktor_au on Fri Jan 24, 2020 9:24 pm, edited 1 time in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

There is nothing to convert! Hex(26) is exactly the same as decimal(38) - no conversion necessary...

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

Re: Byte in HEX format

Post by viktor_au »

I agree with you Martin:
Hex(26) is exactly the same as decimal(38) - no conversion necessary...

However I have to repeat my question:
Question 2
How can I add 0x to the byte value 26?


The reason I have asked this question is that Modbus sends msb as 26. Not as 0x26.
Do you see the difference?
0x26 hex = 38 decimal
But...
26 != 38 decimal.

So. What is 26?
The manual says that it is in hex format.
Does it looks like it is hex format?
The hard to read manual says that 00 26 is in hex.
I checked the data from the wind sensor and I saw only numbers (like in decimal).
Until I calibrate the sensor and compare the results with some known anemometer I cannot say what is 0026.

That is why I am asking you ( as a valued contributor) what do I have to do to present this 26 as a hex value without changing the 26.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

Hi Viktor,

I still think this is a misunderstanding as to what the sensor sends - it looks to me like it sends a 16 bit number as msb then lsb. (So ws = (b1 << 8 ) + b2) where b1 is first data byte read and b2 second.
Try printing the actual value received using SendNumber(ws) and SendString (numbertohex$(ws)) (display macro might vary depending on how you're outputting the data)

Adding a 0x to the front won't convert it to a hex number - I can show you how to do it but it won't do what you want - again this is just a convention for display or input of a number.

Try printing a few values - I might be misinterpreting the manual here - can you show me the code you have so far too?

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

To give an example from the manual - first I read (and it is 2:40am here :( ) is baud rate is returned as 25 80 = 9600 baud

Which is indeed true - the sensor returns a 16 bit binary value which you can display as 25 80, 9600 or 00101001 1000000 - but the only conversion needed is when you display the value to the user. (IE when you convert the number to an output string..)

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

Thank you Martin for helping.

Re: ...this is a misunderstanding as to what the sensor sends - it looks to me like it sends a 16 bit number as msb then lsb. (So ws = (b1 << 8 ) + b2) where b1 is first data byte read and b2 second.

You are right.
Sensor does send the data as one 'word': msb and lsb. But 80% of the time during testing the msb byte is 00 and only lsb byte does the work by sending numbers from 0 to ? (I didn't do a high speed test).
The lsb byte shows low to very high wind speed values.
I can use this lsb byte data (as 26 only) or I can use:
wind_speed_new = (00 << 8 ) + 26
the result is the same: 26.

Please have a look at the situation:

- Chinese manual has (probably) a lot of mistakes created during the translation.
- Chinese manual asks user to read the modbus data as two bytes in the hex format.
Or?
- Is this data in the decimal?

Wind vane test results
Today I have connected the Wind vane sensor (as Node N2). Instead of using the flowcode function GetResponseByte I used the function: GetResponseInt (by using index 4). This data was saved to the integer and showed the result as: 0 to 360 degrees.
After that I used the function GetResponseByte with saving two bytes to integer as:
wind_angle_new = (msb << 8 ) + lsb
The result was 0 to 255 degrees.
Looks like the problem is in the way I read the data bytes (or something else?).

Wind speed test with GetResponseInt function
The result is close but different (as above).
I am waiting from Ebay the reference digitatal anemometer to find out what readings are correct (but looks like the GetResponseInt function does a job).

Conclusion

1.As the test of wind vane showed the 0-360deg result I will use this function GetResponseInt now and test the sensors with this function.
2. I will stop paying attention to doc/manual info with the words about the hex data.
Will see what the result of testing the Chinese wind speed sensor with the reference anemometer.
Thanks Martin.
Last edited by viktor_au on Fri Jan 24, 2020 9:24 pm, edited 1 time in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Byte in HEX format

Post by mnf »

The lsb byte shows low to very high wind speed values.
I can use this lsb byte data (as 26 only) or I can use:
wind_speed_new = (00 << 8 ) + 26
the result is the same: 26.
I'd create a macro to read 16 bit value - that can work with wind speed /baud rate etc - 0 * 256 does (of course) = 0 but you don't want things to fail when things get windy (I live in Shetland - so 100mph (44m/s+) winds are (sadly) not unusual)

Lost in translation - the values returned are 16 bit values.. Can we agree that they are binary? They can be base 10 or 16 when you need the user to input them or output data to them... This requires some calculations (and probably conversion from/or to ASCII characters). Flowcode has plenty of helper routines such as NumberToHex$() - but generally you only want to convert numbers to these formats for output (you can perform math (addition etc) on string values - but this is not a native function to the MCU - so expect to do more work. A lot more work!)
The anemometer seems to return a current that is proportional to speed (or a string) - to correctly send it messages you'll also need to implement a routine to calculate the CRC (and the instructions are a little 'opaque') - as the manual says 'Festival'!

Note that it is easy to convert hex to binary - each nibble (4 bits) represents one digit (0..F) The MCU 'sees' the binary value - but for operator ease - it's easier to visualise as a 4 digit hex number (4 characters compared to 16)
Looks like the problem is in the way I read the data bytes (or something else?).
- Not sure - Getting an int should 'mirror' the code above (some devices might return LSB MSB rather than MSB LSB) Can you post your code (or PM me if you prefer)

Have you tried the ASCII data transfer which seems to return "WS=3.0m/s" or similar? (Still waiting for anemometer?)

Can I point you at https://en.wikipedia.org/wiki/Computer_number_format

Martin

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

I will update this post a few weeks later.

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

Re: YGC-FS wind speed sensor data: HEX or decimal?

Post by viktor_au »

I need to do more work.
For now I stopped at:
- Wind directory sensor: display the result of FC8 macro GetResponseInt as an integer (0-360).
- Wind speed sensor: show the converted to string integer from the FC8 macro GetResponseInt.
Note. When the Modbus master requests the data from both sensors at a very short intervals the system is not stable.
I used the 2 seconds interval between the master requests.

Will update later when I install the both sensors on the roof.

Post Reply