network_monitor.py 2.96 KB
Newer Older
Javier Ortega's avatar
Javier Ortega committed
1
2
3
4
5
6
#! /usr/bin/env python
########################################################################
#                         The MIT License (MIT)                        #
#   Copyright (c) 2018 WHITEWALL ENERGY AND RENEWABLE SOLUTIONS S.L.   #
########################################################################

7
8
9
10
11
12
"""
Example to read the parameters of two transducers in real time

Device:
Digital transducers
Model 1: CE-AD11B-32ES5
Manuel Navarrete's avatar
Manuel Navarrete committed
13
http://www.ce-transducer.com/product/details/185/103/details
14
15
16
17
18
19
20
21
22
23
24
25
26
Model 2: CE-AD11B-32GS4
http://www.ce-transducer.com/product/details/185/108/details

TX_SLAVE1
Addres of slave 1:              01h
Function code:                  03h
Addres of the first register:   0010h
Quanty of registers:            0006h

TX_SLAVE2
Addres of slave 2:              02h
Function code:                  03h
Addres of the first register:   0010h
Manuel Navarrete's avatar
Manuel Navarrete committed
27
Quanty of registers:            0004h
28
29
30
31
32

RX_ COMMON
Addres of slave:                1  Byte
Function code:                  1  Byte
Byte count:                     1  Byte
Manuel Navarrete's avatar
Manuel Navarrete committed
33
Value                           8  Bytes
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
CRC:                            2  Bytes
"""

__author__ = 'Manuel Navarrete'
__copyright__ = 'Copyright (C) 2017'
__license__ = 'MIT (expat) License'
__version__ = '0.1'
__email__ = 'manuel.navarrete@whitewallenergy.com'

import struct
import time
import sys
import os

module_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, module_path)
Javier Ortega's avatar
Javier Ortega committed
50
51

from ivy_gw import modbus
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

def get_values(rx):
    """
    Get the values of the received frame

    : pararm rx: Received frame
    : type tx: Buffer of bytes

    : return result: Values of the frame
    : rtype: Array

    """
    result = []
    i = len(rx)-5
    j = 3
    result.append(struct.unpack('<B',rx[0])[0])

    while (i>0):
        result.append(struct.unpack('<H',rx[j:j+2])[0])
        j = j+2
        i = i-2

    return result

def print_values(result):
    """
    Print the values

    : pararm result: values of the transducer
    : type result: array
Manuel Navarrete's avatar
Manuel Navarrete committed
82

83
84
    """
    print '------- MEASUREMENT-- -----'
Manuel Navarrete's avatar
Manuel Navarrete committed
85
86
87
88
89
    print 'Sensor    ', result[0]
    print "Voltage : "+str(result[1])+"V"
    print "Current : "+str(result[2])+"A"
    print "Power   : "+str(result[3])+"W"
    print "Active power: "+str(result[4])+"W"
90
91
92
93
94
95
96
97
    print '---------------------------'

if __name__ == '__main__':
    m = modbus.Modbus()
    address_sensor = [1,2]
    txs = []

    for i in range(0, len(address_sensor)):
Manuel Navarrete's avatar
Manuel Navarrete committed
98
        tx = struct.pack('B', address_sensor[i]) + '\x03\x00\x10\x00\x04'
99
100
101
102
103
104
105
106
107
108
109
110
        tx += m.get_crc(tx)
        txs.append(tx)

    while True:
        for i in range(0, len(txs)):
            rx = m.raw(txs[i])

            if len(rx) == 17:
                print_values(get_values(rx))
            elif len (rx)>0:
                print "Error en el sensor"+str(address_sensor[i])+": Trama erronea"
                print list (rx)
Manuel Navarrete's avatar
Manuel Navarrete committed
111
            else:
112
113
114
                print "Error en el sensor"+str(address_sensor[i])+": Trama vacia"

        time.sleep(0.5)