Results 1 to 7 of 7

Thread: Serial Port Communication

  1. #1
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Serial Port Communication

    I'm trying to communicate to a serial port RF receiver (specifically the W800RF32A) which is supposed to receive events from wireless X10 devices.

    Within the function that is supposed to read the port I have:
    Qt Code:
    1. ErrorText->setText("Clicked");
    2.  
    3. string line;
    4. ifstream myfile ("/dev/ttyS0");
    5.  
    6. ErrorText->append("Created file object...");
    7.  
    8. if (myfile.is_open())
    9. {
    10. ErrorText->append("file is open...");
    11.  
    12. while ( !myfile.eof() )
    13. {
    14. ErrorText->append("not at end...");
    15. // getline (myfile,line);
    16. ErrorText->append("got line...");
    17. ErrorText->append(line);
    18. cout << line << endl;
    19.  
    20. }
    21.  
    22. ErrorText->append("End of file, closing");
    23. myfile.close();
    24. ErrorText->append("closed");
    25.  
    26. }
    27.  
    28. else cout << "Unable to open file";
    To copy to clipboard, switch view to plain text mode 

    You'll notice the commented getline function. The code can compile and run fine as long as I don't have the getline or similar function in there (and I've tried almost every alternative I could find for "getlin" because I know getline isnt going to work [no terminating character etc]).

    Now when I look around for QT and stuff about serial ports everything is all QextSerial or some other class, but I've been told on linux you don't need that and you can simply read the serial port like a text file....... hence the above approach.

    (btw I have a version of the above that uses QFile, it too behaves the same way)

    So my questions:
    1)Do I need another class to handle the serial port in linux?
    2)If not, then what function should I use to get the incoming characters?? (to replace the commented line)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serial Port Communication

    Quote Originally Posted by soldstatic
    1)Do I need another class to handle the serial port in linux?
    You can use QExtSerial or QFile

    2)If not, then what function should I use to get the incoming characters?? (to replace the commented line)
    QFile::getch() or fgetc()

    If you don't have line terminators and you don't know the length of input, you have to read char by char. Of course if you do know the length, you can use QFile::readLine() and limit the number of characters to read.

  3. The following user says thank you to wysota for this useful post:

    soldstatic (22nd June 2006)

  4. #3
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication

    Quote Originally Posted by wysota
    You can use QExtSerial or QFile


    QFile::getch() or fgetc()

    If you don't have line terminators and you don't know the length of input, you have to read char by char. Of course if you do know the length, you can use QFile::readLine() and limit the number of characters to read.
    I didn't really want to use the another class, so I was going for the file approach. I have a Qfile version of the above, but i'll post that if I can't get getch to work.

    good point w/ the readline and limiting the number of characters.

  5. #4
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication

    Here's my other attempt with the QFile and getch.

    Qt Code:
    1. void Tech::pushButton1_clicked()
    2. {
    3. {
    4. QFile file( "/dev/ttyS0" ); // Read the text from a file
    5. if ( file.open( IO_ReadOnly ) ) {
    6. ErrorText->append( "File opened, trying to read...");
    7. QTextStream stream( &file );
    8. QString line;
    9. char *a;
    10.  
    11. while(!file.atEnd())
    12. {
    13. a = file.getch();
    14. line.append( a );
    15. }
    16. file.close();
    17. ErrorText->append( line) ;
    18. ErrorText->append( "File closed");
    19. }
    20. else
    21. {
    22. ErrorText->setText("Could not open file.");
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Not sure why but it gives me compile error:
    Qt Code:
    1. X10.ui.h:247: error: invalid conversion from `int' to `char*'
    To copy to clipboard, switch view to plain text mode 

    I'm sure that compile error is somethin stupid though... Still, if you see what's givin me that error lemme know please... :-/

    Thanks muchos for the help guys, this has been frustrating me forever.. and by forever i mean almost 4 weeks...
    Last edited by soldstatic; 21st June 2006 at 21:49.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serial Port Communication

    getch returns an int (which is castable to char). The one below is fine:

    Qt Code:
    1. char character = file.getch();
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to wysota for this useful post:

    soldstatic (22nd June 2006)

  8. #6
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication

    oh duh thanks. i knew it was dumb. i had it cast as a char * and not char.
    thanks!

    i'll test it out tomorrow and let you know!

  9. #7
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication

    OK the getch worked with Qfile. Using a null modem cable hooked up to another computer it worked just fine. Now I just need to figure out how to call the function when there is information there that isn't null and how to get this serial device to actually work. At least my program is functional now as far as pure serial communication is concerned.

    Thanks alot I really appreciate it!!!

    -Andy

    Qt Code:
    1. QFile file( "/dev/ttyS0" ); // Read the text from a file
    2. if ( file.open( IO_ReadOnly ) ) {
    3. ErrorText->append( "File opened, trying to read...");
    4. QTextStream stream( &file );
    5. QString line;
    6. char a;
    7. ErrorText->append( "made everything at end?");
    8. while(!file.atEnd())
    9. {
    10. ErrorText->append( "Not at end, reading and appending...");
    11. a = file.getch();
    12. line.append( a );
    13. std::cout << a;
    14. }
    15. ErrorText->append( "at end, closing...");
    16. file.close();
    17. ErrorText->append( line) ;
    18. ErrorText->append( "File closed");
    19. }
    20. else
    21. {
    22. ErrorText->setText("Could not open file.");
    23. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. C++ Library for serial communication
    By dec0ding in forum General Programming
    Replies: 7
    Last Post: 8th July 2007, 19:18
  2. trayicon port to QT4
    By ykohn in forum Qt Programming
    Replies: 10
    Last Post: 21st April 2006, 16:11
  3. Replies: 16
    Last Post: 7th March 2006, 16: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.