My problem is the following: I have a script that slurps a text file into an array and sends data to a serial port. The data slurped in contains a timecode and data.
The timecode is in milliseconds.
I want to ensure that my data is sent to the serial port at exact times as specified.
Example:
- slurp in file that has the following:
0035020 (first five are timecode, 350ms for this line)
0029024 (last 2 are data to be sent, 24 for this line)
1042201
- send the data "20" over the com port
- wait 350 ms
- send the data "24" over the com port
- wait 290 ms
- send the data "1" over the com port
- wait 10422 ms
the following code shows what I'm doing, but somehow the times are getting off sync...
Is there something I'm overlooking?
use warnings; use Win32::SerialPort; use Time::HiRes qw (usleep); open (CODES, "$ARGV[0]"); my @codes = <CODES>; close CODES; my $serial = new Win32::SerialPort ('COM3') or die "Can't open COM3: $ +^E\n"; $serial->databits(8); $serial->baudrate(9600); $serial->parity("none"); $serial->stopbits(1); $serial->write_settings; my $clearout = $serial->input; undef $clearout; my $correctionUs = 13800; //setting to offset the transfer time of s +erial communication, not working print "Hit Enter To Begin"; <STDIN>; my $starttimeepoch = time; my $starttime = localtime; print "Start Time is: $starttime, epoch time is: " . $starttimeepoch . + "\n"; print "Correction is: " . $correctionUs . "\n"; foreach my $line (@codes){ $line =~ /(\d{5})(\d{2})/; $serial->write($2); usleep(($1 * 1000 )- $correctionUs); } my $endtimeepoch = time; my $endtime = localtime; print "End Time is: $endtime, epoch time is: " . $endtimeepoch . "\n"; print "Total time is: " . ($endtimeepoch - $starttimeepoch) . "\n"; print "I should be done now, sleeping for 10 seconds\n"; sleep(10); $serial->close or die "Failed to close COM3"; undef $serial;
There must be some blocking or something going on, the first hundred or so lines send well, then they get off-sync, then after some time, they'll resync by themselves.
Any ideas?
In reply to Serial Port timing and write blocking? by worldsight
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |