From: Jan Michel Date: Wed, 2 Jul 2014 16:53:37 +0000 (+0200) Subject: added access to power switch script via TrbNet X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=d5a70e313a1fa0d7f308132a72710c420e22b246;p=daqtools.git added access to power switch script via TrbNet --- diff --git a/tools/pwrswitch.pl b/tools/pwrswitch.pl index 00bb0b6..ed67d0a 100755 --- a/tools/pwrswitch.pl +++ b/tools/pwrswitch.pl @@ -14,11 +14,14 @@ use Device::SerialPort; use feature 'state'; use URI::Escape; use Data::Dumper; +use HADES::TrbNet; use Time::HiRes qw( usleep); use Getopt::Long; +my $port; my $help; my $ser_dev; +my $isTrbNet = 0; Getopt::Long::Configure(qw(gnu_getopt)); GetOptions( 'help|h' => \$help, @@ -33,26 +36,38 @@ GetOptions( $ser_dev = "/dev/ttyUSB0" unless defined $ser_dev; -my $port = new Device::SerialPort($ser_dev); + sub Cmd { my ($c) = @_; - for my $i (0..2) { - $port->write($c."\n"); - my $a = ""; - for my $j (0..8) { - my ($l,$s) = $port->read(5); - $a .= $l; - if ($l < 5) {next;} - if ($s =~ /^\w[a-f0-9]{3}/) {return hex(substr($s,1,3));} - if ($s =~ /^\w[a-f0-9]{2}/) {return hex(substr($s,1,2));} - usleep(10000); + if($isTrbNet == 0) { + for my $i (0..2) { + $port->write($c."\n"); + my $a = ""; + for my $j (0..8) { + my ($l,$s) = $port->read(5); + $a .= $l; + if ($l < 5) {next;} + if ($s =~ /^\w[a-f0-9]{3}/) {return hex(substr($s,1,3));} + if ($s =~ /^\w[a-f0-9]{2}/) {return hex(substr($s,1,2));} + usleep(10000); + } + usleep(50000); + #print '.'; } - usleep(50000); - #print '.'; + } + else { + trb_register_read_mem($ser_dev,0xd600,1,100); #clear memory + my @c = split('',$c."\n"); + my @cmd = map(ord, @c); + trb_register_write_mem($ser_dev,0xd600,1,\@cmd,scalar @cmd); + usleep(20000); + my $r = trb_register_read_mem($ser_dev,0xd600,1,5); + my $s = join('',map (chr($_ & 0xff),@{$r->{$ser_dev}})); + if ($s =~ /^\w[a-f0-9]{3}/) {return hex(substr($s,1,3));} + if ($s =~ /^\w[a-f0-9]{2}/) {return hex(substr($s,1,2));} } return ; - } if ($help || (defined $ARGV[0] && $ARGV[0] =~ /help/)) { @@ -60,25 +75,38 @@ if ($help || (defined $ARGV[0] && $ARGV[0] =~ /help/)) { print "CHANNEL: Channel number, hex or decimal\n"; print "OPERATION: 1 (on), 0 (off), / (toggle), L (set limit)\n"; print "VALUE: A 10 Bit value, 3 hex digits or decimal\n"; + print "If the DEVICE is an hex value, it is treated as a TrbNet address. Uart must be present and is assumed to be set at right baud rate\n"; exit; } +if($ser_dev =~ /^0x([0-9a-fA-F]{4})/) { + $ser_dev = hex($1); + $isTrbNet = 1; + if(!defined $ENV{'DAQOPSERVER'}) { + die "DAQOPSERVER not set in environment"; + } + if (!defined &trb_init_ports()) { + die("can not connect to trbnet-daemon on the $ENV{'DAQOPSERVER'}"); + } + } +else { + $port = new Device::SerialPort($ser_dev); + unless ($port) { + print "can't open serial interface $ser_dev\n"; + exit; + } + $port->user_msg('ON'); + $port->baudrate(57600); + $port->parity("none"); + $port->databits(8); + $port->stopbits(1); + $port->handshake("none"); + $port->read_char_time(0); + $port->read_const_time(50); + $port->write_settings; + } + -unless ($port) -{ - print "can't open serial interface $ser_dev\n"; - exit; -} - -$port->user_msg('ON'); -$port->baudrate(57600); -$port->parity("none"); -$port->databits(8); -$port->stopbits(1); -$port->handshake("none"); -$port->read_char_time(0); -$port->read_const_time(50); -$port->write_settings; # debug output