forked from david-m-m/SMA-EM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smaem.py
59 lines (49 loc) · 1.84 KB
/
smaem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
*
* by Wenger Florian 2015-11-12
*
* by david-m-m 2019-03-20
*
* this software is released under GNU General Public License, version 2.
* This program is free software;
* you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2 of the License.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
"""
#import socket
import struct
import binascii
import sys
from speedwiredecoder import *
# listen to the Multicast; SMA-Energymeter sends its measurements to 239.12.255.254:9522
#MCAST_GRP = '239.12.255.254'
#MCAST_PORT = 9522
# function to transform HEX to DEC
def hex2dec(s):
"""return the integer value of a hexadecimal string s"""
return int(s, 16)
# clean exit
def abortprogram(signal,frame):
# Housekeeping -> nothing to cleanup
print('STRG + C = end program')
sys.exit(0)
# prepare listen to socket-Multicast
#sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#sock.bind(('', MCAST_PORT))
#mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
#sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
def readem(sock):
smainfo=sock.recv(600)
smainfoasci=binascii.b2a_hex(smainfo)
# split the received message to seperate vars
emparts=decode_speedwire(smainfo)
return emparts