所使用的开发板:友善之臂tiny6140
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <qlcdnumber.h> namespace Ui { class Widget; } class TMainForm : public QWidget { Q_OBJECT public: TMainForm(QWidget * parent = 0, const char * name = 0, Qt::WFlags f = 0); virtual ~TMainForm() {} protected: void timerEvent (QTimerEvent *); private: QLCDNumber* m_label; Ui::Widget *ui; }; #endif // WIDGET_Hwidget.cpp
</pre><pre code_snippet_id="1599573" snippet_file_name="blog_20160306_3_265073" name="code" class="cpp">#include "widget.h" #include "ui_widget.h"
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <linux/fs.h> #include <errno.h> #include <string.h> TMainForm::TMainForm(QWidget *parent, const char *name, Qt::WFlags f) { //setCaption( "ADC-Testing" ); m_label = new QLCDNumber(this); m_label->setGeometry(10,10, 100, 50); m_label->setSegmentStyle(QLCDNumber::Flat); startTimer(500); }
</pre><pre code_snippet_id="1599573" snippet_file_name="blog_20160306_6_2630841" name="code" class="cpp">void TMainForm::timerEvent ( QTimerEvent * ) { int fd = ::open("/dev/adc", 0); if (fd < 0) { return; } char buffer[30] = ""; int len = ::read(fd, buffer, sizeof buffer -1); if(len > 0) { buffer[len] ='\0'; int value=-1 sscanf(buffer,"%d",&value); m_label->display(value); } ::close(fd);
}
</pre><span style="font-family:Microsoft YaHei; font-size:14px"></span><p><span style="font-family:Microsoft YaHei; font-size:14px"></span></p><p><span style="font-family:Microsoft YaHei; font-size:14px">main.cpp</span></p><p><span style="font-family:Microsoft YaHei; font-size:14px"></span></p><p><span style="background-color:rgb(240,240,240)">#include <QtGui/QApplication></span></p><pre code_snippet_id="1599573" snippet_file_name="blog_20160306_8_9401732" name="code" class="cpp">#include "widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); TMainForm w; w.show(); return a.exec(); }