- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
Im in the process to connect one of my programs to a Siemens S7 system. The communication takes place via Winsocket and TCP/IP. The connection is being made and I get data... But they are in the wrong order.
For example, I get an integer(kind=2) for the actual month. I would have expected a 2 for February, but I get 512. In binary it is
00000010 000000 which is 512,
00000000 000010 would be 2
The bytes are swapped, the big - little endian issue.
Is there an easyway to turn them into the right way? I get some real(kind=4) values as well.
Thanks in advance,
Markus
Im in the process to connect one of my programs to a Siemens S7 system. The communication takes place via Winsocket and TCP/IP. The connection is being made and I get data... But they are in the wrong order.
For example, I get an integer(kind=2) for the actual month. I would have expected a 2 for February, but I get 512. In binary it is
00000010 000000 which is 512,
00000000 000010 would be 2
The bytes are swapped, the big - little endian issue.
Is there an easyway to turn them into the right way? I get some real(kind=4) values as well.
Thanks in advance,
Markus
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
After searching the net, I found that the function MVBITS does exactly what I need...
Markus
[bash]CALL IMVBITS( intIn, 8, 8, intOut, 0 ) CALL IMVBITS( intIn, 0, 8, intOut, 8 )[/bash]This is for integer(kind=2). For real(kind=4) you have to do this:
[bash] INTEGER :: i_element INTEGER :: i_element_br !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Transfer 32 bits of realIn to generic 32 bit INTEGER space: i_element = TRANSFER( realIn, 0 ) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Reverse order of 4 bytes in 32 bit INTEGER space: CALL MVBITS( i_element, 24, 8, i_element_br, 0 ) CALL MVBITS( i_element, 16, 8, i_element_br, 8 ) CALL MVBITS( i_element, 8, 8, i_element_br, 16 ) CALL MVBITS( i_element, 0, 8, i_element_br, 24 ) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Transfer reversed order bytes to 32 bit REAL space (realOut): realOut = TRANSFER( i_element_br, 0.0 )[/bash]Thanks to David Stepaniak, he wrote this code!
Markus
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I've seen some C code samples that use masks and shifts, which might be more efficient. But if the speed of the MVBITS is sufficient, then use that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here is a routine I use for rearranging the byte-order in Modbus communications. There is a regrettable lack of standardization in Modbus, so my driver has a flag which can be set to match the byte-ordering convention of various devices. Since the device response is read into a character buffer, the swapping is done bytewise prior to interpretation as a number.
[bash]! byte-order repacking RECURSIVE FUNCTION RealByteOrder (buf1, flags) RESULT (buf2) IMPLICIT NONE INTEGER, INTENT(IN) :: flags CHARACTER(LEN=4), INTENT(IN) :: buf1 CHARACTER(LEN=4) :: buf2 ! rearrange byte-order for REALs; only ! one of these bitflags will be set for ! a given device ! little-endian IF (BTEST(flags, modbus_FPL)) THEN buf2(1:1) = buf1(4:4) buf2(2:2) = buf1(3:3) buf2(3:3) = buf1(2:2) buf2(4:4) = buf1(1:1) ! big-endian byte-swapped ELSE IF (BTEST(flags, modbus_FPBB)) THEN buf2(3:3) = buf1(4:4) buf2(4:4) = buf1(3:3) buf2(1:1) = buf1(2:2) buf2(2:2) = buf1(1:1) ! little-endian byte-swapped ELSE IF (BTEST(flags, modbus_FPLB)) THEN buf2(1:1) = buf1(2:2) buf2(2:2) = buf1(1:1) buf2(3:3) = buf1(4:4) buf2(4:4) = buf1(3:3) ! big-endian (Intel default) ELSE buf2 = buf1 END IF END FUNCTION RealByteOrder [/bash]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting Steve Lionel (Intel)
I've seen some C code samples that use masks and shifts, which might be more efficient. But if the speed of the MVBITS is sufficient, then use that.
I get appr. 200 values every 2 seconds, so I think that the use ofMVBITS should be okay.
Paul, thanks for the code, maybe I will implement it and measure, which method will be faster. But what is the difference between two two byte swapped branches? They do the same but in another order.
Markus

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page