Perl timing while accessing serial port

Discussion in 'Software' started by vrushali, Jan 20, 2011.

  1. vrushali

    vrushali

    Joined:
    Dec 9, 2010
    Messages:
    2
    Timing issue while accessing serial port

    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.

    Code:
       
    
    #!/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 (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->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;
    Last edited: Jan 20, 2011
    vrushali, Jan 20, 2011
    #1
    1. Advertising

  2. vrushali

    ztech

    Joined:
    Feb 15, 2011
    Messages:
    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.
    ztech, Feb 15, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Faustino Dina
    Replies:
    2
    Views:
    2,330
    Hansang Bae
    Aug 18, 2004
  2. °Mike°

    Re: Serial port and PS/2 port schematics

    °Mike°, Jul 6, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    5,175
    naive.verizon@locality.net
    Jul 9, 2003
  3. naive.verizon@locality.net

    Re: Serial port and PS/2 port schematics OR Assistive Tech. suggestion

    naive.verizon@locality.net, Jul 9, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    1,142
    Ralph Wade Phillips
    Jul 10, 2003
  4. Waltjones40
    Replies:
    1
    Views:
    398
    flamer die.spam@hotmail.com
    May 25, 2007
  5. business one way

    Is it a Perl program or a Perl script?

    business one way, Jan 5, 2008, in forum: Digital Photography
    Replies:
    0
    Views:
    721
    business one way
    Jan 5, 2008
Loading...

Share This Page