Hi,

I am wondering how to use QExtSerialPort to declare and read from a Comport.

I am trying to read in a sting of data from an instrument and extract 4 points of data, 2 bytes each, and display them on my gui.

I have three main questions:


How do I make my Comport hook up with a specific comport? (COM38 in my case)
How do I read in the data into a string or char array?
How do I get that data from mainwindow to main?

Here is what I have so far:
Mainwindow.cpp;
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <qextserialport.h>
  4. #include <QTextBrowser>
  5.  
  6. QextSerialPort *port;
  7.  
  8. char buff[1024];
  9.  
  10.  
  11. MainWindow::MainWindow(QWidget *parent) :
  12. QMainWindow(parent),
  13. ui(new Ui::MainWindow)
  14. {
  15. port=new QextSerialPort();
  16. ui->setupUi(this);
  17. port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
  18.  
  19. port->setBaudRate(BAUD38400);
  20.  
  21. port->setFlowControl(FLOW_OFF);
  22.  
  23. port->setParity(PAR_NONE);
  24.  
  25. port->setDataBits(DATA_8);
  26.  
  27. port->setStopBits(STOP_1);
  28.  
  29. port->setQueryMode(QextSerialPort::EventDriven);
  30.  
  31.  
  32. }
  33.  
  34. MainWindow::~MainWindow()
  35. {
  36. //for reading purpose
  37.  
  38. char buff[219];
  39.  
  40. if(port->bytesAvailable())
  41.  
  42. {
  43.  
  44. int i = port->read(buff, 1);//read 1 byte at a time
  45.  
  46. buff[i] = '\0';
  47.  
  48.  
  49.  
  50. }
  51. delete ui;
  52. }
To copy to clipboard, switch view to plain text mode 

Main.cpp;
Qt Code:
  1. #include "qextserialport.h"
  2. #include "qextserialport_p.h"
  3. #include "mainwindow.h"
  4. #include <QApplication>
  5. #include <QLabel>
  6. #include <QVBoxLayout>
  7. #include <QGridLayout>
  8. #include <QString>
  9. #include <stdio.h>
  10. #include <QtCore/QDebug>
  11. #include <QtCore/QReadLocker>
  12. #include <QtCore/QWriteLocker>
  13.  
  14.  
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.  
  19.  
  20. QApplication a(argc, argv);
  21. QWidget window;
  22.  
  23. float beam1,beam2,beam3,beam4,average;
  24.  
  25.  
  26. QString qstr1 = QString::number(beam1);
  27. QString qstr2 = QString::number(beam2);
  28. QString qstr3 = QString::number(beam3);
  29. QString qstr4 = QString::number(beam4);
  30. QString qstr5 = QString::number(average);
  31.  
  32. QGridLayout* mainLayout = new QGridLayout(&window);
  33. QLabel* bl1 = new QLabel("Beam One");
  34. QLabel* br1 = new QLabel(qstr1);
  35. QLabel* bl2 = new QLabel("Beam Two");
  36. QLabel* br2 = new QLabel(qstr2);
  37. QLabel* bl3 = new QLabel("Beam Three");
  38. QLabel* br3 = new QLabel(qstr3);
  39. QLabel* bl4 = new QLabel("Beam Four");
  40. QLabel* br4 = new QLabel(qstr4);
  41. QLabel* al = new QLabel("Average");
  42. QLabel* ar = new QLabel(qstr5);
  43.  
  44. mainLayout->addWidget(bl1,0,0);
  45. mainLayout->addWidget(br1,0,1);
  46. mainLayout->addWidget(bl2,1,0);
  47. mainLayout->addWidget(br2,1,1);
  48. mainLayout->addWidget(bl3,2,0);
  49. mainLayout->addWidget(br3,2,1);
  50. mainLayout->addWidget(bl4,3,0);
  51. mainLayout->addWidget(br4,3,1);
  52. mainLayout->addWidget(al,4,0);
  53. mainLayout->addWidget(ar,4,1);
  54.  
  55. window.show();
  56.  
  57. return a.exec();
  58. }
To copy to clipboard, switch view to plain text mode