Передача Счетчиков по Modbus TCP

«ПульсХАБ» поддерживает передачу статистики каналов по протоколу Modbus TCP и Modbus RTU.

Подробное описание регистров Modbus содержится в руководстве по эксплуатации «ПульсХАБ».

Принятые показания счетчиков можно передать в базу данных, использовать для on-line мониторинга, управления и т.д.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/perl -w
#
# прием показаний от прибора «ПульсХАБ» по протоколу Modbus TCP
#
# http://tigra-electronic.com/products/pulsehub.htm
# 06/05/2014
 
use strict;
use warnings;
use Protocol::Modbus;
use encoding 'cp1251', STDOUT => 'cp866';
 
$| = 1;
 
    my    $address = 1;
    my ($modbus, $req, $trn, $res, $trs);
 
    $modbus = Protocol::Modbus->new(driver=>'TCP');
    $trs = Protocol::Modbus::Transport->new(        # транспорт обмена
        driver  => 'TCP',
        address    => '192.168.0.90'
    );
 
    my $regs = {
        0x00=>{size=>4, fmt=>"%s", desc=>"Device"},
        0x04=>{size=>2, fmt=>"%08X", desc=>"MAC"},
        0x06=>{size=>2, fmt=>"%s", desc=>"Version"},
        0x08=>{size=>2, fmt=>"%08X", desc=>"Checksum"},
        0x10=>{size=>8, fmt=>"%s", desc=>"Description"},
        0x20=>{size=>2, fmt=>"%08X", desc=>"Time"},
        0x22=>{size=>2, fmt=>"%08X", desc=>"UTC Time"},
        0x24=>{size=>1, fmt=>"%i", desc=>"GMT"},
        0x25=>{size=>1, fmt=>"%b", desc=>"Status"},
        0x30=>{size=>1, fmt=>"%i", desc=>"Modbus RTU Address"},
        0x31=>{size=>1, fmt=>"%i", desc=>"Modbus RTU baudrate"},
        0x32=>{size=>1, fmt=>"%i", desc=>"Modbus RTU timeout"},
        0x40=>{size=>2, fmt=>"%b", desc=>"Warning channels"},
        0x42=>{size=>2, fmt=>"%b", desc=>"Alarm channels"},
        0x44=>{size=>2, fmt=>"%b", desc=>"Freeze channels"},
        0x46=>{size=>2, fmt=>"%b", desc=>"Power channels"},
        0x60=>{size=>2, fmt=>"%u", desc=>"Journal writes counter"},
        0x62=>{size=>2, fmt=>"%u", desc=>"Journal capacity"},
        0x64=>{size=>2, fmt=>"%u", desc=>"Journal records"},
        0x66=>{size=>2, fmt=>"%u", desc=>"Journal last record"},
 
                0x0100=>{size=>8, fmt=>"%s", desc=>"Channel"},            # первый канал
                0x0110=>{size=>2, fmt=>"%u", desc=>"Total (nature)"},
                0x0112=>{size=>2, fmt=>"%u", desc=>"Current (nature)"},
                0x0114=>{size=>2, fmt=>"%u", desc=>"Reset (nature)"},
                0x0116=>{size=>2, fmt=>"%u", desc=>"Current MIN (nature)"},
                0x0118=>{size=>2, fmt=>"%u", desc=>"Current MAX (nature)"},
 
                0x0130=>{size=>2, fmt=>"%u", desc=>"Total (units)"},
                0x0132=>{size=>2, fmt=>"%u", desc=>"Current (units)"},
                0x0134=>{size=>2, fmt=>"%u", desc=>"Reset (units)"},
                0x0136=>{size=>2, fmt=>"%u", desc=>"Current MIN (units)"},
                0x0138=>{size=>2, fmt=>"%u", desc=>"Current MAX (units)"},
 
                0x0150=>{size=>2, fmt=>"%08X", desc=>"Date current"},
                0x0152=>{size=>2, fmt=>"%08X", desc=>"Date reset"},
                0x0154=>{size=>2, fmt=>"%08X", desc=>"Date total"},
 
                0x0160=>{size=>2, fmt=>"%s", desc=>"Units"},
                0x0162=>{size=>2, fmt=>"%u", desc=>"K"},
 
                0x0166=>{size=>2, fmt=>"%u", desc=>"Threshold MIN"},
                0x0168=>{size=>2, fmt=>"%u", desc=>"Threshold MAX"},
 
                0x0170=>{size=>1, fmt=>"%i", desc=>"Mode"},
                0x0171=>{size=>1, fmt=>"%16b", desc=>"Status"},
                0x0172=>{size=>1, fmt=>"%i", desc=>"Filter min/start"},
                0x0173=>{size=>1, fmt=>"%i", desc=>"Filter max/stop"},
                0x0174=>{size=>1, fmt=>"%16b", desc=>"Control"},
                0x0175=>{size=>1, fmt=>"%i", desc=>"Unit id"},
                0x0176=>{size=>1, fmt=>"%i", desc=>"Interval"},
                0x0177=>{size=>1, fmt=>"%i", desc=>"Start hour"},
                0x0178=>{size=>1, fmt=>"%i", desc=>"Intervals"},
 
                0x1e00=>{size=>8, fmt=>"%s", desc=>"Channel"},        # 30 канал
                0x1e10=>{size=>2, fmt=>"%u", desc=>"Total (nature)"},
                0x1e12=>{size=>2, fmt=>"%u", desc=>"Current (nature)"},
                0x1e14=>{size=>2, fmt=>"%u", desc=>"Reset (nature)"},
                0x1e16=>{size=>2, fmt=>"%u", desc=>"Current MIN (nature)"},
                0x1e18=>{size=>2, fmt=>"%u", desc=>"Current MAX (nature)"},
 
                0x1e30=>{size=>2, fmt=>"%u", desc=>"Total (units)"},
                0x1e32=>{size=>2, fmt=>"%u", desc=>"Current (units)"},
                0x1e34=>{size=>2, fmt=>"%u", desc=>"Reset (units)"},
                0x1e36=>{size=>2, fmt=>"%u", desc=>"Current MIN (units)"},
                0x1e38=>{size=>2, fmt=>"%u", desc=>"Current MAX (units)"},
 
                0x1e50=>{size=>2, fmt=>"%08X", desc=>"Date current"},
                0x1e52=>{size=>2, fmt=>"%08X", desc=>"Date reset"},
                0x1e54=>{size=>2, fmt=>"%08X", desc=>"Date total"},
 
                0x1e60=>{size=>2, fmt=>"%s", desc=>"Units"},
                0x1e62=>{size=>2, fmt=>"%u", desc=>"K"},
 
                0x1e66=>{size=>2, fmt=>"%u", desc=>"Threshold MIN"},
                0x1e68=>{size=>2, fmt=>"%u", desc=>"Threshold MAX"},
 
                0x1e70=>{size=>1, fmt=>"%i", desc=>"Mode"},
                0x1e71=>{size=>1, fmt=>"%16b", desc=>"Status"},
                0x1e72=>{size=>1, fmt=>"%i", desc=>"Filter min/start"},
                0x1e73=>{size=>1, fmt=>"%i", desc=>"Filter max/stop"},
                0x1e74=>{size=>1, fmt=>"%16b", desc=>"Control"},
                0x1e75=>{size=>1, fmt=>"%i", desc=>"Unit id"},
                0x1e76=>{size=>1, fmt=>"%i", desc=>"Interval"},
                0x1e77=>{size=>1, fmt=>"%i", desc=>"Start hour"},
                0x1e78=>{size=>1, fmt=>"%i", desc=>"Intervals"}
    };
    my ($k, $v, $str, $fmt);
    foreach $k (sort {$a <=> $b} keys $regs) {    # сортировать записи
         $v = $regs->{$k};
         $fmt = $v->{fmt};
 
        $req = $modbus->readHoldRegistersRequest(    # запрос
            address  => $k,
            quantity => $v->{size},
            unit     => $address # Not required for Modbus/TCP
        );
        printf "Address 0x%04X, $v->{desc}...", $k;
        $trn = $modbus->transaction( $trs, $req );    # транзакция запроса через транспорт
        $res = $trn->execute();                        # выполнить транзакцию
        if( !defined $res ) {
            die "failed at $v->{desc}\n";
        }
        $str = join('', @{$res->registers()});            # склеить строковые значения регистров
        $str =~ s/(..)(..)/$2$1/g if( $fmt ne "%s" );    # поменять младшие и старшие байты
        $str = pack("H*", $str);                        # сделать строку
        $str = unpack(length $str > 2 ? "L" : "S", $str) if( $fmt ne "%s" );    # преобразовать, только не строки
 
        printf $v->{fmt}."\n", $str;    # вывести на экран
    }