#!/usr/bin/perl use warnings; use strict; use Device::SerialPort; my $scriptfile = "cnc_surfacing.gcode";#"AllObjects.ngc";#"cnc_run.gcode"; $scriptfile = "cnc_reproduce2.gcode";#"AllObjects.ngc";#"cnc_run.gcode"; #$scriptfile = "cnc_short.gcode";#"AllObjects.ngc";#"cnc_run.gcode"; # Set up the serial port # 19200, 81N on the USB FTDI device my $port = Device::SerialPort -> new("/dev/ttyUSB0"); $port->databits(8); $port->baudrate(19200); $port->parity("none"); $port->stopbits(1); my @cmd = ("G00X01","G00X0","G00Z-10","G00Z+10"); my $rcv = ""; my $continue=0; if ($#ARGV>=0) { foreach my $arg (@ARGV) { sleep(5); chomp(my $out = ($arg)); printf "$out\n"; my $count_out = $port->write("$out\n"); print "Sent character: $out\n"; sleep(1); } } else { open(GCODE,'<',"$scriptfile") or die "cannot open $scriptfile"; chomp( my @script = ); close(GCODE); my $nl = $#script + 1; print "there are $nl lines of script\n"; #return; my $count = 0; while ($count < $#script) { # Poll to see if any data is coming in my $char = $port->lookfor(); # If we get data, then print it # Send a number to the arduino if ($char) { $rcv = "$rcv$char"; print "Recieved character: " . $char . " \n"; my @spl = split(/\n/,$rcv); my $rcv = $spl[$#spl]; print "buffer: " . $char . " \n"; if($rcv =~ m/zomfg/) { $continue = 1; } if($rcv =~ m/ok/) { $continue = 1; } } else { if ($continue > 0) { sleep(1); $count++; my $count_out = $port->write("$script[$count]\n"); print "Sent character: $script[$count] \n"; $continue = 0; } } } } print "End of Script\n";