]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
Added option to use IP address to talk to DCDC boards
authorJan Michel <j.michel@gsi.de>
Tue, 21 Jul 2020 12:45:29 +0000 (14:45 +0200)
committerJan Michel <j.michel@gsi.de>
Tue, 21 Jul 2020 12:45:29 +0000 (14:45 +0200)
atmega32u4/dcdc_rich/dcdc.pl

index ce8496814d1938b8db03bee6ab9b3d953d31b7a8..93cad9f3c90c114f6f38eafe2f063cb18c9eb127 100755 (executable)
 use strict;
 use warnings;
 use Device::SerialPort;
+use IO::Socket;
+use IO::Handle;
+use Fcntl;
+
 use feature 'state';
 use URI::Escape;
 use Data::Dumper;
@@ -27,6 +31,7 @@ GetOptions(
            'device|d=s' => \$ser_dev,
           ) ;
 
+my $isEthernet = 0;          
 my $mode = 0;
 my $ch;
 my $uC;
@@ -54,7 +59,7 @@ sub PrintAnswer {
 sub PrintAnswerNice {
     my ($s) = @_;
     my $FSR = 0.002; #Full Sclae Range is set to +-4.096V; LSB Size is 2mV
-    if (substr($s,0,1) ne 'A') {die 'not an correct Answer from DCDC board'}
+    if (substr($s,0,1) ne 'A') {die 'not a correct Answer from DCDC board'}
     my $command = hex(substr($s,5,1));
     my $ch   = hex(substr($s,4,1));
     my $uC   = hex(substr($s,1,2));
@@ -104,7 +109,7 @@ sub SendCmdShort {
         my $uC = $ARGV[1];
         if (substr($uC,0,2) eq "0x") {$uC = hex(substr($uC,2));}
         if ($uC >= $num) {
-            die "This microcontroller Number is not allowed! \n";
+            die "This microcontroller number is not allowed! \n";
         }
 
         # channel number
@@ -119,15 +124,17 @@ sub SendCmdShort {
         #print $cmd."\n";
         PrintAnswerNice(Cmd($cmd)) #Answer without \n
     } else {
-        die "Not all arguements were specified!\n";
+        die "Not all arguments were specified!\n";
     }
 
 }
 
 sub Cmd {
-    my ($c) = @_;
+  my ($c) = @_;
+  $c .= "\n";
+  if($isEthernet == 0) {  
     for my $i (0..0) {
-        $port->write($c."\n");
+        $port->write($c);
         my $a = "";
         for my $j (0..16) {
             my ($l,$s) = $port->read(12);
@@ -142,12 +149,26 @@ sub Cmd {
         usleep(50000);
         #print '.';
     }
+  }
+  if($isEthernet == 1) {
+    print $port  "$c";
+    my $x = "";
+    for my $i (0..10) {
+      $x .=  <$port>;
+      if($x && ($x =~ /\n/ || $x =~ /\r/) ) {
+        chomp $x;
+        return $x;
+        }
+      usleep(1000);
+      }
+    }
     return ;
 }
 
 if ($help || (defined $ARGV[0] && $ARGV[0] =~ /help/)) {
     print "dcdc.pl [-d DEVICE] reg [uC [CHANNEL [OPERATION [REGISTER [VALUE]]]]]\n";
     print "dcdc.pl [-d DEVICE] CMD [uC [CHANNEL [VALUE]]]\n\n";
+    print "DEVICE:    Either a serial device or an IP number.\n";
     print "CMD:       s: set DCDC setting, g: get DCDC setting,\n";
     print "           t: temperature, v: input voltage, c: input current;\n";  
     print "uC:        number of Microcontrolle rin chain. As Hex or Dec\n";
@@ -158,6 +179,12 @@ if ($help || (defined $ARGV[0] && $ARGV[0] =~ /help/)) {
     exit;
 }
 
+if ($ser_dev =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
+  $isEthernet = 1;
+  $port = IO::Socket::INET->new(PeerAddr => $ser_dev, PeerPort => 23, Proto => "tcp", Type => SOCK_STREAM, Timeout => 1) 
+              or (print("Device not found") && return);  
+  }
+else {  
   $port = new Device::SerialPort($ser_dev);
   unless ($port) {
     print "can't open serial interface $ser_dev\n";
@@ -172,7 +199,7 @@ if ($help || (defined $ARGV[0] && $ARGV[0] =~ /help/)) {
   $port->read_char_time(0);
   $port->read_const_time(50);
   $port->write_settings;
-
+  }
 
 #Scan for number of Microcontrolles.
 $num = Cmd("S");