mainwindow.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. this->serial = new QSerialPort(this);
  9. this-> OpenSerial();
  10. connect(this->serial, SIGNAL(readyRead()), this, SLOT(on_ReadSerial()));
  11. }
  12. MainWindow::~MainWindow()
  13. {
  14. delete ui;
  15. }
  16. void MainWindow::OpenSerial()
  17. {
  18. this->serial->setPortName("/dev/ttyUSB0");
  19. // this->serial->setPortName("/dev/pts/7");
  20. this->serial->setBaudRate(QSerialPort::Baud9600);
  21. this->serial->setDataBits(QSerialPort::Data8);
  22. this->serial->setFlowControl(QSerialPort::NoFlowControl);
  23. this->serial->setParity(QSerialPort::NoParity);
  24. this->serial->setStopBits(QSerialPort::OneStop);
  25. if (serial->open(QIODevice::ReadWrite)) {
  26. qDebug() << "yes";
  27. } else {
  28. qDebug() << "no";
  29. }
  30. }
  31. void MainWindow::on_ReadSerial()
  32. {
  33. if (serial->canReadLine()){
  34. QString data = serial->readLine();
  35. // emit send_data(data);
  36. qDebug() << data << "all";
  37. }
  38. }