Page 1 of 3 123 LastLast
Results 1 to 20 of 47

Thread: serial port programming in qt

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

    Default serial port programming in qt

    hi ,

    i need to develop an application in qt such that it reads the datas whenever data comes from the serial port.Can i use QFile for this ? or is there any other classes.

    thanks in advance,

    saravanan

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Default Re: serial port programming in qt

    yeah , i googled regarding that and i also downloaded the Qextserial package and compiled that for my linux box too. but after that i dnt know how to use it ? i mean there is no executables created. there are only libraries created.i dnt know what to do with that created libraries. i also checked in the link you sent. there only for windows version of qextserial port is given.


    can anybody say me how to use it ?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: serial port programming in qt

    QExtSerial is a library which you can use in your application.
    I did not used it, but they should have API documentation on their site.

    Probably you will have to include some headers and the port initialization and communication should be pretty straightforward.

    regards

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

    Default Re: serial port programming in qt

    hi,

    i had downloaded and compiled that for my host. But there are only library files created. also after there is no documentation explaining as how to use this with the qt or qwt package. i mean what should be done with this library etc? so i tried my own way. the following is my code:
    Qt Code:
    1. sample::sample()
    2. {
    3. mouse=open("/dev/ttyS0",O_RDONLY | O_NDELAY);
    4. cout<<" mouse:"<<mouse<<endl;
    5.  
    6. note =new QSocketNotifier(mouse,QSocketNotifier::Read,this);
    7. connect(note,SIGNAL(activated(int)),this,SLOT(data()));
    8. }
    9.  
    10. void sample::data()
    11. {
    12. n=read(mouse,buffer,1);
    13. printf("n:%d\n",n);
    14. for(int i=0;i<n;i++)
    15. printf("buffer:%d\t",buffer[i]);
    16. cout<<"end of reading"<<endl;
    17. }
    To copy to clipboard, switch view to plain text mode 
    .
    this is working when i send some characters from another system connected to this serial port. but when i connect a device to the serial port. the datas are not read successfully. The characteristics of the device is this: it will send some 5 byte data to the serial port. i need to read that datas as soon as it comes in the serial port.


    can anyone say me why is that when we i enter the datas are read correctly and when a device sends its not recieved correctly?

    can anyone provide me suggestions or solutions for this ?Am i correct ? or is there anyother way for reading the serial port as the datas come ? also if anyone knows how to use the qextserial can you send me the links or docs regarding that ?

  6. #6
    Join Date
    Jan 2006
    Location
    Sofia, Bulgaria
    Posts
    24
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: serial port programming in qt

    I still can't get it why you aren't using syscalls for this issue. I haven't programmed any sockets/ports in Windows, but I can give you enough hints for any posix-complient environment (unixes: aix, hpux, etc., linux, *bsds, probably mac os).

    If you want to attatch to a device, use the open() function to open the file (the old Unix filosophy that everything is a file works perfectly here).
    To read some data, use read().
    To poll for available data, use select().
    To close the buffer when it's not available anymore, use close().
    There is a nice example of using select in its manual page, at least for a linux environment.

    I do usually wrap the logic using external devices in separate threads and communicate with signals (Qt4's emit is thread-safe, to my knowleage). A simple example is the following:

    Qt Code:
    1. class my_mouse_class: public QThread
    2. {
    3. Q_OBJECT
    4. private:
    5. int f_mouse;
    6.  
    7. protected:
    8. virtual void run() //logic here. poll for data, if found, emit a signal.
    9. {
    10. fd_set rfds;
    11. struct timeval tv;
    12. int retval;
    13.  
    14. while ( isRunning() )
    15. {
    16. /* Watch stdin (fd 0) to see when it has input. */
    17. FD_ZERO(&rfds);
    18. FD_SET(0, &rfds);
    19.  
    20. /* Wait up to five microseconds. */
    21. tv.tv_sec = 0;
    22. tv.tv_usec = 5;
    23.  
    24. retval = select( f_mouse, &rfds, NULL, NULL, &tv );
    25. /* Don’t rely on the value of tv now! */
    26.  
    27. if ( retval == -1 )
    28. {
    29. perror( "select()" );
    30. }
    31. else
    32. {
    33. if ( retval )
    34. {
    35. printf( "Data is available now.\n" );
    36. /* FD_ISSET(0, &rfds) will be true. */
    37. emit data_available();
    38. }
    39. else
    40. {
    41. printf( "No data within five seconds.\n" );
    42. }
    43. }
    44. }
    45. }
    46.  
    47. public:
    48. /**
    49.   Basic constructor, tries to open the device.
    50.   */
    51. my_mouse_class( QObject * parent ): QThread( parent )
    52. {
    53. f_mouse = open( "/dev/mouse", O_RDONLY );
    54. if ( f_mouse < 3 ) // 1 and 2 are standart output and error output
    55. {
    56. throw DeviceException( "my_mouse_class::my_mouse_class(): Mouse not available." );
    57. }
    58. }
    59.  
    60. ~my_mouse_class()
    61. {
    62. if ( f_mouse > 2 ) //sanity check
    63. {
    64. close( f_mouse ); //close the file descriptor
    65. }
    66. }
    67.  
    68. //read some data
    69. ssize_t read( void * buffer, ssize_t count )
    70. {
    71. return ::read( f_mouse, buffer, count );
    72. }
    73.  
    74. signals:
    75. void data_available();
    76. };
    To copy to clipboard, switch view to plain text mode 

    I have omitted the headers since they could be retrieved upon issuing the help on each of the functions, presenting in its dedicated manual page (for example, to read the full help of select, use "man select"). Please note that the above example contains code from the glibc's manual page, which is licensed under LGPL, so if you are using it in a commersial application, please modify it.

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

    Default Re: serial port programming in qt

    hi everyone ,

    i had compiled my application with the qextserialport package by specifying the INCLUDEPATH and LIBS in the project file(".pro"). but when i execute it , it says "Segmentation fault " error.

    can anyone provide me any suggestions or solutions for this ?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    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.

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

    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.

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

    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

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

    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.

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

    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 ?

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

    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.

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

    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.

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

    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.

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

    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 ?

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

    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.

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

    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

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

    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?

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

    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 ?

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.