Hi, I am using RS-232 to communicate between PC and a device using UARTs. I am using a script in the PC that, keeps sending commands (CMD1 in a loop) to the Device till it responds with the right response (CorrRsp). On receiving this response subsequent commands (CMD2) are sent to the device. The device has stringent timing constraints and must receive CMD2 within 20 ms from the time it sends the CorrRsp to the PC. The issue is I am not able to get consistent results. Some times CMD2 is received at the device end within 20 ms but sometimes not. The script used and the test results that shows the inconsistency with the timing are copied here for reference. Please help me understand if the inconsistency is due to the script or the OS. As the device is tested and certified as stable.
#!/usr/bin/perl use Win32::SerialPort; use Time::HiRes qw(usleep); $com="COM1"; $PortObj=Win32::SerialPort->new($com); $PortObj->baudrate(38400); $PortObj->databits(8); $PortObj->parity("none"); $PortObj->stopbits(1); $PortObj->handshake("xoff"); $PortObj->xon_limit(100); # bytes left in buffer $PortObj->xoff_limit(100); # space left in buffer $PortObj->xon_char(0x11); $PortObj->xoff_char(0x13); $PortObj->read_interval(2); # max time between read char (milliseco +nds) $PortObj->read_char_time(1); # avg time between read char $PortObj->read_const_time(2); # total = (avg * bytes) + const #writes the settings into the port $PortObj->write_settings; $PortObj->are_match("CorrRsp", ""); # correct response for CMD1 my $gotit = ""; until ("" ne $gotit) { $gotit = $PortObj->lookfor(100); #blocking read if($gotit){last;} $cmd= "CMD1\n"; # CMD1 Send in loop till CorrRsp is received $PortObj->write($cmd); $microseconds = "600"; usleep ($microseconds); } $cmd= "CMD2\n"; $PortObj->write($cmd); print("$gotit\n"); $mydata1 = $gotit; $PortObj->close; undef $PortObj;
Test results for time difference between CMD1 and CorrRsp: Test1: 116 ms; test2: 105 ms; test3: 69 ms; test4: 79 ms; test5: 97 ms; test6: 69 ms; test7: 99 ms; test8: 76 ms;

In reply to Timing issue while accessing serial port by vrn

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.