Page 1 of 2 12 LastLast
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
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: serial port programming in qt

    segfaults are caused by very bad mistakes in code . So we need to see the code before we can give you a solution.

  2. #2
    Join Date
    Jun 2006
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: serial port programming in qt

    sar_van81

    For what its worth I have used QextSerialPort and it works well.
    Below is the snippet of code used to setup the port. "portname1" is determined by the OS being used at the time.

    Hope this is of some use to you....

    Qt Code:
    1. void Progarm::setupSerial()
    2. {
    3. serialPort = new QextSerialPort( portname1 );
    4. serialPort->setBaudRate( QextSerialPort::BAUD9600 );
    5. serialPort->setDataBits( QextSerialPort::DATA_8 );
    6. serialPort->setStopBits( QextSerialPort::STOP_1 );
    7. serialPort->setParity( QextSerialPort::PAR_NONE );
    8. serialPort->setFlowControl( QextSerialPort::FLOW_OFF );
    9. serialPort->setTimeout( 0, 500 );
    10. serialPort->open( QIODevice::ReadWrite);
    11. if (!serialPort->isOpen() )
    12. {
    13. QMessageBox::warning( this, "Connection Error",
    14. "Could not open the serial port\n"
    15. "\n"
    16. "Please check connections and try again\n" );
    17. label->setText( "Serial port error" );
    18. return;
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Regards, B1.

  3. #3
    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

    B1:

    the following is my code:
    Qt Code:
    1. Widget::Widget( QWidget *parent, const char *name )
    2. : QWidget( parent, name )
    3. {
    4. setMinimumSize(640,480 );
    5. Posix_QextSerialPort *note= new Posix_QextSerialPort("/dev/ttyS0");
    6. note->setBaudRate(Posix_QextSerialPort::BAUD115200);
    7. note->setParity(Posix_QextSerialPort::PAR_NONE);
    8. note->setDataBits(Posix_QextSerialPort::DATA_8);
    9. note->setStopBits(Posix_QextSerialPort::STOP_1);
    10. note->open(IO_ReadOnly);
    11. int n;
    12. n=note->bytesWaiting();
    13. if(n!=0)
    14. printf("\nreading the buffer datas \n");
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    .
    Also can you say me how to read the datas from the serial port . i saw that only int Posix_QextSerialPort::getch() is available. is there any function to read a block of datas for example say 10bytes ?

    saravanan

  4. #4
    Join Date
    Jun 2006
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: serial port programming in qt

    sar_van81,

    This is what I use in a simple app...bear in mind I am still learning too!!

    Qt Code:
    1. void Program::dataListener()
    2. {
    3. // see how many bytes available from the serial port
    4. rec = serialPort->bytesAvailable();
    5. if (rec > 0 )
    6. {
    7. if ( rec > 256 ) rec = 256;
    8. z = serialPort->read( buffer, rec);
    9. buffer[z] = '\0';
    10. tempmsg = buffer;
    11. }
    12. dispData();
    13. }
    To copy to clipboard, switch view to plain text mode 

    There may be (and probably are!) better ways of doing this but for me it did what I wanted. You can also use serialPort->getChar(buffer) and read the serial port character by character and then process the buffer accordingly.

    Hope this helps.

    B1.

  5. #5
    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

    B1:

    Ok. let me try this.but the application does not open successfully.it prompts me for "segmenattion fault error".if i comment the following lines:

    Qt Code:
    1. Posix_QextSerialPort *note= new Posix_QextSerialPort("/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 
    .

    the program executes successfully. Did you experience any problem like this ?

    Am i missing some thing more ?

  6. #6
    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

    Why are you commenting out these lines? with out them you don't have an initialized serial port objet.
    Also, QextSerialPort is made crossplatform, so you don't need to explicitly use Posix_QextSerialPort (this wont compile under windows) - there are defines in the code that will select the correct version for you.
    Just use QextSerialPort.
    ==========================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.

  7. #7
    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:

    if i use QextSerialPort instead of Posix_QextSerialPort, it shows me the following error:

    home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:11:21: error: windows.h: No such file or directory
    /home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:57:7: warning: no newline at end of file
    In file included from tux.cpp:21:
    /home/qtprograms/qwt/qextserial/qextserialport-0.8.0/qextserialport.h:26:7: warning: no newline at end of file
    /home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:50: error: ‘HANDLE’ does not name a type
    /home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:51: error: ‘COMMCONFIG’ does not name a type
    /home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:52: error: ‘COMMTIMEOUTS’ does not name a type
    make: *** [tux.o] Error 1
    .
    So only i used Posix_QextSerialPort. Should i define somewhere about _TTY_POSIX ? Also as i said before when i include them in my code its prompting me segmentation fault.

  8. #8
    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

    So only i used Posix_QextSerialPort. Should i define somewhere about _TTY_POSIX ?
    Exactly - You should add DEFINES += _TTY_POSIX in your pro file (or if you are using KDevelop it has a field for that in the qmake configuration).
    ==========================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.

  9. #9
    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:

    that parameter has already been set when i untar the qextserial package. should i specify that in my application pro file ?

    Let me explain what i have done so far :

    1). Downloaded the qextserial package , untarred and entered qmake ,make. there were the following libraries created:
    libqextserialport.so libqextserialport.so.1 libqextserialport.so.1.0 libqextserialport.so.1.0.0

    2). Then created a directory ,copied my source files. entered the header files and the libraries for qextserial in my project file. then entered make.

    is this procedure correct ? else can you say me the correct one ?

  10. #10
    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

    should i specify that in my application pro file ?
    yes.
    is this procedure correct ? else can you say me the correct one ?
    It should work.
    But I would not copy the header files and libs, I would link to them or user the -L and -I in my make file.
    But that is a subjective private taste matter, your way should work just as well.
    ==========================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.

  11. #11
    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 specified in my project file also and it compiled successfully.but still the same error comes . Is there any permission i need to set to serial port ? i'l post my full code here if any one find any error please point me :

    Qt Code:
    1. #include <qwidget.h>
    2. #include <qapplication.h>
    3. #include <stdlib.h>
    4.  
    5. #include <qextserialport.h>
    6.  
    7. class Widget : public QWidget
    8. {
    9. public:
    10. Widget( QWidget *parent=0, const char *name=0 );
    11. private:
    12. int mouse;
    13. int mouseidx;
    14. };
    15.  
    16. Widget::Widget( QWidget *parent, const char *name )
    17. : QWidget( parent, name)
    18. {
    19. setMinimumSize(640,480 );
    20.  
    21. QextSerialPort *note= new QextSerialPort("/dev/ttyS0");
    22. note->setBaudRate(BAUD115200);
    23. note->setParity(PAR_NONE);
    24. note->setDataBits(DATA_8);
    25. note->setStopBits(STOP_1);
    26. note->open(IO_ReadOnly);
    27. }
    28.  
    29. int main( int argc, char **argv )
    30. {
    31. QApplication a( argc, argv );
    32. Widget connect1;
    33. a.setMainWidget( &connect1 );
    34. connect1.show();
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 7th May 2007 at 10:54. Reason: missing [code] tags

  12. #12
    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?

  13. #13
    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 B1,

    DId you experience any segementation problem in creating the serialport in heap like mine ? i tried your way of declaring also ? but i still could not make it ?

  14. #14
    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

    I have not added the settings of the serial port.As i found that starting from this line the error pops up.
    Do you mean that the program crashes on:
    m_note->setBaudRate(BAUD115200); ?

    This code should run.
    How did you discover on which line the program crashes, what method did you use to debug?
    ==========================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.

  15. #15
    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:

    No , the program crashes in the first line itself. i mean :

    m_note= new QextSerialPort("/dev/ttyS0");

    I had only this line in my constructor ( as i had posted in the above post). i did not have any other line.

  16. #16
    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

    I had only this line in my constructor ( as i had posted in the above post). i did not have any other line.
    You mean the only line of code in ConnectWidget constructor right?
    Does it run if you remove that line as well?
    Does it run when this construcotr is empty?
    Are you sure you are not trying to use a 'note' or 'm_note' somehwere else in your code?
    It would be good if you could post you mail() function.
    The problem in your code is not the parts we currently see.
    Ofcourse there is also the chance that the QextSerialPort lib is faulty for some reason on your system.
    ==========================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.

  17. #17
    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

    yeah, it runs if i comment that line and place a printf there.And i'm sure i dont use that anywhere in my code.
    It would be good if you could post you mail() function.
    I have not used any thing like this function .

    Also i'm using qextserialport-0.8.0 package since only this supports qt-3.3.5 version. remaining packages such as qextserialport-0.9.0,1.0,1.1 supports higher versions of qt. will there be any problem with this version changes ?

  18. #18
    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

    I have not used any thing like this function .
    Sorry, I meant main().

    yeah, it runs if i comment that line and place a printf there.And i'm sure i dont use that anywhere in my code.
    Hmm... then from what you have said so far, it all points to a problem in your QextSerialPort lib.
    Also i'm using qextserialport-0.8.0 package since only this supports qt-3.3.5 version. remaining packages such as qextserialport-0.9.0,1.0,1.1 supports higher versions of qt. will there be any problem with this version changes ?
    What do you mean?
    using a Qt4 QextSerialPort with Qt3?
    That wont work.
    ==========================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.

  19. #19
    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

    The following in my main():

    Qt Code:
    1. int main( int argc, char **argv )
    2. {
    3. QApplication a( argc, argv );
    4. ConnectWidget connect1;
    5. a.setMainWidget( &connect1 );
    6. connect1.show();
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    What do you mean?
    using a Qt4 QextSerialPort with Qt3?
    That wont work.
    No i mean i'm using qt-3.3.5 version and the other versions of qextserialport requires header and libraries of qt 4 or greater version.

  20. #20
    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

    Well, from what I can see, there is no problem with the code it self.

    You can only link QextSerialPort for Qt3 (which is linked against Qt3) with Qt3 applications.
    You can only link QextSerialPort for Qt4 (which is linked against Qt4) with Qt4 applications.
    ==========================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.

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.