Results 1 to 11 of 11

Thread: error on closing serial port

  1. #1
    Join Date
    Jun 2011
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default error on closing serial port

    Hi...
    I have successfully open serial port and also able to read and write data on serial port with the following code:
    int fd;
    struct termios options;
    fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY)
    ;

    But when i try to close serial port with close(fd) function,my application has been crashed and it gives segmentation fault.

    So please tell me that how can i close serial port without my app crash..
    Waiting for quick reply

    Thanks..

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    We cannot help you if you don't show your code.

    Are you sure that open () doesn't fail?
    Qt Code:
    1. fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
    2. if (-1 == fd) {
    3. // open failed!!
    4. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jun 2011
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    Yes mcosta, I am damm sure that open function doesn't fail becoz it returns non-negative value.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    ok,

    can you show the code that crashes?
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Jun 2011
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    For open port:-
    int fd = 0;
    struct termios options;
    tcgetattr(fd, &options);
    fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
    fcntl(fd,F_SETFL,FNDELAY);
    cfsetispeed(&options,B9600);
    cfsetospeed(&options,B9600);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_lflag &= ~(ICANON | ECHO | ISIG);
    tcsetattr(fd, TCSANOW, &options);

    For close port:-
    close(fd);

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: error on closing serial port

    Maybe you have some kind of memory corruption problem which is causing the close call to crash?

  7. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    have you checked the value of "fd" just before call "close"?
    A camel can go 14 days without drink,
    I can't!!!

  8. #8
    Join Date
    Jun 2011
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    yes i have checked the value of fd.It gives non-negetive value when i call close(fd).

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    try to debug your application and see how and why the program crash. post the debug output.
    A camel can go 14 days without drink,
    I can't!!!

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: error on closing serial port

    Quote Originally Posted by amitpatel22 View Post
    yes i have checked the value of fd.It gives non-negetive value when i call close(fd).
    But is this the same value as returned by open?

  11. #11
    Join Date
    Jun 2011
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: error on closing serial port

    Thanks to all for your support..

    I think i got the solution..

    I have tried ::close(fd) instead of close(fd) and it works..

    Thanks..

Similar Threads

  1. serial port communication
    By robotics in forum Qt Programming
    Replies: 19
    Last Post: 28th September 2011, 15:11
  2. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  3. serial port communiction
    By jagadish in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2007, 11:52
  4. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05
  5. Serial Port access in Qt
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 18th October 2006, 21:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.