line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Protocol::Modbus::Transaction; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
29
|
use strict; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
191
|
|
4
|
5
|
|
|
5
|
|
25
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
139
|
|
5
|
5
|
|
|
5
|
|
24
|
use Protocol::Modbus::Request; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
115
|
|
6
|
5
|
|
|
5
|
|
24
|
use Protocol::Modbus::Response; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
3127
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Define a progressive id |
9
|
|
|
|
|
|
|
$Protocol::Modbus::Transaction::ID = 0; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
6
|
|
|
6
|
0
|
20
|
my ($obj, %args) = @_; |
13
|
6
|
|
33
|
|
|
24
|
my $class = ref($obj) || $obj; |
14
|
6
|
|
|
|
|
45
|
my $self = { |
15
|
|
|
|
|
|
|
_request => $args{request}, |
16
|
|
|
|
|
|
|
_response => $args{response}, |
17
|
|
|
|
|
|
|
_protocol => $args{protocol}, |
18
|
|
|
|
|
|
|
_transport => $args{transport}, |
19
|
|
|
|
|
|
|
_id => Protocol::Modbus::Transaction::nextId(), |
20
|
|
|
|
|
|
|
}; |
21
|
6
|
|
|
|
|
41
|
bless $self, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Get/set protocol class (Pure modbus or TCP modbus) |
25
|
|
|
|
|
|
|
sub protocol { |
26
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
27
|
0
|
0
|
|
|
|
0
|
if (@_) { |
28
|
0
|
|
|
|
|
0
|
$self->{_protocol} = $_[0]; |
29
|
|
|
|
|
|
|
} |
30
|
0
|
|
|
|
|
0
|
return $self->{_protocol}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Transport object (TCP or Serial) |
34
|
|
|
|
|
|
|
sub transport { |
35
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
36
|
0
|
0
|
|
|
|
0
|
if (@_) { |
37
|
0
|
|
|
|
|
0
|
$self->{_transport} = $_[0]; |
38
|
|
|
|
|
|
|
} |
39
|
0
|
|
|
|
|
0
|
return $self->{_transport}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub close { |
43
|
0
|
|
|
0
|
0
|
0
|
my $self = $_[0]; |
44
|
0
|
|
|
|
|
0
|
$self->transport->disconnect(); |
45
|
0
|
|
|
|
|
0
|
$self->request(undef); |
46
|
0
|
|
|
|
|
0
|
$self->response(undef); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub execute { |
50
|
0
|
|
|
0
|
1
|
0
|
my $self = $_[0]; |
51
|
0
|
|
|
|
|
0
|
my ($req, $res); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# To execute a transaction, we must be connected |
54
|
0
|
0
|
|
|
|
0
|
if (!$self->transport->connect()) { |
55
|
0
|
|
|
|
|
0
|
croak('Modbus connection with server not available!'); |
56
|
0
|
|
|
|
|
0
|
return (undef); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# We must have a request object |
60
|
0
|
0
|
|
|
|
0
|
if (!($req = $self->request())) { |
61
|
0
|
|
|
|
|
0
|
croak('Modbus transaction without request is not possible!'); |
62
|
0
|
|
|
|
|
0
|
return (undef); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Send request |
66
|
0
|
|
|
|
|
0
|
$self->transport->send($req); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#warn('Sent [', $req, '] request object'); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Get a response |
71
|
0
|
|
|
|
|
0
|
my $raw_data = $self->transport->receive($req); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#warn('Received [', uc unpack('H*', $raw_data), '] data'); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Init a response object with the data received by transport |
76
|
0
|
|
|
|
|
0
|
$res = Protocol::Modbus::Response->new(frame => $raw_data); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Protocol (TCP/RTU) should now parse the response |
79
|
0
|
|
|
|
|
0
|
return ($self->protocol->parseResponse($res)); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub id { |
84
|
6
|
|
|
6
|
0
|
21
|
my $self = $_[0]; |
85
|
6
|
|
|
|
|
24
|
return $self->{_id}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub nextId { |
89
|
6
|
|
|
6
|
0
|
39
|
return ($Protocol::Modbus::Transaction::ID++); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Get/set request class |
93
|
|
|
|
|
|
|
sub request { |
94
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
95
|
0
|
0
|
|
|
|
|
if (@_) { |
96
|
0
|
|
|
|
|
|
$self->{_request} = $_[0]; |
97
|
|
|
|
|
|
|
} |
98
|
0
|
|
|
|
|
|
return $self->{_request}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Get/set response class |
102
|
|
|
|
|
|
|
sub response { |
103
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
104
|
0
|
0
|
|
|
|
|
if (@_) { |
105
|
0
|
|
|
|
|
|
$self->{_request} = $_[0]; |
106
|
|
|
|
|
|
|
} |
107
|
0
|
|
|
|
|
|
return $self->{_request}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# TODO Convert transaction to string |
111
|
|
|
|
|
|
|
sub stringify { |
112
|
0
|
|
|
0
|
0
|
|
my $self = $_[0]; |
113
|
0
|
|
|
|
|
|
return 'TRANSACTION_STRING'; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__END__ |