为什么我的代码会出现out of memory(1)啊?以下是具体的功能和实现?大神们帮我看一下
紫林落雪 2017-02-23 05:52:03 串口发送的数据格式是两个包相见发送的没有顺序,其中包1是f5 0a 03 fa _ _ _ fa _ _ _ fa _ _ _.........fa _ _ _c0 00 和包2是f5 0b 02 fb _ _ fb_ _ fb _ _ .........fb _ _ c0 00(fa 0a 啥的都是16进制两位数据)
(其中f5 oa o3和f5 ob o2 包头,03和02是帧后面的数据长度)c0 00是包尾,fa和fb是每个帧头,如何提取出来每个帧后面的划线的三或者两个数据并显示在iplot框中),以及包尾前面的两个数据显示在memo框中
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, Registry, StdCtrls, iComponent, iVCLComponent,
iCustomComponent, iPlotComponent, iPlot, OleCtrls, MSCommLib_TLB;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
ComboBox2: TComboBox;
MSComm1: TMSComm;
Openport: TButton;
Exit: TButton;
clear: TButton;
findport: TButton;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Memo4: TMemo;
Memo5: TMemo;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
iPlot2: TiPlot;
iPlot1: TiPlot;
procedure MSComm1Comm(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure findportClick(Sender: TObject);
procedure clearClick(Sender: TObject);
procedure ExitClick(Sender: TObject);
procedure OpenportClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var //定义变量,全局变量
Form1: TForm1;
comindex:array[0..6] of integer;
indata :array [0..99900000] of integer;
a,b,j,k,h,d :integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Mscomm1.InBufferCount:=0; // 清空接收缓冲区,当前接收缓冲区接收到的数据的长度, count:=MSComm1.InBufferCount
Mscomm1.InputLen:=0; // Input读取整个缓冲区内容 一次读取所有数据
Mscomm1.OutBufferCount:=0; //清空发送缓冲区
a:=0;
b:=0;
j:=0;
k:=0;
h:=10;
end;
procedure TForm1.findportClick(Sender: TObject);
var //通过读取注册表来获取,串口信息都是保存在注册表的这个位置
reg: TRegistry; //利用TRegistry对象来存取注册表文件中的信息
ts: TStrings; //TStrings是一个抽象类,在实际开发中,可以操纵属于组件(如TListBox)的字符串列表
i: integer; //TStrings类定义了它的方法,但它并没有实现这些方法来操纵字符串。TStrings派生类实现了这些方法
begin
ts := TStringList.Create;
reg := TRegistry.Create; //创建TRegistry对象
reg.RootKey := HKEY_LOCAL_MACHINE; //指定根键,存储当前系统的软硬件配置信息
reg.OpenKey('hardware\devicemap\serialcomm', true);
reg.GetValueNames(ts); //获取主键下的数据值名称
memo1.Lines.Add(ts[i]);
memo1.Lines.Add(reg.ReadString(ts[i]));
for i:=1 to ts.Count-1 do
begin //读取串口号设置串口
ComboBox1.Items.Add(reg.ReadString(ts.Strings[i]));//String是一个基础数据类型,而TStrings是一个类,该类的对象中每一行都是一个String
comindex[i]:=strtoint(reg.ReadString(ts.Strings[i])[4]);//指将字符型数据转换为数值型数据
Memo1.Lines.Add(reg.ReadString(ts.Strings[i]));//把每一项下的串值(接收的文字)显示在Memo1中
end;
ts.Free;
reg.CloseKey;
reg.free;
end;
procedure TForm1.OpenportClick(Sender: TObject);
var
ComSetting:String;
begin
if not MSComm1.PortOpen then
begin
MSComm1.CommPort:=comindex[ComboBox1.ItemIndex+1];
ComSetting:=ComboBox2.Text;
MSComm1.Settings:=ComSetting+',N,8,1';
MSComm1.InputMode:=comInputModeBinary;
MSComm1.PortOpen:=True;
end
else begin
MSComm1.PortOpen:=False;
end;
end;
procedure TForm1.ExitClick(Sender: TObject);
begin
if MSComm1.PortOpen = True then
begin
MSComm1.PortOpen:= False;
end;
Application.Terminate;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
SerialInput :variant;
i,c,d,inputlen :Integer;
y1,y2,y3,y4,y5,y6:Integer;
y11,y22,y33,y44,y55,y66,datastr1:string;
c1,c2:double;
begin
inputlen:=MSComm1.InBufferCount;
SerialInput:= MSComm1.Input;
for i:=0 to inputlen-1 do
begin
datastr1:=datastr1+' '+LowerCase(IntToHex(SerialInput[i],2));
indata[a]:= SerialInput[i];
inc(a);
end;
for i:=b to a-1 do
begin
if(indata[i]=$f5)and(indata[i+1]=$0a)and(indata[i+2]=$03)then
begin
for d:=i to a-4 do
begin
if (indata[d+3]=$fa)then
begin
if(indata[d+4]=$fa)then
begin
end
else
begin
y1:=indata[i+4]*256*256+indata[d+5]*256+indata[d+6];
y11:=FormatFloat('00.000',y1);
if TryStrToFloat(y11,c1) then
begin
iplot1.Channel[0].AddXY(j/h,c1);
end;
inc(j);
end;
end
else
begin
if(indata[d+3]=$c0)and(indata[d+4]=$00)then
begin
y2:=indata[d+1];
y3:=indata[d+2];
y22:=FormatFloat('00.000',y2);
y33:=FormatFloat('00.000',y3);
memo2.Lines.Add(y22);
memo3.Lines.Add(y33);
end;
end;
end;
end;
if(indata[i]=$f5)and(indata[i+1]=$0b)and(indata[i+2]=$02)then
begin
for c:=i to a-4 do
begin
if (indata[c+3]=$fb)then
begin
if(indata[c+4]=$fb)then
begin
end
else
begin
y4:=indata[c+4]*256+indata[c+5];
y44:=FormatFloat('00.000',y4);
if TryStrToFloat(y44,c2) then
begin
iplot2.Channel[0].AddXY(k/h,c2);
end;
inc(k);
end;
end
else
begin
if (indata[c+3]=$c0)and(indata[c+4]=$00)then
begin
y5:=indata[c+1];
y6:=indata[c+2];
y55:=FormatFloat('00.000',y5);
y66:=FormatFloat('00.000',y6);
memo4.Lines.Add(y55);
memo5.Lines.Add(y66);
end;
end;
end;
end;
end;
end;
procedure TForm1.clearClick(Sender: TObject);
begin
iplot1.Channel[0].Clear;
iplot1.Channel[1].Clear;
iplot2.Channel[0].Clear;
iplot2.Channel[1].Clear;
end;
end.