Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > Software > Perl timing while accessing serial port

Thread Tools

Perl timing while accessing serial port

vrushali vrushali is offline
Junior Member
Join Date: Dec 2010
Posts: 2
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.


use Win32::SerialPort; 
use Time::HiRes qw(usleep);

$PortObj->xon_limit(100);     # bytes left in buffer
$PortObj->xoff_limit(100);    # space left in buffer

$PortObj->read_interval(2);    # max time between read char (milliseconds)
$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->are_match("CorrRsp", ""); # correct response for CMD1

my $gotit = "";
until ("" ne $gotit) {
        $gotit = $PortObj->lookfor(100); #blocking read
        $cmd= "CMD1\n";  # CMD1 Send in loop till CorrRsp is received 
        $microseconds = "600"; 
        usleep ($microseconds); 

$cmd= "CMD2\n";

$mydata1 = $gotit; 

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;

Last edited by vrushali; 01-20-2011 at 09:57 AM..
Reply With Quote
ztech ztech is offline
Junior Member
Join Date: Feb 2011
Posts: 1
Hi, I invoke ( gettimeofday tv_interval ) instead of (usleep).
I have pretty good timing results now. I found out usleep gives up the time slice, which results in long-unwanted waits when milliseconds do matter.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reduce CPU time while polling serial port jis Perl Misc 4 09-08-2007 12:37 AM
emulate a serial port in windows (create a virtual 'com' port) Pom Python 2 01-31-2007 07:49 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
Re: Serial port and PS/2 port schematics OR Assistive Tech. suggestion Computer Support 1 07-10-2003 11:46 AM
Re: Serial port and PS/2 port schematics °Mike° Computer Support 1 07-09-2003 10:30 PM