//**************************************************************************************
qt测试
spidev.h
#ifndef SPIDEV_H
#define SPIDEV_H
#include <QPainter>
#include <QPen>
#include <QString>
#include <QRect>
#include <QPushButton>
#include <QPoint>
#include <QTimer>
#include <QCloseEvent>
#include "mythread.h"
class mainWidget : public QWidget
{
Q_OBJECT
public:
mainWidget(QWidget *parent = 0);
~mainWidget();
bool labelchange;
private slots:
void transData();
void threadslot();
void writeStopCmd();
void writeStartCmd();
protected:
void paintEvent(QPaintEvent *);
void closeEvent(QCloseEvent *event);
private:
int maxY;
int numYticks;
int minY;
int maxX;
int minX;
int numXticks;
enum { Margin = 20 };
QPushButton *btnclose;
QPushButton *btnstart;
static unsigned char startCmd[5];
static unsigned char stopCmd[5];
MyThread mythread;
QTimer *timer;
};
#endif // SPIDEV_H
spi.cpp
#include "spidev.h"
#include <termios.h> /*PPSIX terminal controle */
#include <stdio.h> /* stand input and output... */
#include <stdlib.h> /* stand lib */
#include <unistd.h> /* UNIX stand function */
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /* file controle */
unsigned char mainWidget::startCmd[5]={0xAA,0x01,0x02,0x03,0x04};
unsigned char mainWidget::stopCmd[5]={0xAA,0x04,0x03,0x02,0x01};
mainWidget::mainWidget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("myspidev data curve");
setBackgroundRole(QPalette::Dark);
setAutoFillBackground(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setFocusPolicy(Qt::StrongFocus);
minX=0;
minY=0;
maxX=500;
maxY=0xffffff;
numXticks=5;
numYticks=5;
btnclose = new QPushButton(tr("&close"),this);
btnstart = new QPushButton(tr("&start"),this);
connect(btnclose,SIGNAL(clicked()),this,SLOT(close()));
connect(btnstart,SIGNAL(clicked()),this,SLOT(transData()));
connect(&mythread,SIGNAL(finished ()),this,SLOT(writeStopCmd()));
connect(&mythread,SIGNAL(sgWriteStart()),this,SLOT(writeStartCmd()));
labelchange=false;
connect(btnstart,SIGNAL(clicked()),this,SLOT(threadslot()));
timer=new QTimer(this);
timer->start(200);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
}
mainWidget::~mainWidget()
{
}
void mainWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QRect rect(2*Margin, Margin,width() - 3 * Margin, height() - 4 * Margin);
if (!rect.isValid())
return;
QPen quiteDark = palette().dark().color().light();
QPen light = palette().light().color();
for (int i = 0; i <= numXticks; ++i) {
int x = rect.left() + (i * (rect.width() - 1) / numXticks);
painter.setPen(quiteDark);
painter.drawLine(x, rect.top(), x, rect.bottom());
painter.setPen(light);
painter.drawLine(x, rect.bottom(), x, rect.bottom() + 5);
painter.drawText(x, rect.bottom() + 5, Margin - 5, Margin,
Qt::AlignHCenter | Qt::AlignTop,
QString::number(i));
}
int y;
for (int j = 0; j <= numYticks; ++j) {
y = rect.bottom() - (j * (rect.height() - 1) / numYticks);
painter.setPen(quiteDark);
painter.drawLine(rect.left(), y, rect.right(), y);
painter.setPen(light);
painter.drawLine(rect.left() - 5, y, rect.left(), y);
painter.drawText(rect.left() - Margin, y - 10, Margin - 5, Margin,
Qt::AlignRight | Qt::AlignVCenter,
QString::number(j));
}
//------------------------------------------------------------coordination painted over.
btnstart->setGeometry(4*Margin,height()-2*Margin,3*Margin,Margin);
btnclose->setGeometry(10*Margin,height()-2*Margin,3*Margin,Margin);
//---------------------------------------------draw curve
QPoint points[240];
unsigned int *p=mythread.getdata();
for(int i=0;i<240;i++)
{
points[i].setX(i+rect.left());
points[i].setY((*(p+i))*(rect.height())/maxY+rect.top()+2*y);
}
painter.drawPoints(points, 240);
}
//************************************************serial port set
void set_speed(int fd, int speed)
{
int status;
struct termios Opt;
tcgetattr(fd, &Opt); // get attribute of serial port
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed);
cfsetospeed(&Opt, speed);
status = tcsetattr(fd, TCSANOW, &Opt); // set attribute
if (status != 0)
{
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
int set_data_format(int fd,int databits,int stopbits,int parity)
{
struct termios opt;
if( tcgetattr(fd, &opt) != 0)
{
perror("SetupSerial 1");
return -1;
}
opt.c_cflag &= ~CSIZE;
switch (databits)
{
case 5:
opt.c_cflag |= CS5;
break;
case 6:
opt.c_cflag |= CS6;
break;
case 7:
opt.c_cflag |= CS7;
break;
case 8:
opt.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return -1;
}
switch (parity)
{
case 'n':
case 'N':
opt.c_cflag &= ~PARENB; /* Clear parity enable */
opt.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
opt.c_cflag |= (PARODD | PARENB); /* parity checking */
opt.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
opt.c_cflag |= PARENB; /* Enable parity */
opt.c_cflag &= ~PARODD; /* */
opt.c_iflag |= INPCK; /* Disnable parity checking */
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
switch (stopbits)
{
case 1:
opt.c_cflag &= ~CSTOPB;
break;
case 2:
opt.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return -1;
}
/* Set input parity option */
if (parity != 'n')
opt.c_iflag |= INPCK;
opt.c_cc[VTIME] = 100; // 10 seconds
opt.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &opt) != 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}
//******************************************************************serial port set
void mainWidget::transData()
{
//change labeltext
labelchange=!labelchange;
if(labelchange)
btnstart->setText(tr("&stop"));
else
btnstart->setText(tr("&start"));
update();
}
void mainWidget::writeStopCmd()
{
int fd;
fd = open("/dev/ttyS1", O_RDWR); // read and write
if(fd == -1)
{
printf("Can't open ttyS1!\n");
exit(0);
}
set_speed(fd, B9600); // 9600
if (set_data_format(fd, 8, 1, 'N')== -1)
{
printf("Data format Error!\n");
exit(1);
}
::write(fd,stopCmd,5);
::close(fd);
}
void mainWidget::writeStartCmd()
{
int fd;
printf("writeStartCmd works!\n");
fd = open("/dev/ttyS1", O_RDWR); // read and write
if(fd == -1)
{
printf("Can't open ttyS1!\n");
exit(0);
}
set_speed(fd, B9600); // 9600
if (set_data_format(fd, 8, 1, 'N')== -1)
{
printf("Data format Error!\n");
exit(1);
}
::write(fd,startCmd,5);
::close(fd);
}
void mainWidget::threadslot()
{
if (mythread.isRunning())
{
mythread.stop();
}
else
{
mythread.start();
}
}
void mainWidget::closeEvent(QCloseEvent *event)
{
mythread.stop();
mythread.wait();
event->accept();
}
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QtGui/QWidget>
#include <QThread>
#include <QVector>
#include <QMutex>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
void stop();
unsigned int* getdata();
signals:
void sgWriteStart();
protected:
void run();
private:
volatile bool stopped;
unsigned int mydata[240];
QVector<unsigned int> vect;
QMutex mutex;
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <stdio.h> /* stand input and output... */
#include <stdlib.h> /* stand lib */
#include <unistd.h> /* UNIX stand function */
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /* file controle */
#include <errno.h> /* error */
#include <strings.h>
#include <pthread.h>
MyThread::MyThread()
{
stopped = false;
memset(mydata,0,sizeof(mydata));
}
void MyThread::run()
{
int fd;
int res;
unsigned char spidata[60];
fd=open("/dev/myspidev",O_RDWR);
if (fd==-1){
printf("Can't open myspidev device!\n");
exit(EXIT_FAILURE);
}
vect.clear();
emit sgWriteStart();
printf(" sgWriteStart() sends!\n");
while (!stopped)
{
res=::read(fd,spidata,60);
if(res!=60)
printf("read error\n");
mutex.lock();
for(int i=0;i<60;i++)
{
vect.append((int)spidata[i]);
}
mutex.unlock();
}//------------------while ends
// printf("after while\n");
stopped = false;
::close(fd);
emit finished ();
}
void MyThread::stop()
{
stopped = true;
}
unsigned int* MyThread::getdata()
{
QVector<unsigned int>::iterator T = vect.begin();
mutex.lock();
if(vect.size()>800)
{
for(int i=0;i<240;i++)
{
mydata[i]=(vect[i*3] << 16)+(vect[i*3+1] << 8)+(vect[i*3+2]);
T++;
T++;
T++;
}
vect.erase(vect.begin(),T);
//the following used for debug
/* for(int i=0;i<720;i++)
{
if(i%15==0)
printf("\n");
printf("%3x\t",vect[i]);
}
while(1);*/
}
mutex.unlock();
return mydata;
}
main.c
#include <QtGui/QApplication>
#include "spidev.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWidget w;
w.show();
return a.exec();
}