Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading data from rotary sensor using USB port

Reading data from rotary sensor using USB port

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 417 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KLAI
    wrote on last edited by KLAI
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • K KLAI

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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?

        K Offline
        K Offline
        KLAI
        wrote on last edited by
        #3

        @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"

        J.HilkJ 1 Reply Last reply
        0
        • K KLAI

          @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"

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @KLAI maybe your sensor is supposed to be polled?

          Have you checked the data sheet?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          K 1 Reply Last reply
          2
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by kuzulis
            #5

            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).

            K 1 Reply Last reply
            4
            • J.HilkJ J.Hilk

              @KLAI maybe your sensor is supposed to be polled?

              Have you checked the data sheet?

              K Offline
              K Offline
              KLAI
              wrote on last edited by
              #6

              @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?

              J.HilkJ 1 Reply Last reply
              0
              • K KLAI

                @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?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @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


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • K kuzulis

                  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).

                  K Offline
                  K Offline
                  KLAI
                  wrote on last edited by
                  #8

                  @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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on last edited by kuzulis
                    #9

                    @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.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved