l 保存到TXT
AssignFile(F, form1.Edit3.Text);
Append(F);
Writeln(F, data) ;
Closefile(F) ;
l 保存到EXCEL
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.WorkBooks.Open(form1.Edit3.Text);
ExcelApp.workbooks[1].sheets[1].name:='数据采集demo';
ExcelApp.workbooks[1].sheets[1].CELLS[1,2]:='DATA';
ExcelApp.workbooks[1].sheets[1].CELLS[1,3]:='TIME';
ExcelApp.workbooks[1].sheets[1].CELLS[1,4]:='sensor1';
ExcelApp.workbooks[1].sheets[1].CELLS[1,5]:='sensor2';
ExcelApp.workbooks[1].sheets[1].CELLS[1,6]:='sensor3';
ExcelApp.workbooks[1].sheets[1].CELLS[1,7]:='sensor4';
ExcelApp.workbooks[1].sheets[1].CELLS[1,8]:='sensor5';
ExcelApp.ActiveSheet.Rows[2].Insert ;
ExcelApp.workbooks[1].sheets[1].CELLS[2,2]:=datetostr(now);
ExcelApp.workbooks[1].sheets[1].CELLS[2,3]:=timetostr(now);
ExcelApp.workbooks[1].sheets[1].CELLS[2,4]:=sensor1;
ExcelApp.workbooks[1].sheets[1].CELLS[2,5]:=sensor2;
ExcelApp.workbooks[1].sheets[1].CELLS[2,6]:=sensor3;
ExcelApp.workbooks[1].sheets[1].CELLS[2,7]:=sensor4;
ExcelApp.workbooks[1].sheets[1].CELLS[2,8]:=sensor5;
ExcelApp.workbooks[1].SAVE;
ExcelApp.QUIT;
l 绘制曲线
form1.Series1.Add(sensor1);
form1.Series2.Add(sensor2);
form1.Series3.Add(sensor3);
form1.Series4.Add(sensor4);
form1.Series5.Add(sensor5);
l 完整串口中断函数
在ComDataPacket控件的OnPacket Events中添加如下代码:
procedure TForm1.ComDataPacket1Packet(Sender: TObject; const Str: String);
var
a:byte;
rev:array[1..25]of uchar;
DateTime:TDateTime;
F:Textfile;
sensor1,sensor2,sensor3,sensor4,sensor5:integer;
ExcelApp: Variant;
begin
move(Str[1],rev,20);
sensor1:= rev[3]*256+rev[4];//在这里只取了16位有效数据,如果想传输浮点数需要4个字节,读者可以思考一下处理的方法
sensor2:= rev[7]*256+rev[8];
sensor3:= rev[11]*256+rev[12];
sensor4:= rev[15]*256+rev[16];
sensor5:= rev[19]*256+rev[20];
form1.Series1.Add(sensor1);
form1.Series2.Add(sensor2);
form1.Series3.Add(sensor3);
form1.Series4.Add(sensor4);
form1.Series5.Add(sensor5);
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.WorkBooks.Open(form1.Edit3.Text);
ExcelApp.workbooks[1].sheets[1].name:='数据采集demo';
ExcelApp.workbooks[1].sheets[1].CELLS[1,2]:='DATA';
ExcelApp.workbooks[1].sheets[1].CELLS[1,3]:='TIME';
ExcelApp.workbooks[1].sheets[1].CELLS[1,4]:='sensor1';
ExcelApp.workbooks[1].sheets[1].CELLS[1,5]:='sensor2';
ExcelApp.workbooks[1].sheets[1].CELLS[1,6]:='sensor3';
ExcelApp.workbooks[1].sheets[1].CELLS[1,7]:='sensor4';
ExcelApp.workbooks[1].sheets[1].CELLS[1,8]:='sensor5';
ExcelApp.ActiveSheet.Rows[2].Insert ;
ExcelApp.workbooks[1].sheets[1].CELLS[2,2]:=datetostr(now);
ExcelApp.workbooks[1].sheets[1].CELLS[2,3]:=timetostr(now);
ExcelApp.workbooks[1].sheets[1].CELLS[2,4]:=sensor1;
ExcelApp.workbooks[1].sheets[1].CELLS[2,5]:=sensor2;
ExcelApp.workbooks[1].sheets[1].CELLS[2,6]:=sensor3;
ExcelApp.workbooks[1].sheets[1].CELLS[2,7]:=sensor4;
ExcelApp.workbooks[1].sheets[1].CELLS[2,8]:=sensor5;
ExcelApp.workbooks[1].SAVE;
ExcelApp.QUIT;
AssignFile(F, form1.Edit3.Text);
Append(F);
Writeln(F, data) ;
Closefile(F) ;
end;
6、总结
本文介绍了一下如何用DELPHI编写数据采集类上位机软件,当然写的比较简单,也比较啰嗦,仅仅适合硬件工程师为了工作方便而编写的工具类软件,也想强调一下,作为硬件工程师如果能够掌握一种上位机编程语言会让工作得到事半功倍的效果,也是一个机器人设计工作者处理各种传感器数据时需要用到的必备技能。
文章评论(0条评论)
登录后参与讨论