Results 1 to 16 of 16

Thread: serial error

  1. #1
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy serial error

    I use qextserial for serial programing,but when I run it and put into the port name,there are endless of "errror" on the screen.
    Please take you a little time,help me!
    the codes as the following:
    Qt Code:
    1. sp = new QextSerialPort;
    2. sp->setBaudRate( QextSerialPort::BAUD9600);
    3. sp->setParity( QextSerialPort::PAR_NONE);
    4. sp->setDataBits( QextSerialPort:: DATA_8);
    5. sp->setStopBits( QextSerialPort::STOP_1);
    6. qDebug("ConstructorDestructorTest");
    7. qDebug("-------------------------");
    8. qDebug("+++ Created");
    9. if( !(sp->open(QIODevice::ReadWrite)))
    10. {
    11. qDebug("+++ Could not open");
    12. cout<<"input port"<<endl;
    13. char *POR;
    14. cin>>POR;
    15. QString PORTNAME1 = QString(POR);
    16. sp->setName(PORTNAME1);
    17.  
    18. if( PORTNAME1 == sp->name() )
    19. {
    20. qDebug("+++ set port name");
    21. if( sp->open() )
    22. {
    23. qDebug("+++ Opened");
    24. sp->close();
    25. qDebug("+++ Closed");
    26.  
    27. }
    28. else {
    29. qDebug("--- Could not open");
    30. }
    31. }
    32. else {
    33. qDebug("--- failed to set port name");
    34. }
    35. }
    36. else {
    37. qDebug("--- Opened");
    38. sp->close();
    39. qDebug("+++ Closed");
    40. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 16th April 2007 at 10:42. Reason: missing [code] tags

  2. #2
    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 error

    1. please post the errors you get.
    2. Make sure you are using QExtSerialPort that is built with Qt4
    3. Please use code tags when you post code.
    ==========================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.

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

    You do not test the second time you open the port (and set the name( if the open is successful ).

    Also, post the errors you get.

  4. #4
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: serial error

    Yes,I use QExtSerial and when I run the program,it shows :
    ConstructorDestructorTest
    -------------------------
    +++ Created
    +++ Could not open
    input port
    (after I input such as /dev/ttyS0)
    error
    error
    error
    error
    ......
    Whether there is something wrong with my card?But I have tested it in windows,it is all right.

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

    input port
    (after I input such as /dev/ttyS0)
    error
    error
    error
    error
    In the code you poseted, there is no output "error"...
    Where is it coming from (in your code)?
    Last edited by high_flyer; 16th April 2007 at 13:23.
    ==========================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
    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 error

    Your code is messy.
    So lets try to go step by step.
    Oh and make sure you have rights to read and write to the serial ports.
    Try this:
    Qt Code:
    1. sp = new QextSerialPort;
    2. sp->setBaudRate( QextSerialPort::BAUD9600);
    3. sp->setParity( QextSerialPort::PAR_NONE);
    4. sp->setDataBits( QextSerialPort:: DATA_8);
    5. sp->setStopBits( QextSerialPort::STOP_1);
    6. sp->setName("/dev/ttyS0");
    7. if( !(sp->open(QIODevice::ReadWrite)))
    8. {
    9. qDebug()<<"could not open"<<sp->name();
    10. }
    11. if(sp->isOpen())
    12. {
    13. qDebug()<<"opened ok";
    14. sp->close();
    15. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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. The following user says thank you to high_flyer for this useful post:

    hiuao (17th April 2007)

  8. #7
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: serial error

    Quote Originally Posted by high_flyer View Post
    Your code is messy.
    So lets try to go step by step.
    Oh and make sure you have rights to read and write to the serial ports.
    Try this:
    Qt Code:
    1. sp = new QextSerialPort;
    2. sp->setBaudRate( QextSerialPort::BAUD9600);
    3. sp->setParity( QextSerialPort::PAR_NONE);
    4. sp->setDataBits( QextSerialPort:: DATA_8);
    5. sp->setStopBits( QextSerialPort::STOP_1);
    6. sp->setName("/dev/ttyS0");
    7. if( !(sp->open(QIODevice::ReadWrite)))
    8. {
    9. qDebug()<<"could not open"<<sp->name();
    10. }
    11. if(sp->isOpen())
    12. {
    13. qDebug()<<"opened ok";
    14. sp->close();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Oh,high_flyer,there is no wrong when I compile it.The "error" appears when I run the program.And I have done as you said,but when I run the program it is waiting,nothing appears.The day before yesterday,it can run nevertheless it can't read data.

  9. #8
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: serial error

    I have opened it!But still I can't read data.When I run the program it shows:
    opened ok
    +++ Created first
    +++ Created second
    ReadLineTest
    -------------
    +++ Opened first
    +++ Opened second
    +++ Transmitted
    then waiting here.

    bool MySerialPort3::WriteReadTest()
    {
    bool result = FALSE;
    sp1 = new QextSerialPort("/dev/ttyS0");
    qDebug("+++ Created first");
    sp1->setBaudRate( QextSerialPort::BAUD9600);
    sp1->setParity( QextSerialPort::PAR_NONE);
    sp1->setDataBits( QextSerialPort:: DATA_8);
    sp1->setStopBits( QextSerialPort::STOP_1);

    sp2 = new QextSerialPort("/dev/ttyS0");
    qDebug("+++ Created second");
    sp2->setBaudRate( QextSerialPort::BAUD9600);
    sp2->setParity( QextSerialPort::PAR_NONE);
    sp2->setDataBits( QextSerialPort:: DATA_8);
    sp2->setStopBits( QextSerialPort::STOP_1);
    qDebug("ReadLineTest");
    qDebug("-------------");

    if( sp1->open(QIODevice::ReadWrite) )
    {
    qDebug("+++ Opened first");

    if( sp2->open(QIODevice::ReadWrite) )
    {
    qDebug("+++ Opened second");

    char data[] = { 0x00, 0x7F, 0x0A, 0xFF, 0x00, 0x0D, 0x7F, 0x80, 0xFF };
    if( sp1->writeBlock( data, 8 ) == 8 )
    {
    qDebug("+++ Transmitted");

    char r_data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0xAB};
    while( sp2->bytesAvailable() < 8 );
    if( 4 == sp2->readLine( r_data, 5 )
    && (uchar)r_data[0] == 0x00
    && (uchar)r_data[1] == 0x7F
    && (uchar)r_data[2] == 0x0A
    && (uchar)r_data[3] == 0x00
    && 6 == sp2->readLine( r_data, 6 )
    && (uchar)r_data[0] == 0xFF
    && (uchar)r_data[1] == 0x00
    && (uchar)r_data[2] == 0x0D
    && (uchar)r_data[3] == 0x7F
    && (uchar)r_data[4] == 0x80
    && (uchar)r_data[5] == 0x00 )
    {
    qDebug("+++ Received");
    result = TRUE;
    } else {
    qDebug("--- Could not receive");
    printf("the read data = %s\n",r_data);
    }
    } else {
    qDebug("--- Could not transmit");
    }
    } else {
    qDebug("--- Could not open second");
    }

    sp1->close();
    qDebug("+++ Closed first");
    sp2->close();
    qDebug("+++ Closed second");
    }
    else {
    qDebug("--- Could not open first");
    }

    return result;
    }
    Last edited by hiuao; 17th April 2007 at 04:32.

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

    Qt Code:
    1. while( sp2->bytesAvailable() < 8 );
    To copy to clipboard, switch view to plain text mode 
    This is bad practice, since you are "chocking" the CPU.
    The reason it waits here is either the while loop maked the CPU so busy it never tends to see if there are new bytes available, or, there are really no new bytes available.
    Use minicom or similar to make sure that the sending port does indeed sends data.
    And I would use a QTimer to poll the serial port for available bytes.
    Oh - and PLEASE use code tags for code! it is really hard to read the code this way.
    ==========================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. The following user says thank you to high_flyer for this useful post:

    hiuao (17th April 2007)

  12. #10
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: serial error

    Quote Originally Posted by high_flyer View Post
    Qt Code:
    1. while( sp2->bytesAvailable() < 8 );
    To copy to clipboard, switch view to plain text mode 
    This is bad practice, since you are "chocking" the CPU.
    The reason it waits here is either the while loop maked the CPU so busy it never tends to see if there are new bytes available, or, there are really no new bytes available.
    Use minicom or similar to make sure that the sending port does indeed sends data.
    And I would use a QTimer to poll the serial port for available bytes.
    Oh - and PLEASE use code tags for code! it is really hard to read the code this way.
    Thank you high_flyer for your advice!I use
    int len = sp1->write( data, 4 );
    int len2=sp2->read(r_data,4);
    find out it writes 4 chars but reads 0 chars.What can I do?

  13. #11
    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 error

    It could be that the amount of data is smaller than the inner serial buffer.
    try using flush() after you write or try writing more data.
    ==========================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. #12
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: serial error

    Quote Originally Posted by high_flyer View Post
    It could be that the amount of data is smaller than the inner serial buffer.
    try using flush() after you write or try writing more data.
    The result is the same as above.I think there is something wrong with my MOXA card's driven function.I plan to reinstall it.Because only "/dev/ttyS0" can write data,am I right?

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

    did you try to use some teminal application like minicom?
    Can you show your current code?
    ==========================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. #14
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: serial error

    Quote Originally Posted by high_flyer View Post
    did you try to use some teminal application like minicom?
    Can you show your current code?
    No,I don't use any other terminal,the above is the whole code.I just test it, hopping it can read and write data.

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

    I suugest you first test your serial ports using a terminal program.
    This will allow you to know if there is a problem with your hardware.
    If you can read/write to your ports with a terminal program then you know the problem is in your code.
    Did you make sure you have read and write rights to the /dev/ttySX?
    ==========================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. The following user says thank you to high_flyer for this useful post:

    hiuao (17th April 2007)

  19. #16
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: serial error

    Quote Originally Posted by high_flyer View Post
    I suugest you first test your serial ports using a terminal program.
    This will allow you to know if there is a problem with your hardware.
    If you can read/write to your ports with a terminal program then you know the problem is in your code.
    Did you make sure you have read and write rights to the /dev/ttySX?
    Thank you high_flyer.I accept your advice,I will use minicom for testing.
    Thank you very much!

Similar Threads

  1. Installation on Fedora Core 4
    By jcr in forum Installation and Deployment
    Replies: 3
    Last Post: 29th January 2009, 01:34
  2. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  3. Qt-x11-commercial-src-4.2.0-snapshot-20060824 error
    By DevObject in forum Installation and Deployment
    Replies: 4
    Last Post: 24th August 2006, 23:31
  4. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  5. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 12:54

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.