Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # This code will automatically detect your Arduino(s)
- # and communicate with them through the webpage
- #
- $LINUX = 1;
- $MAC = 0;
- $dev = 0;
- my %equipment = ();
- use Device::SerialPort;
- if($LINUX)
- {
- @devices = `lsusb`;
- }
- if($MAC)
- {
- @devices = `system_profiler SPUSBDataType`;
- }
- foreach $usb (@devices)
- {
- $founddev = 0;
- $tot = 0;
- @usbinfo = split(/ /,$usb);
- $bus = $usbinfo[1];
- $bus =~ s/^0+//g;
- ($vendorid, $productid) = split(/:/, $usbinfo[5]);
- # print "$bus $vendorid $productid\n";
- if($vendorid =~ /2341/)
- {
- for($x = 1; $x < 10; $x++)
- {
- if(`grep Arduino /sys/bus/usb/devices/$bus-$x/manufacturer`)
- {
- $vend = `cat /sys/bus/usb/devices/$bus-$x/idVendor`;
- if($vendorid == $vend)
- {
- $prod = `cat /sys/bus/usb/devices/$bus-$x/idProduct`;
- if($productid == $prod)
- {
- $ttyusb = `ls /sys/bus/usb/devices/$bus-$x/$bus-$x:1.0/tty/`;
- chomp($ttyusb);
- for($y = 0; $y < $dev; $y++)
- {
- if($equipment{$y}{uport} =~ /$ttyusb/){ $next++; print "YEAY : $equipment{$y}{uport} : $ttyusb : $y\n"; last;}
- }
- if(!$next)
- {
- print "Found Arduino on Port /dev/$ttyusb\n";
- $porty[$dev] = Device::SerialPort->new("/dev/$ttyusb") or print "Can't open Arduino on /dev/$ttyusb";
- $porty[$dev]->read_char_time(0);
- $porty[$dev]->read_const_time(0);
- $porty[$dev]->baudrate(9600); # you may change this value
- $porty[$dev]->databits(8); # but not this and the two following
- $porty[$dev]->parity("none");
- $porty[$dev]->stopbits(1);
- sleep(5);
- while(1)
- {
- $porty[$dev]->purge_all;
- while($tot < 48)
- {
- print "Checking ID\n";
- $porty[$dev]->write("ID\n");
- $porty[$dev]->write("ID\n");
- sleep(1);
- ($count1, $buff) = $porty[$dev]->read(512);
- $tot += $count1;
- $buffer .= $buff;
- if($buffer =~ /IDEND/){ $founddev = 1; last; }
- }
- if($founddev)
- {
- last;
- }
- $tot = 0;
- $count1 = 0;
- $buffer = "";
- }
- ($crap, $deviceattached, $end) = split(/:/, $buffer);
- $deviceattached =~ s/\n\s*//g;
- print "DEVICE: $deviceattached : $ttyusb\n";
- $equipment{$dev}{name} = $deviceattached;
- $equipment{$dev}{uport} = $ttyusb;
- $tot = 0;
- $count1 = 0;
- $buffer = "";
- $dev++;
- last;
- } #!$next
- $next = 0;
- } #if(product
- } #if(vend
- } #if(`grep
- } #for($x
- } #if(vendorid
- } #foreach(usb
- my $chars=0;
- my $buffer="";
- my $timeout=10;
- my $x = 0;
- my $success = 0;
- $start = time();
- #while($count1)
- #{
- # ($count1, $info1) = $porty->read(2048);
- #}
- for($x = 0; $x < $dev; $x++)
- {
- `echo > $equipment{$x}{name}.dat`;
- `echo > /tmp/$equipment{$x}{name}`;
- }
- while(1)
- {
- for($x = 0; $x < $dev; $x++)
- {
- $porty[$x]->purge_all;
- while($tot <512)
- {
- ($count1, $buff) = $porty[$x]->read(512);
- $tot+=$count1;
- $buffer.=$buff;
- if($buffer =~ /END/){ last; }
- }
- @cent = split(/:/,$buffer);
- if($cent[0] =~ /\d+/)
- {
- $cent[0] =~ s/\n\s*//g;
- undef $buffer;
- $currenttime = int((time() - $start));
- open(EQ, ">> $equipment{$x}{name}.dat") or print "Can't open!\n";
- print EQ "$currenttime $cent[0]\n";
- close(EQ);
- `gnuplot $equipment{$x}{name}`;
- sleep(1);
- #close(FILE);
- }
- open(DEVDAT, "/tmp/$equipment{$x}{name}") or print "Equipment data read error /tmp/$equipment{$x}{name}.dat \n";
- @devdat = <DEVDAT>;
- foreach $devline (@devdat)
- {
- if($devline)
- {
- $devline =~ s/\n\s*//g;
- @commands = split(/:/, $devline);
- if($commands[0])
- {
- while(1)
- {
- $porty[$x]->purge_all;
- while($tot < 48)
- {
- print "Sending Command @commands\n";
- $porty[$x]->write("$commands[0]:$commands[1]\n");
- sleep(1);
- ($count1, $buff) = $porty[$x]->read(512);
- $tot += $count1;
- $buffer .= $buff;
- if($buffer =~ /SUCCESS/){ $success = 1; last; }
- }
- if($success)
- {
- print "Success\n";
- last;
- }
- $tot = 0;
- $count1 = 0;
- $buffer = "";
- }
- $success = 0;
- }
- }
- }
- close(DEVDAT);
- `echo > /tmp/$equipment{$x}{name}`;
- undef @devdat;
- undef @commands;
- }#for(x
- }#while(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement