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;

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。