How to use QThread with CAN bus?



  • Hi, i'm using qt5.5 on my computer which based on QT for my OS system.
    I wanna do Sending and Receiving together via CanBus.
    And this is my code here.

    void MainWindow::on_pushButton_SenReceive_clicked()
    {
        QThread *thread1=new QThread;
        QThread *thread2=new QThread;
    
        connect(thread1,SIGNAL(started()),this,SLOT(cansend()));
        connect(thread2,SIGNAL(started()),this,SLOT(candump()));
    
        thread1->moveToThread(thread1);
        thread2->moveToThread(thread2);
        thread1->start();
        thread2->start();
    }
    
    void MainWindow::cansend()
    {
        QString info;
        qDebug("cansend");
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can0 stop",&info);
        qDebug()<<info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can0 bitrate 50000 ctrlmode triple-sampling on",&info);
        qDebug()<<info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can0 start",&info);
        qDebug()<<info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"cansend can0 -i 0x100 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88");
    }
    void MainWindow::candump()
    {
        QString info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can1 stop",&info);
        qDebug()<<info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can1 bitrate 50000 ctrlmode triple-sampling on",&info);
        qDebug()<<info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can1 start",&info);
        qDebug()<<info;
        Process::executeProcessSync(QString("sh1"),QStringList()<<"-c"<<"candump can1 &",&info);
        qDebug("candump4");
    
        ui->textEditPT_Canbus_43->append(info);
    }
    

    But when i pushed my button it will not show anything out on the textEditPT_Canbus_43.
    P.S. the"sh1"is not the wrong commend, i will do some check if it is sh1.
    Did i miss anything?
    Please help!


  • Moderators

    @victor-wang Did you try it like this:

    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig"<< "can0"<< "stop",&info);


  • @jsulm
    Don't you think problem is rather, this:

    QThread *thread1=new QThread;
    thread1->moveToThread(thread1);
    

    I'm not sure this even compiles...

    @victor-wang Thread Example


  • Moderators

    @J.Hilk This is funny :-)
    But it should compile at least.



  • @jsulm
    I don't think it is the problem cause i had debug it it will print the right respond to me.

    @J-Hilk
    Is that you mean i have to move to thread2 and thread2 move to thread 1 for instead?


  • Moderators

    @victor-wang Why do you move thread1 object to the thread managed by it? It is quite unusual.
    Just remove this line

    thread1->moveToThread(thread1);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.