2009年8月20日 星期四

在PC的 Windows 上執行 QT ,QT 透過 RS232 傳送訊息到 ARM 板,用以控制板上 LED

這個範例是在模組D1上課時,臨時決定要作的。其功能為: 在 Windows 上執行QT程式,然後輸入一字串(僅包含0與1)後,PC 可以透過 RS232 將此字串傳送至 ARM( PXA270 )板上,由板上的 RS232 程式接收後,再將此包含0與1的字串逐一控制 ARM( PXA270 )板上的 LED 亮或暗。

整個程式系統包含:1.PC上的QT程式,此程式的執行畫面如下:





由於 QT 程式碼比較長,我在最後再印出來。


2. ARM( PXA270 )板上的程式,程式碼如下:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
int main()
{
  int rs232FD, n1, readLen;
  if( (rs232FD = open ( "/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY ))!= -1 )
  {
    char BUF[11];
    struct termios newterm,oldterm;
    tcgetattr(rs232FD,&oldterm);
    bzero(&newterm,sizeof(newterm));
    newterm.c_cflag|=CLOCAL | CREAD;
    newterm.c_cflag&=~CSIZE;
    cfsetispeed(&newterm,B115200);
    cfsetospeed(&newterm,B115200);
    newterm.c_cflag|=CS8;
    newterm.c_cflag &= ~PARENB;
    newterm.c_cflag&=~CSTOPB;
    tcflush(rs232FD,TCIFLUSH);
    tcsetattr(rs232FD,TCSANOW,&newterm);
    memset( BUF, '\0' , 11);
    while( 1 )
    {
      if( (readLen = read ( rs232FD , (void *)BUF, 11 )) > 0  )
      {
        int x1, fd;
        fd = open( "/dev/LED", O_RDWR );
        printf( "--%s--\r\n" ,BUF );
        for( x1=0;x1<10;x1++ )
        {
          write( fd , (void *)(&BUF[x1]) , sizeof(char) *1  );
          sleep( 1 );
        }
        close(fd);
      }
      memset( BUF, '\0' , 11);
    }
    tcsetattr(rs232FD,TCSANOW,&oldterm);
    close( rs232FD );
  }
  return 0;
}


3.ARM( PXA270 )板上的LED驅動程式。採用【模組C:Step by Step講授嵌入式開發板Linux驅動程式】完成的 LED 驅動程式。


上述第一項所提QT程式碼如下:


main.cpp程式如下:
#include  <QtGui/QApplication>
#include "mymainwindow.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myMainWindow w;
    w.show();
    return a.exec();
}


mymainwindow.h程式如下:
#include <QtGui/QMainWindow>


namespace Ui
{
    class myMainWindow;
}


class myMainWindow : public QMainWindow
{
    Q_OBJECT


public:
    myMainWindow(QWidget *parent = 0);
    ~myMainWindow();


private:
    Ui::myMainWindow *ui;
private slots:
        void myBtnClick();
};


 


mymainwindow.h程式如下:
#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include "qextserialport.h"
#include "qextserialbase.h"
#include "myRs232ForQT.h"
myMainWindow::myMainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::myMainWindow)
{
    ui->setupUi(this);
}


myMainWindow::~myMainWindow()
{
    delete ui;
}
void myMainWindow::myBtnClick()
{
  Rs232 = set_opt( "COM1",115200,8,'N',1);
  Rs232->open();
  Rs232->write( ui->lineEdit->text().toAscii(), ui->lineEdit->text().length() );
  Rs232->close();
  ui->label->setText( ui->lineEdit->text() );
}


ui_mymainwindow.h程式如下:
/********************************************************************************
** Form generated from reading ui file 'mymainwindow.ui'
**
** Created: Fri Aug 21 00:40:41 2009
**      by: Qt User Interface Compiler version 4.5.2
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/


#ifndef UI_MYMAINWINDOW_H
#define UI_MYMAINWINDOW_H


#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>


QT_BEGIN_NAMESPACE


class Ui_myMainWindow
{
public:
    QWidget *centralWidget;
    QPushButton *pushButton;
    QLineEdit *lineEdit;
    QLabel *label;
    QLabel *label_2;
    QLabel *label_3;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;


    void setupUi(QMainWindow *myMainWindow)
    {
        if (myMainWindow->objectName().isEmpty())
            myMainWindow->setObjectName(QString::fromUtf8("myMainWindow"));
        myMainWindow->resize(281, 216);
        centralWidget = new QWidget(myMainWindow);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        pushButton = new QPushButton(centralWidget);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(90, 140, 111, 23));
        lineEdit = new QLineEdit(centralWidget);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(90, 110, 113, 20));
        lineEdit->setMaxLength(10);
        label = new QLabel(centralWidget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(90, 80, 111, 21));
        label->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 255, 255);"));
        label_2 = new QLabel(centralWidget);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setGeometry(QRect(20, 110, 71, 16));
        label_2->setStyleSheet(QString::fromUtf8("background-color: rgb(170, 255, 255);"));
        label_2->setTextFormat(Qt::PlainText);
        label_2->setWordWrap(true);
        label_3 = new QLabel(centralWidget);
        label_3->setObjectName(QString::fromUtf8("label_3"));
        label_3->setGeometry(QRect(10, 0, 261, 71));
        label_3->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 170, 255);"));
        label_3->setWordWrap(true);
        myMainWindow->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(myMainWindow);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 281, 20));
        myMainWindow->setMenuBar(menuBar);
        mainToolBar = new QToolBar(myMainWindow);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        myMainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(myMainWindow);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        myMainWindow->setStatusBar(statusBar);


        retranslateUi(myMainWindow);
        QObject::connect(pushButton, SIGNAL(clicked()), myMainWindow, SLOT(myBtnClick()));


        QMetaObject::connectSlotsByName(myMainWindow);
    } // setupUi


    void retranslateUi(QMainWindow *myMainWindow)
    {
        myMainWindow->setWindowTitle(QApplication::translate("myMainWindow", "myMainWindow", 0, QApplication::UnicodeUTF8));
        pushButton->setText(QApplication::translate("myMainWindow", "Send", 0, QApplication::UnicodeUTF8));
        lineEdit->setText(QApplication::translate("myMainWindow", "1010101010", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("myMainWindow", "TextLabel", 0, QApplication::UnicodeUTF8));
        label_2->setText(QApplication::translate("myMainWindow", "LED On/Off", 0, QApplication::UnicodeUTF8));
        label_3->setText(QApplication::translate("myMainWindow", "\347\257\204\344\276\213\345\212\237\350\203\275\357\274\232\346\226\274 LineEdit \350\274\270\345\205\2450/1\357\274\214\347\204\266\345\276\214\346\214\211\344\270\213Send\343\200\202\346\255\244QT\347\250\213\345\274\217\345\260\207\351\200\217\351\201\216 RS232 \351\200\201\345\207\272\346\226\274 LineEdi \344\270\212t\350\274\270\345\205\245\347\232\204\345\200\274\357\274\214\345\206\215\347\224\261ARM\346\235\277(PXA270)\345\217\246\345\244\226\344\270\200\346\224\257RS232\347\250\213\345\274\217\346\216\245\346\224\266\345\276\214\357\274\214\345\206\215\351\200\217\351\201\216\351\251\205\345\213\225\347\250\213\345\274\217\346\216\247\345\210\266LED\344\272\256\346\210\226\346\232\227\343\200\202", 0, QApplication::UnicodeUTF8));
        Q_UNUSED(myMainWindow);
    } // retranslateUi


};


namespace Ui {
    class myMainWindow: public Ui_myMainWindow {};
} // namespace Ui


QT_END_NAMESPACE


#endif // UI_MYMAINWINDOW_H


1 則留言:

FPGA Verilog 的學習經驗,提供給要入門的新手

今天簡單說說 FPGA Verilog 的學習經驗,提供給要入門的新手: 1.對自己寫的FPGA Verilog程式,所生成的數位電路要心中有數。 這一點個人認為很重要,就正如寫 C語言,心中要能生成對應的組合語言一樣,我是這樣要求自己的。 雖然 FPGA Verilog語言...