#!/usr/bin/perl
use Time::HiRes;
#Time::HiRes::usleep(500000);
$sleep=1000000;
$sleep=300000;
$log=$ENV{'log_fhg'};
# RS232 Setup
$COM_FHG=$ENV{'COM_FHG'}; # Quelle 1: vom Bash sourcing
$COM_FHG="/dev/ttyUSB1"; # Quelle 2: von ttyUSB1
$COM_FHG="/dev/ttyUSB${ARGV[0]}"; # Quelle 3: als Skript parameter
print "$COM_FHG\n";
$SEP=$ENV{'SEP'};
$SEP="=";
use Device::SerialPort;
# Serielle Schnittstelle einstellen
$rc=&init_ser($COM_FHG);
## Main
# check envrionment
# work
if ( $ENV{'work'} eq "" ) {
print STDERR "Error: Environment variable 'work' not set - exiting\n"; exit; } # log if ( $ENV{'log_fhg'} eq "" ) { print STDERR "Error: Environment variable 'log_fhg' not set - exiting\n"; exit; }
if ( $ARGV[1] ne "M1" ) {
&start;
}
# m1 m2 Werte auslesen
while (1) {
# Lese Geschwindigkeit wieder rauf setzen
$sleep=1000000;
print "SEP>>>>$SEP<<<\n";
# wenn er ein M3 will, dann gib ihm eins
if ( $data =~ /^4d 33/ ) {
&o("4D 33 01 01");
&i;
}
# Sende M2
&o("4D 32 01 01");
$data=&i;
print STDERR "length=".length($data).">$data<\n";
my $ts = `date +%s`;
chop($ts);
$data=substr($data,0,164);
$timestamp=$data if ( $data =~ /^4d 32/ && length($data) == 36 );
system("echo $ts$SEP$data$SEP$timestamp > $log") if ( $data =~ /^4d 31/ && length($data) == 165 )
# Sende M1
&o("4D 31 01 01");
$data=&i;
print STDERR "length=".length($data).">$data<\n";
my $ts = `date +%s`;
chop($ts);
$timestamp=$data if ( $data =~ /^4d 32/ && length($data) == 36 );
system("echo $ts$SEP$data$SEP$timestamp > $log") if ( $data =~ /^4d 31/ && length($data) == 165 )
} # ende While
exit;
###########################################
sub start {
##
# Hier wird die Messerei initialisiert und gestartet
# und alle Werte von MA - MZ durchgearbeitetet
# erst wenn diese initialisierungsphase (~ 3 min) durch ist
# sendet er die laufendenen Betriebsdaten
#
$user = "52 61 03 00 00 7B"; # Plumber
$user = "52 61 03 00 00 01"; # 1 normal User
$user = "52 61 03 00 00 F3"; # 1 normal User
$user = "52 61 03 00 FF F9"; # -7 Service
open (ME, ">$ENV{'work'}/me.txt");
open (MD, ">$ENV{'work'}/md.txt");
my $data;
#Sende Initialisierung an S3100
while ( $data !~ /^52 61 01 01/ ) {
&o("$user");
# lese was Steuerung zurueck bringt
$data = &i();
print "<$data\n";
sleep(10) if ( $data !~ /^52 61 01 01/);
}
my $datas = "4d 41";
my $data;
while ( $data !~ /^4d 3. / ) {
&o("$datas 01 01");
# lies nochmal was er jetzt will
$data =&i();
if ( $data == "") {next;};
print "<$data\n";
# dump all ME Values into a text file
print ME "$data\n" if ( $data =~ /^4d 45/ );
# dump all MD Values into a text file
print MD "$data\n" if ( $data =~ /^4d 44/ );
print STDERR &lesbar($data)."\n";
# nächsten Befehl zusammen bauen
$datas = $data;
$datas =~ s/^(.. ..).*$/$1/g;
} # ende While
$datas = $data;
$datas =~ s/^(.. ..).*$/$1/g;
$x = 0;
# 32 mal M2 Werte auslesen
while ($x < 33) {
&o("4D 32 01 01");
$data =&i;
print "<$data\n";
$datas=$data;
$datas =~ s/^(.. ..).*$/$1/g;
$x++;
} # ende x While
# Sende Rb bis init gesendet wird
&o("52 62 03 00 00 01");
# Lese bestätigung; normal: 52 62 01 01 00 b6
$data = &i();
print "<$data\n";
&o("4D 32 01 01");
&i(1);
close(MD);
close(ME);
} # Ende Sub start
# --------------------------------------
sub get_timestamp {
my $i = $_[0];
my $uhr_HH=substr($i,15,2);
my $uhr_MM=substr($i,12,2);
my $datum_YY=substr($i,27,2);
my $datum_MM=substr($i,21,2);
my $datum_DD=substr($i,24,2);
$i = "$datum_DD.$datum_MM.$datum_YY $uhr_HH:$uhr_MM";
return $i;
} # Ende sub get_timestamp
# --------------------------------------
sub o {
my $a = $_[0];
my $debug = $_[1];
# Checksumme hinzufuegen
$a="$a ".&chksum($a);
# alle spaces entfernen
$a =~ s/ //g;
# binaer string erzeugen
my $i = pack( "H*",$a);
print STDERR "SEND:$a<\n";
# Send request
$serial->write($i."\r");
my $rc=$?;
($i = unpack("c*", $a)) =~ s/(..)/$1 /g;
# sleep(1);
Time::HiRes::usleep($sleep);
return $rc;
} # End sub
# ----------------------------------
sub i {
my($count, $data) = $serial->read(1000);
my $data1=$data;
($data = unpack("H*", $data) ) =~ s/(..)/$1 /g;
print STDERR "RECV:$data1\n";
#Time::HiRes::usleep($sleep);
return $data;
} # End sub
#-------------------------------------------------------
sub init_ser {
my $dev = $_[0];
$serial = Device::SerialPort->new($dev);
$serial->debug(1);
# Buffer leeren
$serial ->lookclear();
$| = 1;
#$serial->user_msg(ON);
#$serial->baudrate(57600);
#$serial->baudrate(19200);
$serial->baudrate(9600);
$serial->databits(8);
$serial->parity("none");
$serial->stopbits(1);
$serial->handshake("xon");
}
# ---------------------
sub chksum {
my $data = $_[0];
my $summ = 0 ;
my @values = split(/ /, $data);
foreach (@values) {
# dezimal zahl draus machen
my $tmp = hex($_);
$summ = $summ + $tmp;
} # End foreach
$data = &dec2hex($summ);
$data=~ s/^(...)$/0$1/g;
$data=~ s/^(..)$/00$1/g;
return $data;
} # End sub
# ------------------------------------
sub dec2hex {
# parameter passed to
# the subfunction
my $decnum = $_[0];
# the final hex number
my $hexnum;
my $tempval;
while ($decnum != 0) {
# get the remainder (modulus function)
# by dividing by 16
$tempval = $decnum % 16;
# convert to the appropriate letter
# if the value is greater than 9
if ($tempval > 9) {
$tempval = chr($tempval + 55);
}
# 'concatenate' the number to
# what we have so far in what will
# be the final variable
$hexnum = $tempval . $hexnum ;
# new actually divide by 16, and
# keep the integer value of the
# answer
$decnum = int($decnum / 16);
# if we cant divide by 16, this is the
# last step
if ($decnum < 16) {
# convert to letters again..
if ($decnum > 9) {
$decnum = chr($decnum + 55);
}
# add this onto the final answer..
# reset decnum variable to zero so loop
# will exit
$hexnum = $decnum . $hexnum;
$decnum = 0
}
}
return $hexnum;
} # end sub
# Lese Antwort vom S3100
# # Read response
# $data = &i;
# # Lese von der S3100 geforderte Befehl
# $data =~ s/^..................(.....).*$/${1} 01 01/g;
# $datad=$data." ".&chksum($data);
sub lesbar {
my $string = $_[0];
$string =~ s/^.......................(.*)......$/$1/g;
$string =~ s/E1/ß/g;
$string =~ s/81/ü/g;
$string =~ s/84/ä/g;
$string =~ s/94/ö/g;
$string =~ s/([a-fA-F0-9]{2})/chr(hex $1)/eg;
return $string;
} # Ende lesbar