ubuntu-14.04qt5.3

QSerialPort gives error "no such file or directory"


Used example from here on Ubuntu 14.04 with Qt 5.3

 foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    qDebug() << "Name        : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

    // Example use QSerialPort
    QSerialPort serial;
    serial.setPort(info);
    if (serial.open(QIODevice::ReadWrite)){
        serial.errorString();
        serial.close();
    }
}

Output:

Name "ttyACM2"
Description "E5-00"
Manufacturer "Nokia"
"No such File or Directory"

As it seems from code QSerialPortInfo has detected ports correctly but even though serial.open() is returning true errorString() returns "No such file or directory".Desperately need a solution.


Solution

  • I know this is going to sound funny, but I was just able to resolve this by simply ensuring the port name was the absolute path to the serial device.

    The issue did not become apparent to us until I was distributing builds created using 'macdeployqt'. Debug builds made using Qt Creator connected fine (and Windows builds we were distributing worked fine as well). But deployed builds would give the DeviceNotFound error with error text "no such file or directory.

    The fix is simple, just prepare the port name like so before calling QSerialPort::open():

    #ifdef __APPLE__
    // mac only
            comPort.name = "/dev/cu." + info.portName();
    // end mac only
    #else
            comPort.name = info.portName();
    #endif
    

    of course that is specific to OS X (a 'usbserial*' port is actually '/dev/cu.usbserial*'), but something similar will work for you on Linux if you know the actual port name in /dev/. Also I have always simply used QSerialPort::setPortName as opposed to setPort as you are attempting to do, so you may want to try that.