Advertisement
ZaynerTech

computer_interface.pl for Arduino webpage communication

Mar 31st, 2014
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 4.87 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # This code will automatically detect your Arduino(s)
  4. # and communicate with them through the webpage
  5. #
  6.  
  7. $LINUX = 1;
  8. $MAC = 0;
  9. $dev = 0;
  10. my %equipment = ();
  11. use Device::SerialPort;
  12.  
  13.  
  14. if($LINUX)
  15. {
  16.   @devices = `lsusb`;
  17. }
  18.  
  19. if($MAC)
  20. {
  21.  
  22.  @devices = `system_profiler SPUSBDataType`;
  23. }
  24.  
  25. foreach $usb (@devices)
  26. {
  27.  $founddev = 0;
  28.  $tot = 0;
  29.  
  30.  @usbinfo = split(/ /,$usb);
  31.  $bus = $usbinfo[1];
  32.  $bus =~ s/^0+//g;
  33.  ($vendorid, $productid) = split(/:/, $usbinfo[5]);
  34. # print "$bus $vendorid $productid\n";
  35.  
  36.  
  37.  if($vendorid =~ /2341/)
  38.  {
  39.   for($x = 1; $x < 10; $x++)
  40.   {
  41.     if(`grep Arduino /sys/bus/usb/devices/$bus-$x/manufacturer`)
  42.     {
  43.       $vend = `cat /sys/bus/usb/devices/$bus-$x/idVendor`;
  44.       if($vendorid == $vend)
  45.       {
  46.         $prod = `cat /sys/bus/usb/devices/$bus-$x/idProduct`;
  47.         if($productid == $prod)
  48.         {
  49.           $ttyusb = `ls /sys/bus/usb/devices/$bus-$x/$bus-$x:1.0/tty/`;
  50.           chomp($ttyusb);
  51.           for($y = 0; $y < $dev; $y++)
  52.           {
  53.              if($equipment{$y}{uport} =~ /$ttyusb/){ $next++; print "YEAY : $equipment{$y}{uport} : $ttyusb : $y\n"; last;}
  54.           }
  55.           if(!$next)
  56.           {
  57.           print "Found Arduino on Port /dev/$ttyusb\n";
  58.           $porty[$dev] = Device::SerialPort->new("/dev/$ttyusb") or print "Can't open Arduino on /dev/$ttyusb";
  59.           $porty[$dev]->read_char_time(0);
  60.           $porty[$dev]->read_const_time(0);
  61.           $porty[$dev]->baudrate(9600); # you may change this value
  62.           $porty[$dev]->databits(8); # but not this and the two following
  63.           $porty[$dev]->parity("none");
  64.           $porty[$dev]->stopbits(1);
  65.           sleep(5);
  66.  
  67.           while(1)
  68.           {
  69.              $porty[$dev]->purge_all;
  70.              while($tot < 48)
  71.              {
  72.                  print "Checking ID\n";
  73.                  $porty[$dev]->write("ID\n");
  74.                  $porty[$dev]->write("ID\n");
  75.                  sleep(1);
  76.  
  77.                ($count1, $buff) = $porty[$dev]->read(512);
  78.                $tot += $count1;
  79.                $buffer .= $buff;
  80.                if($buffer =~ /IDEND/){ $founddev = 1; last; }
  81.              }
  82.             if($founddev)
  83.             {
  84.               last;
  85.             }
  86.             $tot = 0;
  87.             $count1 = 0;
  88.             $buffer = "";
  89.           }
  90.           ($crap, $deviceattached, $end) = split(/:/, $buffer);
  91.           $deviceattached =~ s/\n\s*//g;
  92.           print "DEVICE: $deviceattached : $ttyusb\n";
  93.           $equipment{$dev}{name} = $deviceattached;
  94.           $equipment{$dev}{uport} = $ttyusb;
  95.           $tot = 0;
  96.           $count1 = 0;
  97.           $buffer = "";
  98.           $dev++;
  99.           last;
  100.          } #!$next
  101.          $next = 0;
  102.       } #if(product
  103.     } #if(vend
  104.   }  #if(`grep
  105. } #for($x
  106. } #if(vendorid
  107.  
  108. } #foreach(usb
  109.  
  110. my $chars=0;
  111. my $buffer="";
  112. my $timeout=10;
  113. my $x = 0;
  114. my $success = 0;
  115.  
  116. $start = time();
  117.  
  118. #while($count1)
  119. #{
  120. #  ($count1, $info1) = $porty->read(2048);
  121. #}
  122.  
  123. for($x = 0; $x < $dev; $x++)
  124. {
  125.   `echo > $equipment{$x}{name}.dat`;
  126.   `echo > /tmp/$equipment{$x}{name}`;
  127. }
  128.  
  129. while(1)
  130. {
  131.   for($x = 0; $x < $dev; $x++)
  132.   {
  133.  
  134.    $porty[$x]->purge_all;
  135.    while($tot <512)
  136.    {
  137.    ($count1, $buff) = $porty[$x]->read(512);
  138.    $tot+=$count1;
  139.    $buffer.=$buff;
  140.    if($buffer =~ /END/){ last; }
  141.    }
  142.    @cent = split(/:/,$buffer);
  143.   if($cent[0] =~ /\d+/)
  144.   {  
  145.      $cent[0] =~ s/\n\s*//g;
  146.      undef $buffer;
  147.    
  148.      $currenttime = int((time() - $start));
  149.      open(EQ, ">> $equipment{$x}{name}.dat") or print "Can't open!\n";
  150.      print EQ "$currenttime $cent[0]\n";
  151.      close(EQ);
  152.      `gnuplot $equipment{$x}{name}`;
  153.  
  154.      sleep(1);
  155.      #close(FILE);
  156.    
  157.    }
  158.    open(DEVDAT, "/tmp/$equipment{$x}{name}") or print "Equipment data read error /tmp/$equipment{$x}{name}.dat \n";
  159.    @devdat = <DEVDAT>;
  160.    foreach $devline (@devdat)
  161.    {
  162.      if($devline)
  163.      {
  164.        $devline =~ s/\n\s*//g;
  165.        @commands = split(/:/, $devline);
  166.  
  167.        if($commands[0])
  168.        {
  169.  
  170.           while(1)
  171.           {
  172.              $porty[$x]->purge_all;
  173.              while($tot < 48)
  174.              {
  175.                  print "Sending Command @commands\n";
  176.                  $porty[$x]->write("$commands[0]:$commands[1]\n");
  177.                  sleep(1);
  178.  
  179.                ($count1, $buff) = $porty[$x]->read(512);
  180.                $tot += $count1;
  181.                $buffer .= $buff;
  182.                if($buffer =~ /SUCCESS/){ $success = 1; last; }
  183.              }
  184.             if($success)
  185.             {
  186.               print "Success\n";
  187.               last;
  188.             }
  189.            
  190.             $tot = 0;
  191.             $count1 = 0;
  192.             $buffer = "";
  193.           }
  194.          $success = 0;
  195.         }
  196.      }  
  197.    }
  198.    
  199.    close(DEVDAT);
  200.    `echo > /tmp/$equipment{$x}{name}`;
  201.    undef @devdat;
  202.    undef @commands;
  203.  
  204.   }#for(x
  205. }#while(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement