Serial Port Monitor Control Signals Changes

This is a discussion on Serial Port Monitor Control Signals Changes within the Linux Programming forums, part of the Platform Specific Boards category; Hello I'm coding a program that needs to monitor changes in serial port control signals (cts, rts, dtr, dcd), to ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    Serial Port Monitor Control Signals Changes

    Hello

    I'm coding a program that needs to monitor changes in serial port control signals (cts, rts, dtr, dcd), to be more specific I'm interested in cts changes.

    Is there any blocking system call that can be called, and only return when a specified control line is set or unset.

    The only other way I can think of doing it is an infinite loop

    Code:
    int cts_set = 0;
    int status;
    ioctl(fd, TIOCMGET, &status);
    
    if (status & TIOCM_CTS)
        cts_set = 1;
    
    while (do_work)
    {
        ioctl(fd, TIOCMGET, &status);
    
        if ((status & TIOCM_CTS) && (!cts_set))
        {
            // CTS was unset, now set
            cts_set = 1;
            // Do work
        }
        else if ((!(status & TIOCM_CTS)) && (cts_set))
        {
            // CTS was set, now unset
            cts_set = 0;
            // Do work
        }
    
        usleep(200000);
    }
    I know this is not a good way of doing it, and hope there is some sort of blocking system call that would just return when the control signal I'm interested in changes value.

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Normally, CTS is used to signal that it's "clear to send" (That's what CTS stands for), whcih means that if you write() to the port, it won't send the data until CTS is "on". There's really no need for your software to check CTS manually. Unless of course you are using CTS in a non-standard way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Quote Originally Posted by matsp View Post
    Unless of course you are using CTS in a non-standard way.
    Which is the case in my situation. There's no flow control.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Quote Originally Posted by Salem View Post
    Hello Salem

    Thanks for the links, but they don't contain what I'm looking for. Perhaps if I provide further information on why I need this it might help.

    I'm communicating with a smart card reader connected to the serial port. When a smart card is inserted in the reader the CTS line is set. When the card is removed the CTS line is unset. I want to be able to identify when these events occur.

    I want to do it efficiently, so if possible I don't want to waste CPU cycles. Moreover, in my sample code above, during my usleep, the card might be removed and inserted very quickly, when the thread wakes up it will still find the CTS line set, however it would have missed the fact that the card was removed and inserted.

    Thanks again for your help

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > I want to do it efficiently, so if possible I don't want to waste CPU cycles.
    Have you actually tried to measure how much CPU time this loop uses?

    > the card might be removed and inserted very quickly
    Yeah, I'd reduce the timeout to 100000 in the usleep call to make that all but impossible.
    http://en.wikipedia.org/http://cboar...976#post743750
    Serial Port Monitor Control Signals Changes - C Boardwiki/Human%27s_periods

    Short of hacking the driver to provide a "wait for cts change ioctl", the answer you have seems reasonable. Even then, you may end up doing your polling in the driver instead of polling in your program, and be no better off.
    It really depends on the capabilities of the UART in your machine.

    An example discussion is here
    http://xinu.mscs.mu.edu/UART_Driver
    If you read the data sheet, it does indicate that an interrupt CAN be generated on a CTS change.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21