Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reading data from rotary sensor using USB port



  • Hi all,

    I have an rotary absolute encoder : AMT212A , I'm connected to with USB mini B cable, through RS485 interface.

    I want to develop my own application using (Qt and QtSerialPort module) to communicate with the usb port.
    My first code is running and I can detect the encoder, but the problem is, that I did not receive any data, can you help to figure it out ? Thank you.

    Here my app code :

    int main(int argc, char *argv[])
    {
        QCoreApplication coreApplication(argc, argv);
        QSerialPort serialPort;
    
        serialPort.setPortName("COM5");
        serialPort.setBaudRate(QSerialPort::Baud9600);
        serialPort.setDataBits(QSerialPort::Data8);
        serialPort.setFlowControl(QSerialPort::HardwareControl);
        serialPort.setParity(QSerialPort::NoParity);
        serialPort.setStopBits(QSerialPort::OneStop);
    
    
        if (!serialPort.open(QSerialPort::ReadOnly)) {
            qDebug << QObject::tr("Failed to open port") ;
            return -1;
        }
    
    
        SerialPortReader serialPortReader(&serialPort);
        return coreApplication.exec();
    }
    
    

    And here serialPortReader class :

    SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent) :
        QObject(parent),
        m_serialPort(serialPort),
        m_standardOutput(stdout)
    {
        connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead);
        connect(m_serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::handleError);
        connect(&m_timer, &QTimer::timeout, this, &SerialPortReader::handleTimeout);
    
        m_timer.start(5000);
    }
    
    void SerialPortReader::handleReadyRead()
    {
        m_readData.append(m_serialPort->readAll());
        qDebug() << QString::fromStdString(m_readData.toStdString());
    
        if (!m_timer.isActive())
            m_timer.start(5000);
    }
    
    void SerialPortReader::handleTimeout()
    {
        if (m_readData.isEmpty()) {
            m_standardOutput << QObject::tr("No data was currently available "
                                            "for reading from port %1")
                                .arg(m_serialPort->portName())
                             << endl;
        } else {
            m_standardOutput << QObject::tr("Data successfully received from port %1")
                                .arg(m_serialPort->portName())
                             << endl;
            m_standardOutput << m_readData << endl;
    
        }
    
        QCoreApplication::quit();
    }
    
    void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
    {
    
        if (serialPortError == QSerialPort::ReadError) {
            m_standardOutput << QObject::tr("An I/O error occurred while reading "
                                            "the data from port %1, error: %2")
                                .arg(m_serialPort->portName())
                                .arg(m_serialPort->errorString())
                             << endl;
            QCoreApplication::exit(1);
        }
    }
    

    PS: there is an app provided with this encoder.
    When downloaded, I can detect the encoder and read the different values.

    Any helps will be appreciated.
    Thank you.


  • Lifetime Qt Champion

    @KLAI said in Reading data from rotary sensor using USB port:

    that I did not receive any data

    So, handleReadyRead() is never called?
    What about handleTimeout()/handleError(...) - are those called?



  • @jsulm Yes , handleReadyRead() is never called, and I've always handleTimeout() called with an empty m_readData so the message "No data was currently available for reading from port COM5"


  • Moderators

    @KLAI maybe your sensor is supposed to be polled?

    Have you checked the data sheet?


  • Qt Champions 2020

    What's Qt version?

    Are you sure about the HW flow control? Because this control assumes the Rx + Tx and the RTS + CTS pins to be connected to the encoder (four pins).

    Also, the RS485 interface has only the Rx + Tx usually (so, there are no RTS/CTS pins).

    Just change the HardwareControl to NoFlowControl.

    Also, AFAIK, the RS485 (2-wire) is a half-duplex interface, that assumes the request/response approach. In this case, you need to poll the remote encoder, using some protocol (send the request and read the response form the specified encoder by the encoder address on a bus).



  • @J-Hilk Yes, I've already checked the datasheet, but I did not find anything saying that sensor should be polled.
    And if that is the case, how can I poll the sensor?


  • Moderators

    @KLAI said in Reading data from rotary sensor using USB port:

    @J-Hilk Yes, I've already checked the datasheet, but I did not find anything saying that sensor should be polled.
    And if that is the case, how can I poll the sensor?

    I doubt that, I spend 2 seconds scrolling through it and found this section:

    809e3b42-f8b2-446b-b831-696195d30967-image.png

    Contains nearly all information you need, further up is stuff about initializing the sensor



  • @kuzulis I'm using Qt 5.14.2.
    I tried to change HardwareControl to NoFlowControl but I had the same issue.
    Can you tell me how can I poll the encoder please? I've limited knowledges on request/response approaches. thx


  • Qt Champions 2020

    @KLAI said in Reading data from rotary sensor using USB port:

    Can you tell me how can I poll the encoder please?

    I don't know, just read that protocol specification and write an appropriate code.

    Besides, you can download and install any serial port sniffer software and to use an original application to see what happens on the serial side.


Log in to reply