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