2017-05-22 59 views
1

小故事:我有TI AM335x armv7l處理器開發板,它運行嵌入式linux 3.2.0。我想從CAN總線讀取數據並將其可視化。我不知道如何從這開始。TI AM335x QT4.8.3讀取CANbus

更長的故事: 所以我有一個TI AM335x開發板從GOEMBED(http://goembed.com/index.php/Products/detail/tpid/25類似於beaglebone黑色)。它使用運行linux 3.2.0的armv7l處理器。

我連接了一個CAN模塊到開發板。該CAN模塊每秒發送相同的CAN消息到CAN總線上。

通過輸入以下命令到終端中,我可以看到CAN消息。

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

此時我可以看到CAN消息的ID和數據。

[email protected]:~# ip link set can0 type can bitrate 500000 triple-sampling on 
[email protected]:~# ip link set can0 up 
[email protected]:~# candump can0 
    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 

現在最大的問題是,我怎樣才能將這些數據導入到qt應用程序中? 我想將消息的數據打印到文本框中。

希望有人可以給我一些關於如何開始這個,所以我可以從中學習呢?

親切的問候

回答

0

我找到了一個簡單的解決方案。 因此,當我想要讀取嵌入式系統中的所有消息時,我可以輕鬆地採取我的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); 
} 

所以在這一點上,我可以把ID,數據字節和數據字節本身爲變量的量。所以玩的時間可以開始! 我希望這可以幫助其他人。如果人們知道改進,請告訴我。 親切的問候

1

理想情況下,你應該建立的Qt 5.8爲您的平臺,並使用QtSerialBus模塊。否則,您將面臨將QtSerialBus反向移植到Qt 4的無聊任務。這不會特別困難。當您訪問串行總線模塊後,您可以很容易地在CAN數據包到達時得到通知,然後以任何您希望的方式顯示它們。例如。您可以將它們累積在模型中,並將其顯示在QTableView中。

+0

感謝您的迴應,你能舉一個例子來說明如何支持這個嗎?我不明白我該如何開始這樣做。 – MertensToon

+0

@MertensToon或者你可以創建自己的類,它將使用標準套接字API,如[Wikipedia](https://en.wikipedia.org/wiki/SocketCAN)所示。但我也會採取Qt 5.8。 – yegorich

+0

@MertensToon將代碼複製到新的庫項目中,並開始修復編譯器錯誤... –