Results 1 to 20 of 47

Thread: serial port programming in qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: serial port programming in qt

    compiled successfully.but still the same error comes .
    I guess you mean the segmentation fault.

    Your code has the following problems:
    Qt Code:
    1. QextSerialPort *note= new QextSerialPort("/dev/ttyS0");
    To copy to clipboard, switch view to plain text mode 
    You are allocating the serial port on to a local pointer.
    You will not be able to access the serial port outside the constructor.
    'note' needs to be a member variable.

    Where is the code that handels the reading/writing to the serial port?

  2. #2
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: serial port programming in qt

    theLSB:
    I did not add the read/write operation. i thougth first let me open the port sucessfuly. then,read that.

    'note' needs to be a member variable
    Do you mean that i need to declare it under protected and use it in the constructor ?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: serial port programming in qt

    Do you mean that i need to declare it under protected and use it in the constructor ?
    No, you need to declare it as a member variable.
    Do you know what a member variable is?
    Qt Code:
    1. class Widget : public QWidget
    2. {
    3. private: //depends on your needs, this can be any access mode
    4. QextSerialPort *m_note;
    5. public:
    6. Widget( QWidget *parent=0, const char *name=0 );
    7. private:
    8. int mouse;
    9. int mouseidx;
    10. };
    11.  
    12. Widget::Widget( QWidget *parent, const char *name )
    13. : QWidget( parent, name),
    14. m_note(NULL)
    15. {
    16. setMinimumSize(640,480 );
    17.  
    18. m_note= new QextSerialPort("/dev/ttyS0");
    19. if(note)
    20. {
    21. m_note->setBaudRate(BAUD115200);
    22. m_note->setParity(PAR_NONE);
    23. m_note->setDataBits(DATA_8);
    24. m_note->setStopBits(STOP_1);
    25. m_note->open(IO_ReadOnly);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 7th May 2007 at 13:46.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: serial port programming in qt

    high_flyer:

    yeah i did as you said. i tried both the ways-private or protected. But no use.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: serial port programming in qt

    But no use.
    Could you be a bit more descriptive?
    The access mode has nothing to do with it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: serial port programming in qt

    first i tried declaring the Qextserialport as a private member variable, compiled and when i rum the same error came. next i tried declaring it as protected member variable, the same thing happened.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: serial port programming in qt

    what error? segmentation falut?
    Are there any other methods do the Widget class?
    Try setting debug messages along the code or run in a debugger and see where the applciation crashes.

    EIDT:
    Wait, in the code I edited for you, your should change all 'note' instances to 'm_note' did you do that?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: serial port programming in qt

    Hi,

    i solved that problem. i did not declare it in the heap.The following is what i did:
    Qt Code:
    1. QextSerialPort note("/dev/ttyS0");
    2. note.setBaudRate(BAUD115200);
    3. note.setParity(PAR_NONE);
    4. note.setDataBits(DATA_8);
    5. note.setStopBits(STOP_1);
    6. note.open(IO_ReadOnly);
    To copy to clipboard, switch view to plain text mode 
    With this the application opens without segmentation fault. Now i'm finding out how to read the datas ? the suggestion b1 gave did not work out as there is no member function called read in the Qextserialport.

    Anyway thanks a lot for all of your kind response .

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: serial port programming in qt

    What you did is no good.
    The serial port gets created on the stack, and dies when the constructor ends.
    Use the code I gave you.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: serial port programming in qt

    high_flyer:

    Oops. it still not working. This is my code:

    ConnectWidget::ConnectWidget( QWidget *parent, const char *name )
    : QWidget( parent, name),m_note(NULL)
    {
    setBackgroundColor( white );
    setMinimumSize(640,480 );

    m_note= new QextSerialPort("/dev/ttyS0");
    if(m_note)
    {
    m_note->setBaudRate(BAUD115200);
    m_note->setParity(PAR_NONE);
    m_note->setDataBits(DATA_8);
    m_note->setStopBits(STOP_1);
    m_note->open(IO_ReadOnly);
    }
    printf("\n serial port openned \n");
    }

    Is this what you suggested?

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: serial port programming in qt

    what error? segmentation falut?
    Are there any other methods do the Widget class?
    Try setting debug messages along the code or run in a debugger and see where the applciation crashes.
    And please use code tags.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: serial port programming in qt

    Sir,

    I modified the code. Now i have onely three lines in my code:

    Qt Code:
    1. class ConnectWidget : public QWidget
    2. {
    3. public:
    4. ConnectWidget( QWidget *parent=0, const char *name=0 );
    5. private:
    6. QextSerialPort *m_note;
    7. };
    8. ConnectWidget::ConnectWidget( QWidget *parent, const char *name )
    9. : QWidget( parent, name),m_note(NULL)
    10. {
    11. setMinimumSize(640,480 );
    12. m_note= new QextSerialPort("/dev/ttyS0");
    13. printf("\n serialport openned \n");
    14. }
    To copy to clipboard, switch view to plain text mode 

    I have not added the settings of the serial port.As i found that starting from this line the error pops up. the following is the error message:

    suse:/home/qtprograms/roughserial # ./roughserial -qws
    Connected to VFB server: 640 x 480 x 32
    Segmentation fault
    suse:/home/qtprograms/roughserial #

Similar Threads

  1. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  2. First attempt to display serial port data on GUI
    By ShaChris23 in forum Newbie
    Replies: 12
    Last Post: 4th May 2007, 09:14
  3. Replies: 12
    Last Post: 23rd March 2007, 09:23
  4. Serial Port access in Qt
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 18th October 2006, 21:03
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Qt is a trademark of The Qt Company.