⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8.7.txt

📁 六路三十二次谐波信号发生器 可产生三十次以下任何谐波叠加的波形
💻 TXT
字号:
unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,math,StrUtils, StdCtrls, OleCtrls, MSCommLib_TLB, XPMan;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    Label2: TLabel;
    MSComm1: TMSComm;
    Memo1: TMemo;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Label3: TLabel;
    Label4: TLabel;
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Button3: TButton;
    Label5: TLabel;
    Label6: TLabel;
    Memo2: TMemo;
    Button8: TButton;
    Memo3: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListBox2Click(Sender: TObject);
    procedure MSComm1Comm(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  dataout:array of integer;
  dataout1:array[0..94] of double;
  dataout2:array[0..94] of integer;
  b:integer; //显示序号
  m:integer;//谐波的个数
  k:array of integer;//谐波次数
  l:array of integer;//谐波幅度
  p:array of integer;//相角
  w:integer;//采样点数
  v:integer;//时间点
  
implementation

{$R *.dfm}
uses functions;
procedure TForm1.FormCreate(Sender: TObject);
var
  i:integer;
  begin
  m:=0;     //谐波数为零
  w:=64;    //采64个点
  button3.Enabled:=false;
  setlength(dataout,95);  //保存95个数据

  //初始化listbox1
  for i:=1 to 32 do
    if i<10 then
     begin
     listbox1.Items.add('0'+inttostr(i)+'次谐波');
     end
    else
     begin
     listbox1.Items.add(inttostr(i)+'次谐波');
     end;


//测试数据
// for i:=0 to 94 do
//    begin
//    dataout[i]:=round(sin(PI*i/47)*127+128);
//   memo1.lines.add(inttostr(dataout[i]));
//  end;
//测试数据

  end;
procedure TForm1.Button1Click(Sender: TObject);
var
  i : integer;
  b:integer;
  cc:boolean;
  begin
       //添加选中波形
  for i := 0 to listbox1.Count -1 do
    begin
    if listbox1.Selected[i] then
      begin
      for b:=0 to listbox2.Count-1 do
        begin
        //判断是否已经添加过
        if listbox2.Items[b]=listbox1.Items[i] then
          begin
          cc:=true
          end;
        end;

     if cc=true then
      begin
      cc:=false;
      end
     else
      begin
      listbox2.Items.Add(listbox1.Items[i]);
      m:=m+1;
      Label1.Caption:=inttostr(m);
      end;

    end;
  end;
 end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i : integer;
  begin

  //防止在无删除项目时M减一
  for i := 0 to listbox2.Count -1 do
    begin
    if listbox2.Selected[i] then
     begin
     m:=m-1;
     end;
    end;
    Label1.Caption:=inttostr(m);
    //删除选中的项目
    listbox2.Items.Delete(listbox2.ItemIndex);
 end;

procedure TForm1.ListBox2Click(Sender: TObject);
var
  i:integer;
  str:string;
  begin
//    if ok=true then
//      begin
      //显示当前选中波次
      button3.Enabled:=true;
        for i := 0 to listbox2.Count -1 do
          begin
          if listbox2.Selected[i] then
             begin
              edit1.Text:=inttostr(l[i]);
              edit2.Text:=inttostr(p[i]);
              str:=trim(ListBox2.Items[i]);
              str:=ansireplacestr(str,'次谐波','');
              Label2.Caption:=str;
             end;
          end;
    //  end;
  end;

procedure TForm1.MSComm1Comm(Sender: TObject);

var
  buffer: Olevariant;//MSComm1.InputMode = comInputModeBinary
  i: integer;

  strr:string;
  cc:string;
  tempvalue:real;
  endt:int64;
  r:real;
begin

  if(MSComm1.CommEvent=comEvReceive) then //串行接收事件处理
  begin
           buffer := MSComm1.Input;//读出后会自动清除接收缓冲区,buffer[0]~buffer[31]
          strr:='';

        //  for i := 0 to MSComm1.RThreshold - 1 do //32字节Hex转换
           strr:= strr + inttohex(buffer[0], 2);
            memo2.Lines.Add(inttostr(strtoint('$'+strr)));  //显示接收数据
            cc:='                ';
            b:=b+1;
           strr:=inttohex(b,2)+cc+strr;

  end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
i:integer;
ii:integer;
str:string;
begin
  setlength(k,m);   //设置波次
  setlength(l,m);   //设置幅度
  setlength(p,m);   //设置相角

 // ok:=true;
  button1.Enabled:=false;         //不可再进行波形选择
  button2.Enabled:=false;
  button3.Enabled:=true;

  for i := 0 to listbox2.Count -1 do
  begin
    l[i]:=1;               //确定被选中波形的波次
    p[i]:=0;
    str:=trim(listbox2.Items[i]);
    str:=ansireplacestr(str,'次谐波','');
    k[i]:=strtoint(str);

  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i:integer;
  begin
  for i := 0 to listbox2.Count -1 do
    begin                          //更改提交的数据
    if listbox2.Selected[i] then
      begin
      l[i]:=strtoint(edit1.Text);
      p[i]:=strtoint(edit2.Text);
      end;
    end;
  end;

procedure TForm1.Button5Click(Sender: TObject);
  begin
 // ok:=false;    //重置
  button1.Enabled:=true;
  button2.Enabled:=true;
  button3.Enabled:=false;
  listbox2.Items.Clear;
  mscomm1.PortOpen:=false;
  
  memo1.Lines.Clear;
  memo2.Lines.Clear;
  memo3.Lines.Clear;

  l:=0;
  k:=0;
  m:=0;
  end;

procedure TForm1.Button6Click(Sender: TObject);
var
  str:string;
  osenddata:variant;
  osendstr:olevariant;
  i:integer;
  g:integer;

begin

  //显示各个值是否正确
   memo1.Lines.Clear;
    for i:=0 to m-1 do
     begin

     memo1.lines.add('波次'+inttostr(i)+'   '+inttostr(k[i]));
     memo1.lines.add('幅值'+inttostr(i)+'   '+inttostr(l[i]));
     memo1.lines.add('相角'+inttostr(i)+'   '+inttostr(p[i]));
     end;
    memo1.lines.add('总数'+'    '+inttostr(m));

  /////////////////////////////////////////

  //清除原数据
  for i:=0 to 94 do
      begin
      dataout1[i]:=0;
      dataout2[i]:=0;
      dataout[i]:=0;
      end;
   //波形数据计算
    for i:=0 to 94 do
      begin
        for v:=0 to m-1 do
        begin
        dataout1[i]:=dataout1[i]+l[v]*sin(PI*k[v]*i/47);
        end;
      end;

      //求最大值
      for i:=0 to 94 do
        begin
        dataout2[i]:=round(dataout1[i]);
        end;
   g:=GetMaxInArray(dataout2);   //数值处理成0-255
   memo1.lines.add('g'+'   '+inttostr(g)); //显示g值

   //求最后结果
   for i:=0 to 94 do
   begin
   dataout[i]:=round(round(dataout1[i]*127)/g)+128;
   end;

  ////////////////////////////////////////
   //数据转化,为的是让硬件识别,仿真无需要变换类型输出
  for  i:=0 to 94 do
    str:=str+inttoHEX(dataout[i],2);
    osenddata:=vararraycreate([0,(length(str) div 2) -1],varbyte);
      for  i:=1 to (length(str) div 2) do
        begin
        osenddata[i-1]:=strtoint('$'+(str[2*i-1]+str[2*i]));
        memo3.lines.add(inttostr(osenddata[i-1]));
        end;
      osendstr:=osenddata;

      try
        mscomm1.output:=osendstr;
      except
      end;


 //显示发送的数据
      for  i:=0 to 94 do
      begin
     // memo1.lines.add(inttostr(dataout[i]));
      end;

  end;

procedure TForm1.Button7Click(Sender: TObject);
begin
      if(mscomm1.PortOpen=true) then mscomm1.portopen:=false;
      mscomm1.CommPort:=3;
      mscomm1.InBufferSize:=256;
      mscomm1.OutBufferSize:=256;
      mscomm1.Settings:='9600,n,8,1';
      mscomm1.InputLen:=0;
      mscomm1.InBufferCount:=0;
      mscomm1.OutBufferCount:=0;
      mscomm1.RThreshold:=1;
      mscomm1.InputMode:=comINputmodeBinary;
      mscomm1.PortOpen:=true;
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
memo2.Lines.Clear;
memo3.Lines.Clear;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -