Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get "CD AB" order from ByteBuf? #8

Closed
vbnetvbnet opened this issue Dec 10, 2016 · 4 comments
Closed

How to get "CD AB" order from ByteBuf? #8

vbnetvbnet opened this issue Dec 10, 2016 · 4 comments

Comments

@vbnetvbnet
Copy link
Contributor

vbnetvbnet commented Dec 10, 2016

How can I get a "CD AB" ordered float from ByteBuf? Like:
image

I know very little about modbus. Can you please help me? Thanks.

@vbnetvbnet vbnetvbnet changed the title How to get "DC BA" order from ByteBuf? How to get "CD AB" order from ByteBuf? Dec 10, 2016
@kevinherron
Copy link
Contributor

kevinherron commented Dec 10, 2016

I'm not sure what that software would consider the order to be by default (maybe DC BA), but try something like this:

short cd = Short.reverseBytes(buffer.readShort());
short ab = Short.reverseBytes(buffer.readShort());
float f = Float.intBitsToFloat((cd << 16) | ab);

Assuming the buffer contains at least 4 bytes (meaning you read at least 2 registers, because each register is 16-bit).

@vbnetvbnet
Copy link
Contributor Author

vbnetvbnet commented Dec 11, 2016

@kevinherron Thank you for your reply. I'm trying using your code, but I got the value equal to order BA DC rather than CD AB. Is it because of different default order?

@kevinherron
Copy link
Contributor

Maybe this then?

short ab = buffer.readShort();
short cd = buffer.readShort();
float f = Float.intBitsToFloat((cd << 16 | ab));

@vbnetvbnet
Copy link
Contributor Author

It works! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants