iec-60870 icon indicating copy to clipboard operation
iec-60870 copied to clipboard

Problems with negative values in IeNormalizedValue

Open L4S1L0 opened this issue 7 years ago • 0 comments

In the last days my colleague and I tested the communication between my C#-Application using this library and his C++-Application using lib60870-5 (http://libiec61850.com/libiec61850/). During our tests we got problems with receiving values of type M_ME_NA_1 (normalized value / IeNormalizedValue). Every time he sends me negative values this results in exceptions.

After some debugging I was able to figure out what's the matter. I think there is a bug in IeNormalizedValue (Line 22): Value = reader.ReadByte() | (reader.ReadByte() << 8); The code handles two bytes (16 bit) and calculates an int (32 bit) where only 16 bits are used. The problem is that negative values in 16 bit representation are not handled correct this way - the result of negative 16 bit values is a number > 32767 in 32 bit (caused by the sign at the first bit).

I was able to fix it by replacing the line with the following code using BitConverter to create a short from the two bytes. This short has the correct value and sign an can be casted to int:

byte[] bytes = { reader.ReadByte(), reader.ReadByte()};
Value = BitConverter.ToInt16(bytes,0);

I tested it with QTester104 (https://sourceforge.net/projects/qtester104/) and lib60870-5 and it seems to work so I hope that this is correct. Please check it - hope that my code helps!

L4S1L0 avatar Apr 27 '17 08:04 L4S1L0