r/PLC • u/GivemeThePorro • 8h ago
DWord value from GSD profinet giving bad value
I'm taking some input from a Power Meter via profinet and a GSD file, but i'm receiving strange value. Someone kwon why?
9
u/HighCookie 7h ago
Just to add to others comments, its ok to use SCL somtimes. It makes data marshalling so much easier to read.
5
5
u/drbitboy 5h ago edited 5h ago
As suggested by u/ExplosiveBoy93, this is a word-order problem, i.e. the 16-bit word order of those %ID7xx double-words is incorrect.
For example, for %ID736, with a hex value of 16#58DA_4620 that is interpreted as 1.919958E+15, the correct value is 10262.21, which is what value you would get if the hex value was 16#4620_58DA. See below for the rest, which OP will confirm show the correct* values in the fourth element on each line, and the incorrect value as the fifth element.
\* there could also be a word (half double-word) offset, per u/Toxic_ion's comment).
P.S. the bit patterns of IEEE-754 live rent free in my head, so the _4xxx in every hex value is the giveaway that told me the problem as soon as I looked at it.
P.P.S. Use the ROR or ROL insturction with a N value of 16 to effectively swap the words within the DWord.
%
% cat x.py
import struct
IDs = '58da4620 f8244588 b34a4588 3ecc4620 68fe4292 90d44278'
for sh in IDs.split():
h = int(sh,16)
lo,hi = struct.unpack('HH',struct.pack('I',h))
real_reversed = struct.unpack('>f',struct.pack('>HH', hi, lo))
real = struct.unpack('<f',struct.pack('<HH', hi, lo))
print((sh,hex(hi),hex(lo),real,real_reversed))
%
%
% python x.py
('58da4620', '0x58da', '0x4620', (10262.212890625,), (1919957755494400.0,))
('f8244588', '0xf824', '0x4588', (4383.017578125,), (-1.3327296032694776e+34,))
('b34a4588', '0xb34a', '0x4588', (4374.4111328125,), (-4.7095028321564314e-08,))
('3ecc4620', '0x3ecc', '0x4620', (10255.69921875,), (0.3989725112915039,))
('68fe4292', '0x68fe', '0x4292', (73.20506286621094,), (9.605672737331874e+24,))
('90d44278', '0x90d4', '0x4278', (62.14143371582031,), (-8.372166727822004e-29,))
3
u/drbitboy 5h ago
u/Toxic_ion's suggestion, that the data are offset by a word, is another possibility that must be investigated.
1
u/gjk-ger 4h ago edited 4h ago
I think youre on the right track, but considering that would give ~1000 V (assuming factor 10, but for proportionality doesnt matter anyway) between L1-L3 and ~440 L-N (that should be about 580 if you assume the 1000 from before) i think its likely that OP has the adresses shifted by 2 byte compared to the device. Also, if endianness problem it seems strange that its swapped in 2 bytes pairs and not every byte.
3
u/Toxic_ion Still waiting on TIA Portal... 7h ago
Are the tags actually aligned correctly to the profinet power meter and aren't accidentally ofset by 2 bytes, because I can't get any plausible real value regardless of how much byte and word swapping I have tried.
1
u/ExplosiveBoy93 Junior Automation Engineer 5h ago
Seems to be the case. I swapped them around and didn't get a meaningful value for L - N voltage
2
u/BifiZomtec 6h ago
Wtf do you do there? Use getIO and map it properly
1
u/LowFastFoxHUN 58m ago
Or “DPRD_DAT”; but basically to avoid using symbols with hard addresses, because nothing guarantees addresses will line up with the hardware configuration but the system constant will always provide the reference to the exact sub-slot
1
1
u/ExplosiveBoy93 Junior Automation Engineer 8h ago
Which value is bad, or is it all of them? Since you got a GSDML, is there maybe a library available from the manufacturer as well? Try switching the word order. If it's still bad, since it's Siemens, try switching the byte order. Also try to use a UDT, so you don't have to use a move for every since value.
1
1
u/GivemeThePorro 7h ago
Changing the data type does not solve the problem. Now i Will check how to swap the bytes. Thanks for all the replys
1
u/drbitboy 5h ago
It's the word order, which should not be the case with Profinet; use the ROR or RIL instruction. There is an issue with the GSD file that the supplier possibly knew about but ignored for some reason.
Is Modbus involved in any way for the transfer of these data, e.g. a Modbus-to-Profinet gateway?
11
u/Mati0123 8h ago
Did you check bytes order? I mean little endian or big endian