Results 1 to 5 of 5

Thread: data from serial port

  1. #1
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default data from serial port

    hi all,

    i'm newbie in qt, and i have to make a program in qt to read data from usb device, digital compass to be more precisely

    and this is the part of my code

    in .h file
    Qt Code:
    1. private:
    2. char buf[256];
    3. QString line
    To copy to clipboard, switch view to plain text mode 

    in .cpp
    Qt Code:
    1. while(running) {
    2. res = read(fd, buf, 255);
    3. if(res > 1) {
    4. buf[res-1] = 0;
    5. line = buf;
    6. emit newData(line);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    but when i try to run it i got message : "The program has unexpectedly finished"
    when i try to comment the "emit newData(line)" with "qDebug() << line;" it'll show the data that i want.
    even when i try to use mid() function on line like this "line.mid(2,5)" it'll show the same message, but not with "line.length()"

    my purpose it to emit the data, and parse in other class outside this file

    anybody can help me please

    thanks in advance

    cheers,

  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: data from serial port

    I think the problem is emitting a signal with a char*argument.
    Try to use a const QByteArray& or const QString&
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: data from serial port

    Quote Originally Posted by mcosta View Post
    I think the problem is emitting a signal with a char*argument.
    Try to use a const QByteArray& or const QString&
    do you mean i should use QByteArray instead of QString for the argument of the "newData"?
    can you give any example or URL that show about it?
    still a little bit confuse to use QT, but i like QT anyway

  4. #4
    Join Date
    Apr 2009
    Location
    Finland
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: data from serial port

    My solution is use Qextserialport (http://qextserialport.sourceforge.ne...ialport-1.1.x/)

    I use winXP, usb-serial converter and QtCreator 4.5.1

    i have tested this with simple microcontroller program which sends data when button is pressed..

    When using in "real" situation you need to use QThread to set reader active if you want to maintain programs functionality.




    Here is simply code example
    Qt Code:
    1. comPort = new QextSerialPort("COM7");
    2. //set com port settings
    3.  
    4. comPort->setBaudRate(BAUD9600);
    5. comPort->setFlowControl(FLOW_HARDWARE);
    6. comPort->setParity(PAR_NONE);
    7. comPort->setDataBits(DATA_8);
    8. comPort->setStopBits(STOP_1);
    9.  
    10. //read data from com port
    11.  
    12. comPort->open(QIODevice::ReadWrite);
    13. char buff[1024];
    14. int numBytes;
    15. QString msg;
    16. numBytes = comPort->bytesAvailable();
    17. if(numBytes > 0)
    18. {
    19. if(numBytes > 1024){
    20. numBytes = 1024;}
    21. int i = comPort->read(buff, numBytes);
    22. buff[i] = '\0';
    23. msg = buff;
    24. }
    To copy to clipboard, switch view to plain text mode 

    And here is .pro

    Qt Code:
    1. //my qextserialport dir is C:/qt/qextserialport
    2. ...
    3. INCLUDEPATH += C:/Qt/qextserialport
    4. LIBS += C:\Qt\qextserialport\build\qextserialport.dll
    5. ..
    To copy to clipboard, switch view to plain text mode 

    -Jeo

  5. #5
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: data from serial port

    thanks for mcosta, I've solved with const QString&
    and Jeo_, thanks also, I'll try later

    cheers,

Similar Threads

  1. serial port same local machine
    By adamatic in forum Qt Programming
    Replies: 7
    Last Post: 3rd February 2009, 08:29
  2. accessing serial port without CONFIG += console
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 22:47
  3. Replies: 9
    Last Post: 4th June 2008, 13:29
  4. First attempt to display serial port data on GUI
    By ShaChris23 in forum Newbie
    Replies: 12
    Last Post: 4th May 2007, 10:14
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

Tags for this Thread

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.