Stefan009

Array of bytes to int, basically.. but..

13 posts in this topic

Hi.

When i read from an rs485-module i get a buntch of bytes in to an array where certain position means something,

I want to pick pos 11..14 and convert those to a integer.

As example, i get "57574848" (shown as decimals in a watch table, hex is: 39 39 30 30).
The thing i want, it to turn these bytes into (in this example) 9900 in an integer variable, i have trying a bunch of stuff such as ArytoString, StringToAry and others i cant remember their names of.
I have a new array of bytes with only the 4 values i do want, but i cannot figure out how to convert those ary bytes to a number that is usable to be and can be handles like a number for different calculations and stuff.

 

Any suggestions where to start off with?

 

The Array:

sHPA[0] = 39   (INT: 9)
sHPA[1] = 39
sHPA[2] = 30 (INT: 0)
sHPA[2] = 30

Share this post


Link to post
Share on other sites

Only decimal digits may be encoded in the bytes?
The result value integer only? SIgned? Unsigned?

Share this post


Link to post
Share on other sites
17 minutes ago, Sergei Troizky said:

Only decimal digits may be encoded in the bytes?
The result value integer only? SIgned? Unsigned?

Hi.

In these 4 positions of the array, the values are only going to be 0-9, unsigned since they can never go negative (other positions tells if its something like x10^5 or x10^-2)

Edited by Stefan009

Share this post


Link to post
Share on other sites

MyInt = 0
MyInt = MyInt * 10 + (sHPA[0] - 48 )
MyInt = MyInt * 10 + (sHPA[1] - 48 )
MyInt = MyInt * 10 + (sHPA[2] - 48 )
MyInt = MyInt * 10 + (sHPA[3] - 48 )

 

or

MyInt =  (sHPA[0] - 48 )
MyInt = MyInt * 10 + (sHPA[1] - 48 )
MyInt = MyInt * 10 + (sHPA[2] - 48 )
MyInt = MyInt * 10 + (sHPA[3] - 48 )

 

48 is decimal value for 0x30

 

Share this post


Link to post
Share on other sites

Get those bytes into a string variable, then do string=>integer conversion.  (Not terribly familiar with Omron, but that's the idea.)

Share this post


Link to post
Share on other sites

exactly... those byte values are ASCII characters. 

1 person likes this

Share this post


Link to post
Share on other sites

I have basically "solved" it like this:

snipsnip.PNG.d396af0c2335fb530bfe642e327

 

But the EXPT gets something wrong.

I am trying to make it so it will divide the number i have by "x 10^-2) with should(?) be 0.01 for the rDive in the lower part in the image, but instead i get some wierd decimals.

The rHP should end up be like 94.02 and not 940199.94. Any ideas?

 

wolphran.thumb.PNG.ac29dbdf6c758d2f5a82e

Share this post


Link to post
Share on other sites

your rDiv is 0.01 which is 1/10^2

so you need to multiply by rDiv, not divide. :-D

...or the rDiv need to be 10^2 instead of 10^-2

Share this post


Link to post
Share on other sites
16 minutes ago, panic mode said:

your rDiv is 0.01 which is 1/10^2

so you need to multiply by rDiv, not divide. :-D

Thanks.

But the rDiv is not 0.01 see this image:

rdiv.PNG.fbdb8affc912b91d5ac3795f902b44d

Share this post


Link to post
Share on other sites

so you start with 9950, and get 99.50... isn't that what you wanted?

Share this post


Link to post
Share on other sites
16 minutes ago, panic mode said:

so you start with 9950, and get 99.50... isn't that what you wanted?

Its close, but i do actually get 99.500008, i expect to get 99.50/ 99.5 and no more decimals.

Edited by Stefan009

Share this post


Link to post
Share on other sites

you need to define what is that you really need, stop moving the goal posts. first you wanted an integer, then floating point, then fixed point.

what are you using value for?

if it is display, then integer is fine. you can specify decimal place by formatting your HMI object

if you are looking at value to control the process, floating point (95.000002) is fine

but floating point values (real type) cannot accurately represent any number.... there is going to be rounding.

so the other option is to keep it in string format...

what is it going to be?

 

Share this post


Link to post
Share on other sites

0.01 looks simple in decimal, but in binary it infinitely repeats.  A floating point register with that value won't be exact because real registers do not repeat infinitely.  Divide by 100.0.  That'll get you closest in the real world.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now