qtembedded-linuxcan-bus

TI AM335x QT4.8.3 read CAN bus


Short story: I have TI AM335x armv7l processor development board which runs embedded Linux 3.2.0. I would like to read data from the CAN bus and visualize this. I don't have any idea on how to start with this.

Longer story: So I have a TI AM335x development board from GOEMBED (similar to the BeagleBone Black). It uses an armv7l processor which runs Linux 3.2.0.

I connected a CAN module to the development board. This CAN module sends the same CAN message every second onto the CAN bus.

By entering the following commands into the terminal, I can visualize the CAN messages.

ip link set can0 type can bitrate 500000 triple-sampling on
ip link set can0 up
candump can0

At this point, I can see the the ID and data of the CAN message (at root).

ip link set can0 type can bitrate 500000 triple-sampling on
ip link set can0 up
candump can0

Output:

  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3
  can0  1FC0000F   [8]  F5 F8 F1 00 00 00 F2 F3

Now the big problem is, how can I get this data into a Qt-application? I would like to print the data of a message into a textbox.

What are some hints on how to start with this?


Solution

  • I found an easy solution.

    As I want to read all messages in my embedded system, I can easily just take the response of my QProcess.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(&proc3, SIGNAL(readyRead()), this, SLOT(dataReady()));
        QD << QDir::currentPath();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::dataReady()
    {
        while(proc3.bytesAvailable())
        {
            QString in = proc3.readLine();
            QString out;
    
            QRegExp reg("  can([01])  ([0-9A-F]+)   \\[([0-9]+)\\]  ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+)");
    
            if(reg.indexIn(in) >= 0)
            {
                out = "from Can%1 Id:%2 amount bytes:%3 first byte:%4 second byte:%5 third byte:%6 fourth byte:%7 fifth byte:%8 sixth byte:%9 seventh byte:%10 eight byte:%11";
                out = out.arg(reg.cap(1)).arg(reg.cap(2)).arg(reg.cap(3)).arg(reg.cap(4)).arg(reg.cap(5)).arg(reg.cap(6)).arg(reg.cap(7)).arg(reg.cap(8)).arg(reg.cap(9)).arg(reg.cap(10)).arg(reg.cap(11));
            }
            else
            {
                out = "Error:" + in;
            }
            ui->plainTextEdit->appendPlainText(out);
        }
    }
    
    void MainWindow::on_startButton_clicked()
    {
        QString cmd1 = "ip";
        QStringList opt1;
        QD;
        opt1 << "link"<< "set"<< "can0"<< "bitrate"<< "500000"<< "triple-sampling"<< "on";
        proc1.start(cmd1, opt1);
    
        QString cmd2 = "ip";
        QStringList opt2;
        QD;
        opt2 << "link"<< "set"<< "can0"<< "up";
        proc2.start(cmd2, opt2);
    
        QString cmd3 = "candump";
        QStringList opt3;
        QD;
        opt3 << "can0";
        proc3.start(cmd3, opt3);
    }
    
    void MainWindow::on_stopButton_clicked()
    {
        QString cmd4 = "ip";
        QStringList opt4;
        QD;
        opt4 << "link"<< "set"<< "can0"<< "down";
        proc4.start(cmd4, opt4);
    }
    

    So at this point I can put the ID, amount of data bytes and the data bytes itself into variables. So play time can start!