How can I combine two bytes into an double word?

nic_rf

Member
Join Date
Nov 2014
Location
Jonquiere
Posts
2
Hi,

I try to combine 2 Word to a Double Word, but I don't know how to make that in ST.

I'm a guy from C/ASM,so in C, it's would look like this:
Code:
unsigned int i
unsigned char a, b;

a = 42;
b = 24;

i = (a << 8) | b;

So I would like to know if there is a better method in ST or I need to make my own manipulations as my example?

Thank you.
 
There is undoubtedly a way to shift bits on your platform in ST, so it would be very similar to the above. In the platform in front of me right now, B&R, I would just do it in C, but I could also do it in ST like this:

Code:
i:=    SHL(a,8);
i:=    i + b;

If that had issues, I would explicitly convert a and b inline to INT using SINT_TO_INT() on this platform.
 
Use "AT":

Code:
VAR
   word_1 : WORD ;
   word_2 : WORD ;
   d_word AT word_1 : DWORD ;
END_VAR

d_word will contain word_1 and word_2.
AT is very powerful if you know what you do.

edit: using SHL as suggested by Capinwinky will also work.
But AT is more elegant IMHO.

edit again: The C code uses an unsigned integer and chars. That implies that the final result is an integer value. After combining the chars together (with AT or SHL) it will probably not be the integer value you expect.
Do you want "42" and "24" to be "4224" ? Or do you want the bitpattern for 42 and 24 to be appended to each other, or what ?
And you have your data types mixed up. Chars are 8 bit in ST, but 16 bit in C. Unsigned integers are 16 bit in both St and C. So there are no double-words.
I think you must describe what you have to achieve in more detail.
 
Last edited:
Yes, I want to get my position feedback from a Drive (Kinectix 3) with ModBus and it's use 2 address :
ZQmO4rS.png


And on the drive, all position as the range of -2147483647 @+2147483647, so it's double signed integers:
slzSLWi.png


From my Help (Rockwell CCW):
YsAbSoM.png


I think my drive use a DINT for the position feedback and it'll send me 2 Words (MSB and the LSB of DINT) from the ModBus.
 
You don't state which platform you are using but if it is Schneider Unity then there is a function to do this Byte_to_word
 
Hi,

I try to combine 2 Word to a Double Word, but I don't know how to make that in ST.

I'm a guy from C/ASM,so in C, it's would look like this:
Code:
unsigned int i
unsigned char a, b;
 
a = 42;
b = 24;
 
i = (a << 8) | b;

So I would like to know if there is a better method in ST or I need to make my own manipulations as my example?

Thank you.


Hi

You don't state what platform you use. "ST" has different meanings.
You don't state clearly if you want to combine bytes (8 bit) or words (16 bit).

Anyway: i := a * 256 + b might work on each platform.
i := byte_to_word(a) * 256 + b might work better :)
 
If I understand correctly.
The position data from the drive is a DINT (32-bit signed).
But since Modbus only supports 16-bit data types, the data will arrive as 2 16-bit words.
The bitpattern of the 2 words must be combined to 1 DINT.

This should work:
Code:
VAR
   word_1 : WORD ;
   word_2 : WORD ;
   position AT word_1 : DINT ;
END_VAR

This should also work:
Code:
VAR
   word_1 : WORD ;
   word_2 : WORD ;
   position : DINT ;
END_VAR ;

position := DWORD_TO_DINT((SHL(IN:=WORD_TO_DWORD(word_1), N:=16)) OR (WORD_TO_DWORD(word_2))) ;

If the PLC supports AT, then it is the most elegant solution.
 

Similar Topics

Hi; I have installed Weintek HMIs at different machines. I have configured data logging and HMI logs the data in a file having extension...
Replies
0
Views
1,287
Hi; I have installed Weintek HMIs at different machines. I have configured data logging and HMI logs the data in a file having extension...
Replies
0
Views
1,189
I am getting data from a modbus device and I need to combine 2 16 bit N registers in to 1 32bit L register. I have tried a CPW N242:2 -> L245:0...
Replies
5
Views
2,128
Hello Everyone, I am working on CCW 9.0 software with PLC Micro820. I am communicating with Energy meter over Modbus RTU. I...
Replies
3
Views
3,988
How do es a person combine two words of data into one. N7:0 and N7:1, High word and Low word, 1234 and 5678 and get 12345678.
Replies
7
Views
2,243
Back
Top Bottom