29
29
__license__ = "BSD-3"
30
30
__version__ = "1.0.0-alpha"
31
31
32
+ ###############################################################
33
+
34
+ # UDP connection was implemented by Yuri Poledna under the supervision of
35
+
36
+ # Prof. Eduardo Parente in R&D project supported by Brazilian electric utility
37
+
38
+ # Companhia Paranaense de Energia – COPEL.
39
+ ###############################################################
32
40
33
41
class CommonFrame (metaclass = ABCMeta ):
34
42
"""
@@ -75,15 +83,17 @@ def __init__(self, frame_type, pmu_id_code, soc=None, frasec=None, version=1):
75
83
:param int version:
76
84
:return:
77
85
"""
78
-
86
+
79
87
self .set_frame_type (frame_type )
80
88
self .set_version (version )
81
89
self .set_id_code (pmu_id_code )
82
-
90
+
83
91
if soc or frasec :
84
92
self .set_time (soc , frasec )
85
93
86
94
95
+ def get_receivedData ():
96
+ return self .receivedData
87
97
def set_frame_type (self , frame_type ):
88
98
"""
89
99
### set_frame_type() ###
@@ -133,7 +143,7 @@ def get_frame_type(self):
133
143
134
144
def extract_frame_type (byte_data ):
135
145
"""This method will only return type of the frame. It shall be used for stream splitter
136
- since there is no need to create instance of specific frame which will cause lower performance."""
146
+ since there Phasor 5 Angle(rad): 0.6818265914916992is no need to create instance of specific frame which will cause lower performance."""
137
147
138
148
# Check if frame is valid
139
149
if not CommandFrame ._check_crc (byte_data ):
@@ -226,12 +236,11 @@ def set_time(self, soc=None, frasec=None):
226
236
227
237
t = time () # Get current timestamp
228
238
229
- if soc :
239
+ if soc is not None :
230
240
self .set_soc (soc )
231
241
else :
232
242
self .set_soc (int (t )) # Get current timestamp
233
-
234
- if frasec :
243
+ if frasec is not None :
235
244
if isinstance (frasec , collections .Sequence ):
236
245
self .set_frasec (* frasec )
237
246
else :
@@ -241,7 +250,6 @@ def set_time(self, soc=None, frasec=None):
241
250
# overflow (24 bit number).
242
251
self .set_frasec (int ((((repr ((t % 1 ))).split ("." ))[1 ])[0 :6 ]))
243
252
244
-
245
253
def set_soc (self , soc ):
246
254
"""
247
255
### set_soc() ###
@@ -370,8 +378,6 @@ def set_frasec(self, fr_seconds, leap_dir="+", leap_occ=False, leap_pen=False, t
370
378
frasec |= fr_seconds # Bits 23-0: Fraction of second.
371
379
372
380
self ._frasec = frasec
373
-
374
-
375
381
def get_frasec (self ):
376
382
377
383
return self ._int2frasec (self ._frasec )
@@ -392,7 +398,7 @@ def _int2frasec(frasec_int):
392
398
leap_occ = bool (leap_occ )
393
399
leap_pen = bool (leap_pen )
394
400
395
- fr_seconds = frasec_int & (2 ** 23 - 1 )
401
+ fr_seconds = frasec_int & (2 ** 24 - 1 )
396
402
397
403
return fr_seconds , leap_dir , leap_occ , leap_pen , time_quality
398
404
@@ -592,7 +598,6 @@ def _int2format(data_format):
592
598
def _check_crc (byte_data ):
593
599
594
600
crc_calculated = crc16xmodem (byte_data [0 :- 2 ], 0xffff ).to_bytes (2 , "big" ) # Calculate CRC
595
-
596
601
if byte_data [- 2 :] != crc_calculated :
597
602
return False
598
603
@@ -632,7 +637,6 @@ def convert2bytes(self, byte_message):
632
637
633
638
@abstractmethod
634
639
def convert2frame (byte_data , cfg = None ):
635
-
636
640
convert_method = {
637
641
0 : DataFrame .convert2frame ,
638
642
1 : HeaderFrame .convert2frame ,
@@ -1114,7 +1118,7 @@ def _phunit2int(scale, phasor_type="v"):
1114
1118
* ``scale`` **(int)** - scale factor.
1115
1119
* ``phasor_type`` **(char)** - ``v`` - voltage, ``i`` - current.
1116
1120
Default value: ``v``.
1117
-
1121
+ phasor
1118
1122
**Returns:**
1119
1123
1120
1124
* ``int`` which represents phasor channels conversion factor.
@@ -1847,14 +1851,15 @@ class DataFrame(CommonFrame):
1847
1851
TRIGGER_REASON_WORDS = { code : word for word , code in TRIGGER_REASON .items () }
1848
1852
1849
1853
1850
- def __init__ (self , pmu_id_code , stat , phasors , freq , dfreq , analog , digital , cfg , soc = None , frasec = None ):
1854
+ def __init__ (self , pmu_id_code , stat , phasors , freq , dfreq , analog , digital , cfg , soc = None , frasec = None , receivedData1 = b'0' ):
1851
1855
1852
1856
if not isinstance (cfg , ConfigFrame2 ):
1853
1857
raise FrameError ("CFG should describe current data stream (ConfigurationFrame2)" )
1854
1858
1855
1859
# Common frame for Configuration frame 2 with PMU simulator ID CODE which sends configuration frame.
1856
1860
super ().__init__ ("data" , pmu_id_code , soc , frasec )
1857
-
1861
+
1862
+ self .receivedData = receivedData1
1858
1863
self .cfg = cfg
1859
1864
self .set_stat (stat )
1860
1865
self .set_phasors (phasors )
@@ -1864,6 +1869,8 @@ def __init__(self, pmu_id_code, stat, phasors, freq, dfreq, analog, digital, cfg
1864
1869
self .set_digital (digital )
1865
1870
1866
1871
1872
+ def getReceivedData (self ):
1873
+ return self .receivedData
1867
1874
def set_stat (self , stat ):
1868
1875
1869
1876
if self .cfg ._num_pmu > 1 :
@@ -2037,24 +2044,18 @@ def get_phasors(self, convert2polar=True):
2037
2044
2038
2045
@staticmethod
2039
2046
def _phasor2int (phasor , data_format ):
2040
-
2041
2047
if not isinstance (phasor , tuple ):
2042
2048
raise TypeError ("Provide phasor measurement as tuple. Rectangular - (Re, Im); Polar - (Mg, An)." )
2043
2049
2044
2050
if isinstance (data_format , int ):
2045
2051
data_format = DataFrame ._int2format (data_format )
2046
2052
2047
2053
if data_format [0 ]: # Polar representation
2048
-
2049
2054
if data_format [1 ]: # Floating Point
2050
-
2051
- if not - 3.142 <= phasor [1 ] <= 3.142 :
2052
- raise ValueError ("Angle must be in range -3.14 <= ANGLE <= 3.14" )
2053
-
2055
+ # raise ValueError("Angle must be in range -3.14 <= ANGLE <= 3.14")
2054
2056
mg = pack ("!f" , float (phasor [0 ]))
2055
2057
an = pack ("!f" , float (phasor [1 ]))
2056
2058
measurement = mg + an
2057
-
2058
2059
else : # Polar 16-bit representations
2059
2060
2060
2061
if not 0 <= phasor [0 ] <= 65535 :
@@ -2085,7 +2086,6 @@ def _phasor2int(phasor, data_format):
2085
2086
re = pack ("!h" , phasor [0 ])
2086
2087
im = pack ("!h" , phasor [1 ])
2087
2088
measurement = re + im
2088
-
2089
2089
return int .from_bytes (measurement , "big" , signed = False )
2090
2090
2091
2091
@@ -2141,15 +2141,12 @@ def _freq2int(freq, data_format):
2141
2141
data_format = DataFrame ._int2format (data_format )
2142
2142
2143
2143
if data_format [3 ]: # FREQ/DFREQ floating point
2144
- if not - 32.767 <= freq <= 32.767 :
2145
- raise ValueError ("FREQ must be in range -32.767 <= FREQ <= 32.767." )
2146
-
2144
+ #raise ValueError("FREQ must be in range -32.767 <= FREQ <= 32.767.")
2147
2145
freq = unpack ("!I" , pack ("!f" , float (freq )))[0 ]
2148
2146
else :
2149
2147
if not - 32767 <= freq <= 32767 :
2150
2148
raise ValueError ("FREQ must be 16-bit signed integer. -32767 <= FREQ <= 32767." )
2151
2149
freq = unpack ("!H" , pack ("!h" , freq ))[0 ]
2152
-
2153
2150
return freq
2154
2151
2155
2152
@@ -2368,12 +2365,12 @@ def get_measurements(self):
2368
2365
"phasors" : self .get_phasors (),
2369
2366
"analog" : self .get_analog (),
2370
2367
"digital" : self .get_digital (),
2371
- "frequency" : self .cfg .get_fnom () + self .get_freq () / 1000 ,
2368
+ #"frequency": self.cfg.get_fnom() + self.get_freq() / 1000,
2369
+ "frequency" : self .get_freq (),
2372
2370
"rocof" : self .get_dfreq ()
2373
2371
})
2374
-
2375
2372
data_frame = { "pmu_id" : self ._pmu_id_code ,
2376
- "time" : self .get_soc () + self .get_frasec ()[0 ] / self .cfg .get_time_base (),
2373
+ "time" : ( self .get_soc () + ( self .get_frasec ()[0 ] / self .cfg .get_time_base ()) ),
2377
2374
"measurements" : measurements }
2378
2375
2379
2376
return data_frame
@@ -2414,7 +2411,8 @@ def convert2bytes(self):
2414
2411
2415
2412
@staticmethod
2416
2413
def convert2frame (byte_data , cfg ):
2417
-
2414
+
2415
+
2418
2416
try :
2419
2417
2420
2418
if not CommonFrame ._check_crc (byte_data ):
@@ -2429,7 +2427,6 @@ def convert2frame(byte_data, cfg):
2429
2427
pmu_code = int .from_bytes (byte_data [4 :6 ], byteorder = "big" , signed = False )
2430
2428
soc = int .from_bytes (byte_data [6 :10 ], byteorder = "big" , signed = False )
2431
2429
frasec = CommonFrame ._int2frasec (int .from_bytes (byte_data [10 :14 ], byteorder = "big" , signed = False ))
2432
-
2433
2430
start_byte = 14
2434
2431
2435
2432
if num_pmu > 1 :
@@ -2515,7 +2512,7 @@ def convert2frame(byte_data, cfg):
2515
2512
digital .append (dig )
2516
2513
start_byte += 2
2517
2514
2518
- return DataFrame (pmu_code , stat , phasors , freq , dfreq , analog , digital , cfg , soc , frasec )
2515
+ return DataFrame (pmu_code , stat , phasors , freq , dfreq , analog , digital , cfg , soc , frasec , byte_data )
2519
2516
2520
2517
except Exception as error :
2521
2518
raise FrameError ("Error while creating Data frame: " + str (error ))
0 commit comments