Results 1 to 3 of 3

Thread: serial programming help

  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default serial programming help

    Hi all,

    anyone use before qextserialport? I need some help in getting it to work properly. I have a arm development board which does not have any keyboard attached so I need to hyper terminal my laptop to the board for keyboard entry.

    here are my problems:
    1) at start it proceed fine but after a while it started to print garbage and also lose some input. eg. abcd@@.- after that I typed g once and it will show abcd@@.-g@@.-

    2) I seems to get seg fault after I close the form to load another form or end prog (if I start the receive function by button clicked event).

    3) regardless of whether I click on button to start receiving or not, when I end the program, it will bring me to the login in screen of linux. eg ARM login: _

    Can someone help me? below is my attached coded. Thanks


    form1.h
    Qt Code:
    1. #ifndef FORM1_H
    2. #define FORM1_H
    3.  
    4. #include <QWidget>
    5. //#include <qextserialport.h>
    6. #include "serial.h"
    7. #include "form2.h"
    8. #include <QLineEdit>
    9.  
    10. namespace Ui {
    11. class form1;
    12. }
    13.  
    14. class form1 : public QWidget {
    15. Q_OBJECT
    16. public:
    17. form1(QWidget *parent = 0);
    18. ~form1();
    19.  
    20. protected:
    21. void changeEvent(QEvent *e);
    22.  
    23. private slots:
    24. void on_btnLoadf2_clicked();
    25. void on_btn_rec_clicked();
    26. void on_btn_exit_clicked();
    27. void closeEvent(QCloseEvent *e);
    28.  
    29. private:
    30. Ui::form1 *ui;
    31. Form2 *f2;
    32. serial *port;
    33. };
    34.  
    35. #endif // FORM1_H
    To copy to clipboard, switch view to plain text mode 

    form1.cpp
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3. #include <QString>
    4. #include "form2.h"
    5. #include <iostream>
    6.  
    7. form1::form1(QWidget *parent) :
    8. QWidget(parent),
    9. ui(new Ui::form1)
    10. {
    11. ui->setupUi(this);
    12. port = new serial;
    13. port->init();
    14. }
    15.  
    16. form1::~form1()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void form1::closeEvent(QCloseEvent *e)
    22. {
    23. port->close();
    24. }
    25.  
    26. void form1::changeEvent(QEvent *e)
    27. {
    28. QWidget::changeEvent(e);
    29. switch (e->type()) {
    30. case QEvent::LanguageChange:
    31. ui->retranslateUi(this);
    32. break;
    33. default:
    34. break;
    35. }
    36. }
    37.  
    38. void form1::on_btnLoadf2_clicked()
    39. {
    40. QString str = "to be pass over to f2";
    41. f2 = new Form2;
    42. f2->show();
    43. this->close();
    44. this->destroy();
    45. }
    46.  
    47. void form1::on_btn_exit_clicked()
    48. {
    49. this->close();
    50. }
    51.  
    52. void form1::on_btn_rec_clicked()
    53. {
    54. port->receive((void *) ui->line_serial);
    55. }
    To copy to clipboard, switch view to plain text mode 

    serial.h
    Qt Code:
    1. #ifndef SERIAL_H
    2. #define SERIAL_H
    3.  
    4. #include <qextserialport.h>
    5.  
    6. class serial
    7. {
    8. public:
    9. serial();
    10. ~serial();
    11. void init();
    12. void receive(void *line);
    13. void close();
    14.  
    15. private:
    16. bool stop;
    17. QextSerialPort *com;
    18. };
    19.  
    20. #endif // SERIAL_H
    To copy to clipboard, switch view to plain text mode 

    serial.cpp
    Qt Code:
    1. #include "serial.h"
    2. #include <QApplication>
    3. #include <QLineEdit>
    4.  
    5. serial::serial()
    6. {
    7. }
    8.  
    9. serial::~serial()
    10. {
    11. }
    12.  
    13. void serial::init()
    14. {
    15. //set up serial port
    16. com = new QextSerialPort("/dev/ttyS0");
    17. com->setBaudRate(BAUD115200);
    18. com->setParity(PAR_NONE);
    19. com->setDataBits(DATA_8);
    20. com->setStopBits(STOP_1);
    21. com->setFlowControl(FLOW_OFF);
    22. com->open(QIODevice::ReadOnly);
    23. stop = false;
    24. }
    25.  
    26. void serial::close()
    27. {
    28. stop = true;
    29. delete com;
    30. com = NULL;
    31. }
    32.  
    33. void serial::receive(void *obj)
    34. {
    35. char data[256];
    36. int nbytes;
    37.  
    38. QLineEdit *line = (QLineEdit *) obj;
    39. QString* str = new QString;
    40.  
    41. while (stop == false) {
    42. QApplication::processEvents();
    43. nbytes = com->bytesAvailable();
    44.  
    45. if (nbytes > 0) {
    46. com->read(data,nbytes);
    47. *str = data;
    48. line->setText(line->text().append(*str));
    49. }
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

  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 programming help

    I don't think QExtSerialPort has anything to do with this, it uses a native implementation for handling the port. It would be more probably that you terminal sends some special characters which you don't know how to read or that you otherwise corrupt the port.

    By the way, what's the reason for passing a void pointer around your code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: serial programming help

    Quote Originally Posted by eva2002 View Post
    2) I seems to get seg fault after I close the form to load another form or end prog (if I start the receive function by button clicked event).
    Problem is in this two lines :
    Qt Code:
    1. QApplication::processEvents();
    2. nbytes = com->bytesAvailable();
    To copy to clipboard, switch view to plain text mode 

    I think that serial::close() is called in some place activated by QApplication::processEvents(). After this com == NULL. Yours code must looks like :
    Qt Code:
    1. QApplication::processEvents();
    2. if( com != NULL )
    3. {
    4. nbytes = com->bytesAvailable();
    5. .....
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. serial port programming
    By sujatashooter in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2008, 15:51
  2. Serial Programming with QT
    By ape in forum Newbie
    Replies: 3
    Last Post: 24th December 2007, 20:33
  3. serial communication programming
    By jagadish in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2007, 07:47
  4. serial port programming in qt
    By sar_van81 in forum Qt Programming
    Replies: 46
    Last Post: 13th June 2007, 12:27
  5. Replies: 12
    Last Post: 23rd March 2007, 09:23

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.