Results 1 to 6 of 6

Thread: <solved> QtSerialPort: Open multiple ports

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Default <solved> QtSerialPort: Open multiple ports

    Hi all. Hope you are able to help me with this.
    I'm fairly new to both C++ and the qt framework so I apologize if the question is a bit vague.

    I have made a simple desktop serial port application using Qt Creator 3.5.0 (opensource) under windows 7.
    The program works using one serial port but I need to read data from two barcode scanners, connected to two different serial ports, and compare the received data to a string.

    I don't know if it is of any importance, but the barcode only passes one of the scanners, so the scanners will never transmit any data at the same time.

    I have pasted my code below:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSerialPort/QSerialPort>
    6. #include <QMessageBox>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20.  
    21. private slots:
    22. void readData();
    23. void openSerial(const int comNr);
    24. void closeSerial(const int comNr);
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. QSerialPort *serial;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.c
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #define nrBytes 8
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. serial = new QSerialPort(this);
    12.  
    13. openSerial(3);
    14.  
    15. connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23.  
    24. void MainWindow::readData()
    25. {
    26. if (serial->bytesAvailable() >= nrBytes)
    27. {
    28. QByteArray data = serial->readAll();
    29. QString rxString = QString(data);
    30. ui->serialTxt->setText(rxString);
    31. if (rxString == "test")
    32. {
    33. //Do stuff
    34. }
    35. }
    36. }
    37.  
    38. void MainWindow::openSerial(const int comNr)
    39. {
    40. serial->setPortName(QString("COM%1").arg(comNr));
    41. serial->setBaudRate(QSerialPort::Baud9600);
    42. serial->setDataBits(QSerialPort::Data8);
    43. serial->setParity(QSerialPort::NoParity);
    44. serial->setStopBits(QSerialPort::OneStop);
    45. serial->setFlowControl(QSerialPort::NoFlowControl);
    46. if (serial->open(QIODevice::ReadWrite))
    47. {
    48. ui->serialTxt->append("Connected");
    49. }
    50. else
    51. {
    52. QMessageBox::critical(this, tr("Error"), serial->errorString());
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 

    I'm aware my questions are plagued my my lacking c++ knowledge, but here we go:

    How do I make single function, openSerial(), that both constructs and opens a new serial port? Do i pass a QSerialPort by reference to openSerial(). Or is it possible to create the function, so calling Eg. openSerial(3) would create: QSerialPort *serial3 and open COM3?
    - And if so how do i make eg. serial3 visible to my other functions?

    Thanks in advance

    Kind regards
    Kfp
    Last edited by kfp; 31st August 2015 at 15:50.

Similar Threads

  1. Replies: 3
    Last Post: 7th April 2014, 10:46
  2. Replies: 9
    Last Post: 1st May 2013, 09:29
  3. qextserialport cant open ports in windows 7
    By manaila in forum Newbie
    Replies: 4
    Last Post: 16th April 2012, 15:40
  4. Multiple open databases and MVC
    By mtnbiker66 in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2012, 00:08
  5. multiple view ports
    By manmohan in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2009, 07: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.