75 lines
1.8 KiB
Perl
Executable File
75 lines
1.8 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
#
|
|
package main;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Path::Tiny;
|
|
use Cwd qw(getcwd);
|
|
use IO::Socket::INET;
|
|
|
|
my $workdir = getcwd;
|
|
|
|
push @INC, "$workdir/lib";
|
|
|
|
require iec1107;
|
|
|
|
my $port = Device::SerialPort->new(
|
|
"/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AK072UA9-if00-port0")
|
|
or die "$! \n";
|
|
|
|
$port->baudrate(9600);
|
|
$port->databits(7);
|
|
$port->parity("even");
|
|
$port->stopbits(1);
|
|
$port->handshake("none");
|
|
$port->write_settings;
|
|
|
|
$port->purge_all();
|
|
$port->read_char_time(0);
|
|
$port->read_const_time(150); #was 100ms previously, this lead to race conditions
|
|
|
|
my $dir = path("$workdir/sensors"); # Config Directory there find the id-file
|
|
|
|
my $file = $dir->child("id.list");
|
|
|
|
# Read in the entire contents of a file
|
|
my $content = $file->slurp();
|
|
|
|
# openr() returns an IO::File object to read from
|
|
my $file_handle = $file->openr();
|
|
|
|
# It is possible to find out the device id of a single device on RS-485 9600@7E1 by sending "/?!\r\n"
|
|
# It does not work with more than one device on the same bus, it results in garbage!
|
|
my $passwd = "00000000"; # Standard password 0 over 8-digits
|
|
|
|
my $fhemDummy = 'Meter_';
|
|
|
|
my $HOSTNAME = "localhost";
|
|
my $HOSTPORT = "7072";
|
|
my $socket = IO::Socket::INET->new(
|
|
'PeerAddr' => $HOSTNAME,
|
|
'PeerPort' => $HOSTPORT,
|
|
'Proto' => 'tcp'
|
|
) or die "Cant\'t connect to FHEM Instance \n";
|
|
|
|
while ( my $id = $file_handle->getline() ) {
|
|
chomp $id;
|
|
my $drs110m =
|
|
iec1107->new( "port" => $port, "id" => $id, "passwd" => $passwd );
|
|
|
|
$drs110m->set_clock();
|
|
|
|
my $values = $drs110m->get_values();
|
|
|
|
next if ( ref($values) ne 'HASH' );
|
|
|
|
while ( my ( $reg, $val ) = each( %{$values} ) ) {
|
|
$socket->print(
|
|
'setreading ' . $fhemDummy . $id . ' ' . $reg . ' ' . $val . "\n" );
|
|
}
|
|
}
|
|
|
|
$socket->close;
|