1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/****************************************************************************
** Form interface generated from reading ui file 'drawdemo.ui'
**
** Created: Wed Feb 20 13:05:54 2008
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#ifndef MYPLOTTER_H
#define MYPLOTTER_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class
QVBoxLayout;
class
QHBoxLayout;
class
QGridLayout;
class
MyPlotter :
public
QWidget
{
Q_OBJECT
public
:
MyPlotter( QWidget* parent = 0,
const
char
* name = 0, WFlags fl = 0 );
~MyPlotter();
protected
:
virtual
void
paintEvent(QPaintEvent *);
private
slots:
void
rad();
void
savefile();
void
dos();
void
sendFG();
void
flushBuff();
private
:
char
buffer1[500];
char
buffer2[500];
int
buffer[10];
int
shijz[250];
int
highb;
int
lowb;
float
x_data[250];
QTimer *timer0;
QFrame *frame;
public
:
QLabel *x_label[10];
QLabel *y_label[10];
QPainter *painter;
QMultiLineEdit *medit;
QLabel *file_name;
QString file_name_in;
QLineEdit *file_name_edit;
QPushButton *button0;
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
QString convert_data;
int
confirm_flag;
char
ReceiveData[20];
int
fd;
int
i;
int
j;
int
qianb;
int
baib;
int
shib;
int
geb;
int
otd;
int
m1;
int
m2;
int
ret;
int
k;
int
wtable;
int
rtable;
char
buff[10];
int
yCoor[10];
int
xCoor[10];
int
hui[10];
void
drawHui();
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/****************************************************************************
** Form implementation generated from reading ui file 'drawdemo.ui'
**
** Created: Wed Feb 20 13:06:23 2008
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "plotter.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define xStep 25 //x
#define yStep 13 //y
#define frameWidth 250
#define frameHeight 160
#define DEVICE "/root/Documnets/text/plain/text.txt"
/*
* Constructs a Form1 which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*/
MyPlotter::MyPlotter( QWidget* parent,
const
char
* name, WFlags fl )
: QWidget( parent, name, fl )
{
if
(!name)
setName(
"PlotterWindow"
);
resize( 320, 200 );
setCaption( tr(
"Plotter"
) );
int
value=0;
int
j=0;
QString s=NULL;
/***建立绘图框区***/
frame=
new
QFrame(
this
,
"frame"
);
frame->setBackgroundColor(black);
frame->setGeometry(QRect(15,20,frameWidth,frameHeight));
/***建立横坐标轴上的时间标签***/
for
(
int
i=0;i<10;i++)
{
x_label[i]=
new
QLabel(
"timer"
,
this
);
x_label[i]->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
x_label[i]->setGeometry(QRect(5+(i*xStep),185,20,10));
}
/***建立纵坐标轴上的数据标签***/
for
(
int
i=0;i<=10;i++)
{
s=s.setNum(value);
//该函数是将整数转换为字符串
y_label[i]=
new
QLabel(s,
this
);
y_label[i]->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
y_label[i]->setGeometry(QRect(5,185-(i*yStep),6,5));
value+=5;
xCoor[i]=(i*xStep);
//保存x坐标轴上的坐标值,以供后面绘图所用
}
file_name=
new
QLabel(
this
);
file_name_edit=
new
QLineEdit(
this
);
button0=
new
QPushButton(
"R&D"
,
this
);
button0->setGeometry(QRect(270,20,40,20));
button0->setFont(QFont(
"Times"
,12,QFont::Bold));
connect(button0,SIGNAL(clicked()),
this
,SLOT(rad()));
button1=
new
QPushButton(
"SF"
,
this
);
button1->setGeometry(QRect(270,50,40,20));
button1->setFont(QFont(
"Times"
,12,QFont::Bold));
connect(button1,SIGNAL(clicked()),
this
,SLOT(savefile()));
button2=
new
QPushButton(
"DOS"
,
this
);
button2->setGeometry(QRect(270,80,40,20));
button2->setFont(QFont(
"Times"
,12,QFont::Bold));
connect(button2,SIGNAL(clicked()),
this
,SLOT(dos()));
button3=
new
QPushButton(
"send"
,
this
);
button3->setGeometry(QRect(270,110,40,20));
button3->setFont(QFont(
"Times"
,12,QFont::Bold));
connect(button3,SIGNAL(clicked()),
this
,SLOT(sendFG()));
file_name->setGeometry(QRect(270,140,40,20));
file_name->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
file_name->setText(
"File_Name"
);
file_name_edit->setGeometry(QRect(270,170,40,20));
timer0=
new
QTimer(
this
,
"timer0"
);
connect(timer0,SIGNAL(timeout()),
this
,SLOT(flushBuff()));
timer0->start(1000);
}
void
MyPlotter::rad()
{
/***串口初始化***/
struct
termios newtio,oldtio;
fd=open(
"/dev/ttyS1"
,O_RDWR|O_NOCTTY|O_NDELAY);
//打开串口设备文件
if
(fd<0)
{
perror
(
"open serial device error"
);
exit
(1);
}
if
(fcntl(fd,F_SETFL,0)<0)
perror
(
"fcntl failed"
);
if
(isatty(STDIN_FILENO)!=0)
printf
(
"isatty sucess!\n"
);
tcgetattr(fd,&oldtio);
bzero(&newtio,
sizeof
(newtio));
newtio.c_cflag|=CLOCAL|CREAD;
//CREAD:接收使能,CLOCAL:本地连接
newtio.c_cflag&=~CSIZE;
//无位掩码
newtio.c_cflag|=CS8;
//8位数据位
newtio.c_cflag&=~PARENB;
//非校验位使能
cfsetispeed(&newtio,B115200);
//设置输入输出波特率
cfsetospeed(&newtio,B115200);
newtio.c_cflag&=~CSTOPB;
//1位停止位
newtio.c_cc[VTIME]=0;
newtio.c_cc[VMIN]=0;
tcflush(fd,TCIFLUSH);
if
(tcsetattr(fd,TCSANOW,&newtio)<0)
{
perror
(
"tcsetattr is error"
);
exit
(1);
}
/***读取串口数据***/
printf
(
"begin to read:\n"
);
ret = read(fd,buffer1,500 );
printf
(
"ret=%d\n"
, ret );
close(fd);
/***数据处理***/
for
( i = 0; i < 500; i=i+2 )
{
int
ww=0;
otd=buffer1[i]+buffer1[i+1]*256;
//其中buffer[i]为低8位,buffer[i+1]为高2位
//lowb=(int)strtol(buffer1[i],&str,16);//低字节转换数
//highb=(int)strtol(buffer1[i+1],&str,16);//高字节转换数
qianb=otd/1000;
m1=otd%1000;
baib=m1/100;
m2=m1%100;
shib=m2/10;
geb=m2%10;
ww=i/2;
shijz[ww]=qianb*1000+baib*100+shib*10+geb;
//数据合成
// printf("read1=%d\n",shijz[ww]);
}
for
( i = 0; i < 250; i++)
{
x_data[i]=(shijz[i]*5)/1024;
//换算为模拟数值
// printf("x_data=%d\n",x_data[i]);
}
}
void
MyPlotter::savefile()
{
if
((fd = open(DEVICE,O_RDWR | O_NONBLOCK))<0)
{
qWarning(
"can not open device"
);
return
;
}
qWarning(
"open device: %s\n"
,DEVICE);
wtable=500*j;
lseek(fd, SEEK_SET, wtable);
ret=write(fd,buffer1,500);
printf
(
"ret=%d\n"
, ret );
if
(ret!=500)
printf
(
"write error\n"
);
else
j=j+1;
printf
(
"write data ok\n"
);
for
(i=0;i<500;i++)
{
printf
(
"buffer1=%x\n"
,buffer1[i]);
}
close(fd);
}
void
MyPlotter::dos()
{
file_name_in=file_name_edit->text();
k=file_name_in.toInt();
if
((fd = open(DEVICE,O_RDWR | O_NONBLOCK))<0)
{
qWarning(
"can not open device"
);
return
;
}
qWarning(
"open device: %s\n"
,DEVICE);
rtable=k*500+1;
lseek(fd, SEEK_SET, rtable);
printf
(
"begin to read:\n"
);
ret = read(fd, buffer2,500 );
printf
(
"ret=%d\n"
, ret );
close(fd);
/***数据处理***/
for
( i = 0; i < 500; i=i+2 )
{
int
ww=0;
otd=buffer2[i]+buffer2[i+1]*256;
//其中buffer[i]为低8位,buffer[i+1]为高2位
//lowb=(int)strtol(buffer1[i],&str,16);//低字节转换数
//highb=(int)strtol(buffer1[i+1],&str,16);//高字节转换数
qianb=otd/1000;
m1=otd%1000;
baib=m1/100;
m2=m1%100;
shib=m2/10;
geb=m2%10;
ww=i/2;
shijz[ww]=qianb*1000+baib*100+shib*10+geb;
//数据合成
//lowb=(int)strtol(buffer2[i],&str,16);//低字节转换数
//highb=(int)strtol(buffer2[i+1],&str,16);//高字节转换数
shijz[ww]=highb*16*16+lowb;
//数据合成
printf
(
"read1=%d\n"
,shijz[ww]);
}
for
( i = 0; i < 250; i++)
{
x_data[i]=(shijz[i]*5)/1024;
//换算为模拟数值
printf
(
"x_data=%d\n"
,x_data[i]);
}
}
void
MyPlotter::sendFG()
{
/***串口初始化***/
struct
termios newtio,oldtio;
fd=open(
"/dev/ttyS0"
,O_RDWR|O_NOCTTY|O_NDELAY);
//打开串口设备文件
if
(fd<0)
{
perror
(
"open serial device error"
);
exit
(1);
}
if
(fcntl(fd,F_SETFL,0)<0)
perror
(
"fcntl failed"
);
if
(isatty(STDIN_FILENO)!=0)
printf
(
"isatty sucess!\n"
);
tcgetattr(fd,&oldtio);
bzero(&newtio,
sizeof
(newtio));
newtio.c_cflag|=CLOCAL|CREAD;
//CREAD:接收使能,CLOCAL:本地连接
newtio.c_cflag&=~CSIZE;
//无位掩码
newtio.c_cflag|=CS8;
//8位数据位
newtio.c_cflag&=~PARENB;
//非校验位使能
cfsetispeed(&newtio,B115200);
//设置输入输出波特率
cfsetospeed(&newtio,B115200);
newtio.c_cflag&=~CSTOPB;
//1位停止位
newtio.c_cc[VTIME]=0;
newtio.c_cc[VMIN]=0;
tcflush(fd,TCIFLUSH);
if
(tcsetattr(fd,TCSANOW,&newtio)<0)
{
perror
(
"tcsetattr is error"
);
exit
(1);
}
/***串口写入数据***/
printf
(
"begin to read:\n"
);
ret = write(fd,buffer1,500 );
printf
(
"ret=%d\n"
, ret );
close(fd);
}
void
MyPlotter::flushBuff()
{
int
tmp = x_data[0];
int
i;
for
( i=0; i<250; i++)
{
x_data[i]=x_data[i+1];
}
buffer[249]=tmp;
repaint( 0, 0, 320, 200, TRUE);
}
void
MyPlotter::paintEvent(QPaintEvent *)
{
frame->erase(0,0,frameWidth,frameHeight);
painter=
new
QPainter();
painter->begin(frame);
QPoint beginPoint;
QPoint endPoint;
drawHui();
painter->end();
bitBlt(frame,0,0,frame);
}
void
MyPlotter::drawHui()
{
QPoint beginPoint,endPoint;
int
step=1;
painter->setPen(red);
for
(
int
i=0;i<250;i++)
{
beginPoint.setX(i*step);
beginPoint.setY(frameHeight-32*x_data[i]);
endPoint.setX(step+i*step);
endPoint.setY(frameHeight-32*x_data[i+1]);
painter->drawLine(beginPoint,endPoint);
}
}
/*
* Destroys the object and frees any allocated resources
*/
MyPlotter::~MyPlotter()
{
// no need to delete child widgets, Qt does it all for us
}
|