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

How to send data to Serial Port in Qt Test



  • I am trying to write some integration tests around my serial device and it appears that i can't send data in my tests i get the following error message:

    QWARN  : Test::test_case3() QObject::startTimer: Timers can only be used with threads started with QThread
    

    My Test case:

    void Test::test_case3()
    {
        // MotorTest mt;
        // QThread* thread = new QThread( );
        // mt.moveToThread(thread);
        // thread->start();
    
        qDebug("MOTOR TESTS");
        Motor motor;
        // // QThread::sleep(1);
        QTest::qSleep(1000);
        motor.motor_jog_accel(10);
        // / QTest::qSleep(1000);
        motor.motor_jog_decel(10);
        // // QTest::qSleep(1000);
        motor.motor_jog_speed(10);
        // // QTest::qSleep(1000);
        motor.motor_jog_enable();
        // // QTest::qSleep(5000);
        motor.motor_jog_disable();
        // // QTest::qSleep(1000);
    }
    

    Here is some of the Code from the motor that is relevant:

    #include "motor.h"
    #include <string.h>
    #include <QIODevice>
    #include <QDebug>
    
    Motor::Motor()
    {
        sp.setPortName("COM1");
        sp.open(QIODevice::ReadWrite);
    }
    
    // void Motor::motor_send_command(char buff[], uint16_t buffLen)
    void Motor::motor_send_command(char buff[], uint16_t buffLen)
    {
        qDebug() << "SENDING: " << buff;
        char cbuff[50] = { '\0' };
        strncpy(cbuff, buff, 50);
        snprintf(cbuff, 50, "%s\r", buff);
        sp.write(cbuff);
    }
    
    void Motor::motor_send_command(char buff[])
    {
        qDebug() << "SENDING: " << buff;
        char cbuff[50] = { '\0' };
        strncpy(cbuff, buff, 50);
        snprintf(cbuff, 50, "%s\r", buff);
        qDebug() << "ABOUT: " << buff;
        sp.write(cbuff);
        qDebug() << "SENT: " << buff;
    }
    
    void Motor::motor_set_accel(uint16_t accel)
    {
    }
    
    void Motor::motor_set_accel_max(uint16_t accel)
    {
    
    }
    
    void Motor::motor_start_jog()
    {
    
    }
    
    void Motor::motor_change_speed(uint16_t speed)
    {
    
    }
    
    void Motor::motor_set_decel(uint16_t decel)
    {
    
    }
    
    void Motor::motor_jog_accel(uint16_t accel)
    {
        motor_send_command("JA1000");
    }
    
    void Motor::motor_jog_velocity(uint16_t velocity)
    {
    
    }
    
    void Motor::motor_jog_disable()
    {
        motor_send_command("SJ");
    }
    
    void Motor::motor_jog_enable()
    {
        motor_send_command("CJ");
    }
    
    void Motor::motor_jog_decel(uint16_t decel)
    {
        motor_send_command("JL1000");
    }
    
    void Motor::motor_jog_speed(uint16_t speed)
    {
        motor_send_command("JS1");
    }
    
    void Motor::motor_disable()
    {
    
    }
    
    void Motor::motor_enable()
    {
    
    }
    
    void Motor::motor_microstep_resolution(uint16_t resolution)
    {
    
    }
    
    void Motor::motor_set_direction(uint8_t direction)
    {
    
    }
    
    void Motor::motor_stop_jogging()
    {
    
    }
    
    void Motor::motor_velocity_max(uint16_t vmax)
    {
    
    }
    
    

    Is there any way to be able to start the main test with QThread? I guess the interrupts used for the serialport are not able to start because of that?


  • Lifetime Qt Champion

    Hi,

    Are you using QTEST_MAIN for your tests ?


Log in to reply