Thread: Problem with TIOCMGET ioctl

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    Problem with TIOCMGET ioctl

    Hi. I am currently porting an application which interfaces with a serial port from Windows to Linux. The two main things done are the setting of DTR and the reading of DSR. I am having a problem, however, reading off the value of DSR from the serial port.

    My current code does it like so:
    Code:
    bool getDSR (void)
    {
    	int controlBits;
    	
    	ioctl(serialFD, TIOCMGET, &controlBits);
    	
    	return controlBits & TIOCM_DSR;
    }
    Problem is that ioctl keeps returning "Bad address" as the error. Does anyone have any idea why this is? Setting the value of DTR works perfectly. I have tried it under two systems, a PIII-era Debian system (2.6.15) and a 2.6.23 Gentoo system.

    Regards, Freddie.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    TIOCMGET is no longer supported.

    http://www.linuxjournal.com/node/7000/print

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    Quote Originally Posted by brewbuck View Post
    TIOCMGET is no longer supported.

    http://www.linuxjournal.com/node/7000/print
    I found the article as well during my googling-frenzy but it seems to be for kernel modules by the looks of it as opposed to user-space applications.

    After a good few hours searching I did find the root cause of the problem, and as usual it was down to my stupidity. I made a dangerous assumption that ioctl returned 0 on failure, so was detecting that the wrong part of the code was failing.

    Once I worked this out it was just a simple error where I forgot an & in front of a variable so was passing it as opposed to its address to ioctl. Why my compiler did not pick up on this I am unsure.

    Sadly my application still has teething trouble, no doubt due to the subtle differences in Linux/Windows serial ports.

    Regards, Freddie.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Once I worked this out it was just a simple error where I forgot an & in front of a variable so was passing it as opposed to its address to ioctl. Why my compiler did not pick up on this I am unsure.
    It's likely that you didn't include the header file that ioctl is in (in which case you should have gotten a warning anyway), or perhaps the prototype for ioctl is K&R-style, not specifying the parameters, something like this.
    Code:
    int ioctl();
    In which case there's nothing you can do about this lack of errors or warnings.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    It's likely that you didn't include the header file that ioctl is in (in which case you should have gotten a warning anyway), or perhaps the prototype for ioctl is K&R-style, not specifying the parameters, something like this.
    Code:
    int ioctl();
    It's actually
    Code:
    int ioctl(int d, int request, ...);
    . ioctl() is a variadic function and way too complex for the compiler to check parameters.

    Yeah -- nearly all POSIX/UNIX functions return 0 on success, -1 on failure, setting errno appropriately. And errno is only set when an error occurs, which means that if an error does NOT occur, that's no guarantee that errno isn't clear.

    Generally you should only check errno IMMEDIATELY after a failed call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM