码迷,mamicode.com
首页 > 编程语言 > 详细

delphi 中一种好用的数组定义方法以及函数嵌套的使用源代码

时间:2015-04-09 10:37:10      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:delphi   方法   嵌套   

type
TByteBuff;= array of integer;

 function abc(a:integer):TByteBuff;;
 var
 tempArr:TByteBuff;
 begin
 setlength(tempArr,2);
 tempArr[0] := a;
 tempArr[1] := a + 10;
 result:=tempArr;
 end;

 procedure TForm1.Button1Click(Sender: TObject);
 var
 a : integer;
 begin
 a := 5;
 showmessage(inttostr(   abc(a)[0]   ));  //5  //这以这样使用返回数组 
 showmessage(inttostr(   abc(a)[1]   ));  //15 //这以这样使用返回数组
end;

function TFrmMain.CommandFrameData(CommandCode, MeasureType, SampleMode: Byte;
  DataSize, MeasureSum, MeasureID: Integer): TByteBuff;
var
tempsendbuff,InteraBuff:TByteBuff;
J,sum:Integer;


function BuildFrameData(CommandCode, MeasureType, SampleMode: Byte;
  DataSize, MeasureSum, MeasureID: Integer): TByteBuff;  //组帧数据
  var
sendbuff: TByteBuff;
i:Integer;
begin
setlength(sendbuff,Datasize);
//FillChar(sendbuff,SizeOf(sendbuff),0);
if CommandCode=$01 then   //建立连接帧
begin
    sendbuff[0]:=$01;
end;
if CommandCode=$02 then   //测量任务及参数设置指令
begin
  if MeasureType=$03 then                         //数字测量类型
  begin
    sendbuff[0]:=CommandCode;                     //指令码
    sendbuff[1]:=MeasureSum;                      //被测量个数
    sendbuff[2]:=MeasureID;                       //测量ID
    sendbuff[3]:=MeasureType;                     //测量类型

    sendbuff[5]:=DMeasureParam.DChannelNum;       //数字通道号
    sendbuff[6]:=DMeasureParam.PulsePerCir[0];    //每圈脉冲数
    sendbuff[7]:=DMeasureParam.PulsePerCir[1];
    sendbuff[8]:=DMeasureParam.FrequencyCoefficient;   //分频系数
    sendbuff[4]:=SizeOf(sendbuff[5])+SizeOf(sendbuff[6])+SizeOf(sendbuff[7])+SizeOf(sendbuff[8]);   //测量参数长度


  end
  else
  begin
    sendbuff[0]:=CommandCode;                     //指令码
    sendbuff[1]:=MeasureSum;                      //被测量个数
    sendbuff[2]:=MeasureID;                       //测量ID
    sendbuff[3]:=MeasureType;

    if AMeasureParam.Samplemode=$00 then
    begin
    sendbuff[5]:=AMeasureParam.AChannelNum;       //模拟通道号
    sendbuff[6]:=AMeasureParam.DChannelNum;     //数字通道号
    sendbuff[7]:=AMeasureParam.NodeNumPerCir[0]; //每圈采集点数
    sendbuff[8]:=AMeasureParam.NodeNumPerCir[1];
    sendbuff[9]:=AMeasureParam.PulsePerCir[0];    //每圈脉冲数
    sendbuff[10]:=AMeasureParam.PulsePerCir[1];
    sendbuff[11]:=AMeasureParam.ChoseRange;       //量程选择
    sendbuff[4]:=SizeOf(sendbuff[5])+SizeOf(sendbuff[6])+SizeOf(sendbuff[7])+SizeOf(sendbuff[8])+SizeOf(sendbuff[9])+SizeOf(sendbuff[10])+SizeOf(sendbuff[11]);   //测量参数长度

    end;
    if AMeasureParam.Samplemode=$01 then
    begin
    sendbuff[5]:=AMeasureParam.AChannelNum;       //模拟通道号
    sendbuff[6]:=AMeasureParam.SampleRate[0];     //采样率
    sendbuff[7]:=AMeasureParam.SampleRate[1];
    sendbuff[8]:=AMeasureParam.SampleRate[2];
    sendbuff[9]:=AMeasureParam.SampleRate[3];
    sendbuff[10]:=AMeasureParam.SaveLetter;
    sendbuff[4]:=SizeOf(sendbuff[5])+SizeOf(sendbuff[6])+SizeOf(sendbuff[7])+SizeOf(sendbuff[8])+SizeOf(sendbuff[9])+SizeOf(sendbuff[10]);
    end;
  end;

end;
Result:=sendbuff;

end;
  begin
sum:=0;
InteraBuff:=BuildFrameData(CommandCode, MeasureType, SampleMode,
  DataSize, MeasureSum, MeasureID);
setlength(tempsendbuff,Sizeof(InteraBuff)+6);
tempsendbuff[0]:=$7E;
tempsendbuff[1]:=Sizeof(InteraBuff)+2;
for J := 0 to Sizeof(InteraBuff) - 1 do
begin
  tempsendbuff[2+J]:=InteraBuff[J];
  sum:=sum+interabuff[J];
end;
tempsendbuff[3+J]:=(sum mod 65535);
tempsendbuff[4+J]:=$7F;
Result:=tempsendbuff;


  end;

delphi 中一种好用的数组定义方法以及函数嵌套的使用源代码

标签:delphi   方法   嵌套   

原文地址:http://blog.csdn.net/singular2611/article/details/44955399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!