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. How to use QThread with CAN bus?

How to use QThread with CAN bus?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.8k 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.
  • V Offline
    V Offline
    victor wang
    wrote on last edited by A Former User
    #1

    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!

    jsulmJ 1 Reply Last reply
    0
    • V victor wang

      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!

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

      @victor-wang Did you try it like this:

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

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

      J.HilkJ 1 Reply Last reply
      0
      • jsulmJ jsulm

        @victor-wang Did you try it like this:

        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig"<< "can0"<< "stop",&info);
        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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


        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.

        jsulmJ 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @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

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

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

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

          V 1 Reply Last reply
          0
          • jsulmJ jsulm

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

            V Offline
            V Offline
            victor wang
            wrote on last edited by
            #5

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

            jsulmJ 1 Reply Last reply
            0
            • V victor wang

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

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

              @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);
              

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

              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