| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Lab::Moose::Connection::Socket; |
|
2
|
|
|
|
|
|
|
$Lab::Moose::Connection::Socket::VERSION = '3.900'; |
|
3
|
|
|
|
|
|
|
#ABSTRACT: Transfer IEEE 488.2 / SCPI messages over TCP |
|
4
|
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
203930
|
use v5.20; |
|
|
3
|
|
|
|
|
31
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
1103
|
use Moose; |
|
|
3
|
|
|
|
|
943554
|
|
|
|
3
|
|
|
|
|
20
|
|
|
8
|
3
|
|
|
3
|
|
23060
|
use MooseX::Params::Validate; |
|
|
3
|
|
|
|
|
179239
|
|
|
|
3
|
|
|
|
|
28
|
|
|
9
|
3
|
|
|
3
|
|
3507
|
use Socket qw(IPPROTO_TCP TCP_NODELAY); |
|
|
3
|
|
|
|
|
11460
|
|
|
|
3
|
|
|
|
|
567
|
|
|
10
|
3
|
|
|
3
|
|
1392
|
use IO::Socket::INET; |
|
|
3
|
|
|
|
|
43667
|
|
|
|
3
|
|
|
|
|
19
|
|
|
11
|
3
|
|
|
3
|
|
2776
|
use IO::Select; |
|
|
3
|
|
|
|
|
4870
|
|
|
|
3
|
|
|
|
|
151
|
|
|
12
|
3
|
|
|
3
|
|
25
|
use Carp; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
177
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
3
|
|
|
3
|
|
991
|
use Lab::Moose::Instrument qw/timeout_param read_length_param/; |
|
|
3
|
|
|
|
|
12
|
|
|
|
3
|
|
|
|
|
261
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
3
|
|
|
3
|
|
68
|
use namespace::autoclean; |
|
|
3
|
|
|
|
|
15
|
|
|
|
3
|
|
|
|
|
34
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has client => ( |
|
19
|
|
|
|
|
|
|
is => 'ro', |
|
20
|
|
|
|
|
|
|
isa => 'IO::Socket::INET', |
|
21
|
|
|
|
|
|
|
writer => '_client', |
|
22
|
|
|
|
|
|
|
init_arg => undef, |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has select => ( |
|
26
|
|
|
|
|
|
|
is => 'ro', |
|
27
|
|
|
|
|
|
|
isa => 'IO::Select', |
|
28
|
|
|
|
|
|
|
writer => '_select', |
|
29
|
|
|
|
|
|
|
init_arg => undef, |
|
30
|
|
|
|
|
|
|
); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has host => ( |
|
33
|
|
|
|
|
|
|
is => 'ro', |
|
34
|
|
|
|
|
|
|
isa => 'Str', |
|
35
|
|
|
|
|
|
|
required => 1, |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has port => ( |
|
39
|
|
|
|
|
|
|
is => 'ro', |
|
40
|
|
|
|
|
|
|
isa => 'Int', |
|
41
|
|
|
|
|
|
|
required => 1, |
|
42
|
|
|
|
|
|
|
); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
has write_termchar => ( |
|
45
|
|
|
|
|
|
|
is => 'ro', |
|
46
|
|
|
|
|
|
|
isa => 'Maybe[Str]', |
|
47
|
|
|
|
|
|
|
default => "\n", |
|
48
|
|
|
|
|
|
|
); |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub BUILD { |
|
51
|
1
|
|
|
1
|
0
|
19
|
my $self = shift; |
|
52
|
1
|
|
|
|
|
87
|
my $host = $self->host(); |
|
53
|
1
|
|
|
|
|
37
|
my $port = $self->port(); |
|
54
|
1
|
|
|
|
|
45
|
my $timeout = $self->timeout(); |
|
55
|
1
|
50
|
|
|
|
32
|
my $client = IO::Socket::INET->new( |
|
56
|
|
|
|
|
|
|
PeerAddr => $host, |
|
57
|
|
|
|
|
|
|
PeerPort => $port, |
|
58
|
|
|
|
|
|
|
Proto => 'tcp', |
|
59
|
|
|
|
|
|
|
Timeout => $timeout, |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# enable use of orphaned connection after a script was killed by user |
|
62
|
|
|
|
|
|
|
ReuseAddr => 1, |
|
63
|
|
|
|
|
|
|
) or croak "cannot open connection with $host on port $port: $!"; |
|
64
|
|
|
|
|
|
|
|
|
65
|
1
|
50
|
|
|
|
2310
|
$client->setsockopt( IPPROTO_TCP, TCP_NODELAY, 1 ) |
|
66
|
|
|
|
|
|
|
or croak "setsockopt: cannot enable TCP_NODELAY"; |
|
67
|
|
|
|
|
|
|
|
|
68
|
1
|
50
|
|
|
|
27
|
my $select = IO::Select->new($client) |
|
69
|
|
|
|
|
|
|
or croak "cannot create IO::Select object: $!"; |
|
70
|
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
118
|
$self->_client($client); |
|
72
|
1
|
|
|
|
|
49
|
$self->_select($select); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub Write { |
|
76
|
3
|
|
|
3
|
0
|
16
|
my ( $self, %args ) = validated_hash( |
|
77
|
|
|
|
|
|
|
\@_, |
|
78
|
|
|
|
|
|
|
timeout_param, |
|
79
|
|
|
|
|
|
|
command => { isa => 'Str' }, |
|
80
|
|
|
|
|
|
|
); |
|
81
|
|
|
|
|
|
|
|
|
82
|
3
|
|
50
|
|
|
1109
|
my $write_termchar = $self->write_termchar() // ''; |
|
83
|
3
|
|
|
|
|
9
|
my $command = $args{command} . $write_termchar; |
|
84
|
3
|
|
|
|
|
25
|
my $timeout = $self->_timeout_arg(%args); |
|
85
|
3
|
|
|
|
|
106
|
my $host = $self->host(); |
|
86
|
3
|
|
|
|
|
104
|
my $port = $self->port(); |
|
87
|
|
|
|
|
|
|
|
|
88
|
3
|
|
|
|
|
104
|
my $client = $self->client(); |
|
89
|
|
|
|
|
|
|
|
|
90
|
3
|
|
|
|
|
7
|
my $length = length($command); |
|
91
|
3
|
|
|
|
|
4
|
my $written = 0; |
|
92
|
3
|
|
|
|
|
101
|
my $select = $self->select(); |
|
93
|
3
|
|
|
|
|
8
|
while ($length) { |
|
94
|
3
|
50
|
|
|
|
13
|
if ( !$select->can_write($timeout) ) { |
|
95
|
0
|
|
|
|
|
0
|
croak "timeout in Socket connection Write, host $host on port $port"; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
3
|
50
|
|
|
|
193
|
my $bytes_written = $client->syswrite( $command, $length, $written ) |
|
98
|
|
|
|
|
|
|
or croak("Write: syswrite to host $host on port $port failed: $!"); |
|
99
|
3
|
|
|
|
|
240
|
$written += $bytes_written; |
|
100
|
3
|
|
|
|
|
23
|
$length -= $bytes_written; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub Read { |
|
105
|
3
|
|
|
3
|
1
|
21
|
my ( $self, %args ) = validated_hash( |
|
106
|
|
|
|
|
|
|
\@_, |
|
107
|
|
|
|
|
|
|
timeout_param(), |
|
108
|
|
|
|
|
|
|
read_length_param(), |
|
109
|
|
|
|
|
|
|
); |
|
110
|
3
|
|
|
|
|
981
|
my $timeout = $self->_timeout_arg(%args); |
|
111
|
3
|
|
|
|
|
19
|
my $read_length = $self->_read_length_arg(%args); |
|
112
|
3
|
|
|
|
|
94
|
my $client = $self->client(); |
|
113
|
3
|
|
|
|
|
104
|
my $select = $self->select(); |
|
114
|
3
|
|
|
|
|
92
|
my $host = $self->host(); |
|
115
|
3
|
|
|
|
|
124
|
my $port = $self->port(); |
|
116
|
|
|
|
|
|
|
|
|
117
|
3
|
|
|
|
|
11
|
my $string; |
|
118
|
3
|
|
|
|
|
5
|
my $length = 0; |
|
119
|
3
|
100
|
|
|
|
9
|
if ( $args{read_length} ) { |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# explicit read_length arg: |
|
122
|
|
|
|
|
|
|
# Keep reading until we have $read_length bytes. |
|
123
|
2
|
|
|
|
|
7
|
while ($read_length) { |
|
124
|
4
|
50
|
|
|
|
14
|
if ( !$select->can_read($timeout) ) { |
|
125
|
0
|
|
|
|
|
0
|
croak "timeout in connection Read, host $host on port $port"; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
4
|
50
|
|
|
|
170
|
my $read_bytes |
|
128
|
|
|
|
|
|
|
= $client->sysread( $string, $read_length, $length ) |
|
129
|
|
|
|
|
|
|
or croak "socket read error, host $host on port $port: $!"; |
|
130
|
4
|
|
|
|
|
360
|
$read_length -= $read_bytes; |
|
131
|
4
|
|
|
|
|
12
|
$length += $read_bytes; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
else { |
|
135
|
1
|
50
|
|
|
|
8
|
if ( !$select->can_read($timeout) ) { |
|
136
|
0
|
|
|
|
|
0
|
croak "timeout in connection Read, host $host on port $port"; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
1
|
50
|
|
|
|
62
|
$client->sysread( $string, $read_length ) |
|
139
|
|
|
|
|
|
|
or croak "socket read error: $!"; |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
3
|
|
|
|
|
89
|
return $string; |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
0
|
0
|
|
sub Clear { |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Some instruments provide an additional control port. |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
with qw/ |
|
151
|
|
|
|
|
|
|
Lab::Moose::Connection |
|
152
|
|
|
|
|
|
|
/; |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable(); |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
1; |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
__END__ |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=pod |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=encoding UTF-8 |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 NAME |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Lab::Moose::Connection::Socket - Transfer IEEE 488.2 / SCPI messages over TCP |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 VERSION |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
version 3.900 |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
use Lab::Moose; |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
my $instrument = instrument( |
|
177
|
|
|
|
|
|
|
type => 'random_instrument', |
|
178
|
|
|
|
|
|
|
connection_type => 'Socket', |
|
179
|
|
|
|
|
|
|
connection_options => { |
|
180
|
|
|
|
|
|
|
host => '132.199.11.2', |
|
181
|
|
|
|
|
|
|
port => 5025 |
|
182
|
|
|
|
|
|
|
}, |
|
183
|
|
|
|
|
|
|
); |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This connection uses L<IO::Socket::INET> to interface with the operating |
|
188
|
|
|
|
|
|
|
system's TCP stack. This works on most operating systems without installing any |
|
189
|
|
|
|
|
|
|
additional software. |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Without knowing the syntax of the used command-messages there is no way for the |
|
192
|
|
|
|
|
|
|
connection to determine when C<Read> is finished. This is unlike GPIB, USBTMC, |
|
193
|
|
|
|
|
|
|
or VXI-11 which have explicit End of Message indicators. To deal with this, the |
|
194
|
|
|
|
|
|
|
C<read_length> parameter has the following semantics: |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=over |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item C<Read> is given an explicit C<read_length> parameter |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Keep calling sysread until C<read_length> bytes are read. |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item C<Read> is not given an explicit C<read_length> parameter |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Do a single sysread with the connections default C<read_length>. |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=back |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
For SCPI definite length blocks you will have to give the exact block length |
|
209
|
|
|
|
|
|
|
with the C<read_length> parameter. |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 CONNECTION OPTIONS |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=over |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item host |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Host address. |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item port |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Host port. |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item write_termchar |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Append this to each write command. Default: C<"\n">. |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=back |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
This software is copyright (c) 2023 by the Lab::Measurement team; in detail: |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Copyright 2017 Andreas K. Huettel, Simon Reinhardt |
|
234
|
|
|
|
|
|
|
2020 Andreas K. Huettel |
|
235
|
|
|
|
|
|
|
2021 Andreas K. Huettel, Simon Reinhardt |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
239
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |