Results 1 to 6 of 6

Thread: <solved> QtSerialPort: Open multiple ports

  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 16:50.

  2. #2
    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: QtSerialPort: Open multiple ports

    Define 2 pointers to QSerialPort object and open both ports - line 28 in mainwindow.h should be :
    Qt Code:
    1. QSerialPort *serial1,*serial2;
    To copy to clipboard, switch view to plain text mode 
    Lines 23 and 24 should be :
    Qt Code:
    1. QSerialPort *openSerial(const int comNr);
    2. void closeSerial(QSerialPort *com_ptr);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QtSerialPort: Open multiple ports

    Thanks a lot for your quick answer.

    Qt Code:
    1. QSerialPort *serial1,*serial2;
    To copy to clipboard, switch view to plain text mode 
    - Got it, thanks.

    But it's unclear to me what the following code does.
    Qt Code:
    1. QSerialPort *openSerial(const int comNr);
    To copy to clipboard, switch view to plain text mode 

    How would that enable me to call e.g:
    Qt Code:
    1. openSerial(3);
    2. openSerial(4);
    To copy to clipboard, switch view to plain text mode 

    To open COM3 and COM4?

    Thanks for you patience...
    Last edited by kfp; 31st August 2015 at 15:41.

  4. #4
    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: QtSerialPort: Open multiple ports

    Simply (without testing) :
    Qt Code:
    1. void MainWindow::openSerial(const int comNr)
    2. {
    3. QSerialPort *serial = new QSerialPort();
    4. serial->setPortName(QString("COM%1").arg(comNr));
    5. serial->setBaudRate(QSerialPort::Baud9600);
    6. serial->setDataBits(QSerialPort::Data8);
    7. serial->setParity(QSerialPort::NoParity);
    8. serial->setStopBits(QSerialPort::OneStop);
    9. serial->setFlowControl(QSerialPort::NoFlowControl);
    10. if (serial->open(QIODevice::ReadWrite))
    11. {
    12. ui->serialTxt->append("Connected");
    13. }
    14. else
    15. {
    16. QMessageBox::critical(this, tr("Error"), serial->errorString());
    17. delete serial;
    18. serial = 0;
    19. }
    20. return serial;
    21. }
    To copy to clipboard, switch view to plain text mode 
    and in another place :
    Qt Code:
    1. serial1 = openSerial(3);
    2. serial2 = openSerial(4);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QtSerialPort: Open multiple ports

    Thanks, that's great. - Got it up and running.

    For completeness sake, I corrected your suggestion to:
    Qt Code:
    1. QSerialPort *MainWindow::openSerial(const int comNr)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: QtSerialPort: Open multiple ports

    As I said : without testing

Similar Threads

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