Perlmonks,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.