Results 1 to 6 of 6

Thread: how to open the serial port of my pc from the qt app

  1. #1
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to open the serial port of my pc from the qt app

    Hello all,
    I was writing code for RFID reader interfacing for my pc. RFID reader is responding fine with my minicom program ...but from the application code it is saying unable to open port.

    Here is my code...

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. textEdit=ui->textEdit;
    7.  
    8. qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();
    9.  
    10. foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    11. {
    12. qDebug()<<"Name: "<<info.portName();
    13. qDebug()<<"Description: "<<info.description();
    14. qDebug()<<"Manufactures: "<<info.manufacturer();
    15.  
    16.  
    17. QSerialPort *port = new QSerialPort(info);
    18. if (port->open(QIODevice::ReadWrite)) {
    19. qDebug() << "Baud rate:" << port->baudRate();
    20. qDebug() << "Data bits:" << port->dataBits();
    21. qDebug() << "Stop bits:" << port->stopBits();
    22. qDebug() << "Parity:" << port->parity();
    23. qDebug() << "Flow control:" << port->flowControl();
    24. qDebug() << "Read buffer size:" << port->readBufferSize();
    25. port->close();
    26. } else {
    27. qDebug() << "Unable to open port, error code" << port->error();
    28. }
    29.  
    30. }
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    The output i am getting is
    ---------------------------------
    Number of serial ports: 1
    Name: "ttyS0"
    Description: ""
    Manufactures: ""
    Unable to open port, error code 2
    --------------------------------------------

    Thanks in advance.
    Last edited by anda_skoa; 26th February 2014 at 14:16. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to open the serial port of my pc from the qt app

    if you use miniterm or some other program can you open that device? You might need to adjust the permissions on the device.
    Qt Code:
    1. QSerialPort::NoError 0 No error occurred.
    2. QSerialPort::DeviceNotFoundError 1 An error occurred while attempting to open an non-existing device.
    3. QSerialPort::PermissionError 2 An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open.
    4. QSerialPort::OpenError 3 An error occurred while attempting to open an already opened device in this object.
    5. QSerialPort::ParityError 4 Parity error detected by the hardware while reading data.
    6. QSerialPort::FramingError 5 Framing error detected by the hardware while reading data.
    7. QSerialPort::BreakConditionError 6 Break condition detected by the hardware on the input line.
    8. QSerialPort::WriteError 7 An I/O error occurred while writing the data.
    9. QSerialPort::ReadError 8 An I/O error occurred while reading the data.
    10. QSerialPort::ResourceError 9 An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.
    11. QSerialPort::UnsupportedOperationError 10 The requested device operation is not supported or prohibited by the running operating system.
    12. QSerialPort::UnknownError 11 An unidentified error occurred.
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to open the serial port of my pc from the qt app

    Thanks for the Quick reply sir,
    The device (rfid reader) is opening and giving data to minicom of my pc...
    Now how to set the permission access from Qt app..i.e how to open the port, can u please explain in brief...

    Thank you

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to open the serial port of my pc from the qt app

    There are two possibilities straight from the error message:
    • Attempting to open an already opened device by another process. If there is another process with the port already open then this might happen. Minicom should not be running when you try to run this.
    • User not having enough permission and credentials to open. The permissions are those of the file representing the serial port: probably "/dev/ttyS0" but it may be in a subdirectory of /dev. Often only root and a group called "uucp" have write access to the device.
      Qt Code:
      1. $ ls -l /dev/ttyS0
      2. crw-rw---- 1 root uucp 4, 64 Feb 27 07:15 /dev/ttyS0
      3. $ groups
      4. wheel audio cdrom video games cdrw users wireshark vboxusers truecrypt uucp
      To copy to clipboard, switch view to plain text mode 
      Your Linux user should be generally placed in the "uucp" group (recommended) although you can alter the serial port permissions directly (not recommended).


    Here is a User Management tutorial.
    Here is a Linux File Permissions tutorial.
    Last edited by ChrisW67; 27th February 2014 at 08:35.

  5. #5
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to open the serial port of my pc from the qt app

    Thanks for the quick reply sir,
    First of all sorry for my english.

    I was running my code in 'root' and it is getting data from serial port....
    I written the code for reading data as shown below,
    Qt code:
    if(port->waitForReadyRead(5000)){
    QByteArray readdata=port->readAll();
    //textEdit->append(readdata);
    while(port->waitForReadyRead(1000)){
    readdata +=port->readAll();
    }

    textEdit->append(readdata);
    }
    It is reading one time and displaying on the textEdit.. but i want to monitor and read the data from the port continuously whenever the data available...what should i have to do...
    Thanks in advance...

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to open the serial port of my pc from the qt app

    Don't "wait for readyRead", connect a slot to the readyRead() signal and read the data in the slot.

    Btw, running a GUI program as root is usually a bad idea. At some point you should invest a bit of time to get the permissions right for a normal user account.

    Cheers,
    _

Similar Threads

  1. qt serial port
    By saman_artorious in forum Newbie
    Replies: 9
    Last Post: 24th April 2013, 17:19
  2. Best serial port library?
    By Momergil in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2013, 07:38
  3. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 03:38
  4. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 11:19
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 03:05

Tags for this Thread

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.