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

📄 uport.~pas

📁 利用DELPHI结合研华数据采集卡PCI1711对模拟量和数字量进行采集。
💻 ~PAS
字号:
unit UPort;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Spin, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    GroupBox_OutPut: TGroupBox;
    Edit_PortOutVoltage: TEdit;
    Btn_PortOut: TButton;
    GroupBox_Input: TGroupBox;
    RadioBtn_OutputValue: TRadioButton;
    RadioBtn_OutputVoltage: TRadioButton;
    Edit_PortInCh0: TEdit;
    Btn_PortIn: TButton;
    Edit_PortOutValue: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label_Channel0: TLabel;
    Label_Channel1: TLabel;
    Label_Channel2: TLabel;
    Label_Channel3: TLabel;
    Label_Channel4: TLabel;
    Label_Channel5: TLabel;
    Label_Channel6: TLabel;
    Label_Channel7: TLabel;
    Edit_PortInCh1: TEdit;
    Edit_PortInCh2: TEdit;
    Edit_PortInCh3: TEdit;
    Edit_PortInCh4: TEdit;
    Edit_PortInCh5: TEdit;
    Edit_PortInCh6: TEdit;
    Edit_PortInCh7: TEdit;
    Edit_VoltageInCh0: TEdit;
    Edit_VoltageInCh1: TEdit;
    Edit_VoltageInCh2: TEdit;
    Edit_VoltageInCh3: TEdit;
    Edit_VoltageInCh4: TEdit;
    Edit_VoltageInCh5: TEdit;
    Edit_VoltageInCh6: TEdit;
    Edit_VoltageInCh7: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    UpDown_Voltage: TUpDown;
    Label5: TLabel;
    Edit_PortInCh9: TEdit;
    Label6: TLabel;
    Edit_PortInCh8: TEdit;
    GroupBox_Status: TGroupBox;
    RadioBtn_AutoIO: TRadioButton;
    RadioBtn_ManIO: TRadioButton;
    Timer_IO: TTimer;
    Edit_Interval: TEdit;
    UpDown_Interval: TUpDown;
    Label7: TLabel;
    Label8: TLabel;
    procedure Btn_PortInClick(Sender: TObject);
    procedure Btn_PortOutClick(Sender: TObject);
    procedure UpDown_VoltageClick(Sender: TObject; Button: TUDBtnType);
    procedure RadioBtn_AutoIOClick(Sender: TObject);
    procedure RadioBtn_ManIOClick(Sender: TObject);
    procedure UpDown_IntervalClick(Sender: TObject; Button: TUDBtnType);
    procedure Timer_IOTimer(Sender: TObject);
  private
    { Private declarations }
       InVoltage:Real;          //输入电压

       OutVoltage:Real;       //输出电压
       OutValue:SmallInt;     //输出数值
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//单字节端口输入
Function PortIn(PortNum:Word):Byte;
var
   a:Byte;
begin
     asm
     mov DX,PortNum;
     in AL,DX;
     mov a,AL;
     end;{end of asm}
     PortIn:=a;
end;

//单字节端口输出
Procedure PortOut(PortNum:Word;a:Byte);
begin
     asm
     mov DX,PortNum;
     mov AL,a;
     out DX,AL;
     end;{end of asm}
end;


procedure TForm1.Btn_PortInClick(Sender: TObject);
var
   PortNum:Word;
   InValue:integer;
   InValueH,InValueL:Byte;
   i:integer;
 begin
      for i:=0 to 7 do
      begin
           PortNum:=$342;
           PortOut(PortNum,i);       //设定通道

           PortNum:=$340;
           InValueL:=PortIn(PortNum);       //输入电压低位

           PortNum:=$341;
           InValueH:=PortIn(PortNum);       //输入电压高位

           InValueL:=InValueL div 16;

           InValue:=InValueH*16+InValueL;        //计算输入数值

           //计算输入电压值
           InVoltage:=10*(InValue-2048)/2048;
           //显示电压值
           case i of
           0:
           begin
             Edit_PortInCh0.text:=inttostr(InValue);
             Edit_VoltageInCh0.text:=FormatFloat('0.0',InVoltage);
           end;
           1:
           begin
             Edit_PortInCh1.text:=inttostr(InValue);
             Edit_VoltageInCh1.text:=FormatFloat('0.0',InVoltage);
           end;
           2:
           begin
             Edit_PortInCh2.text:=inttostr(InValue);
             Edit_VoltageInCh2.text:=FormatFloat('0.0',InVoltage);
           end;
           3:
           begin
             Edit_PortInCh3.text:=inttostr(InValue);
             Edit_VoltageInCh3.text:=FormatFloat('0.0',InVoltage);
           end;
           4:
           begin
             Edit_PortInCh4.text:=inttostr(InValue);
             Edit_VoltageInCh4.text:=FormatFloat('0.0',InVoltage);
           end;
           5:
           begin
             Edit_PortInCh5.text:=inttostr(InValue);
             Edit_VoltageInCh5.text:=FormatFloat('0.0',InVoltage);
           end;
           6:
           begin
             Edit_PortInCh6.text:=inttostr(InValue);
             Edit_VoltageInCh6.text:=FormatFloat('0.0',InVoltage);
           end;
           7:
           begin
             Edit_PortInCh7.text:=inttostr(InValue);
             Edit_VoltageInCh7.text:=FormatFloat('0.0',InVoltage);
           end;
           end;//end of case
         end;//end of for i:=0 to 7 do

      PortOut($342,8);       //设定通道8
      InValueL:=PortIn($340);       //输入位置低位
      InValueH:=PortIn($341);       //输入位置高位
      InValue:=InValueH*256+InValueL;        //计算输入数值(编码器旋转位置)
      Edit_PortInCh8.text:=inttostr(InValue);

      PortOut($342,9);       //设定通道9
      InValueL:=PortIn($340);       //输入位置低位
      InValueH:=PortIn($341);       //输入位置高位
      InValue:=InValueH*256+InValueL;        //计算输入数值(编码器旋转速度)
      Edit_PortInCh9.text:=inttostr(InValue);

end;

procedure TForm1.Btn_PortOutClick(Sender: TObject);
var
   PortNum:Word;
   OutValueH,OutValueL:Byte;

begin
     //设定数值输出
     if RadioBtn_OutputValue.checked then
     begin
          OutValue:=strtoint(Edit_PortOutValue.text);
     end;

     //设定电压值输出
     if RadioBtn_OutputVoltage.checked then
     begin
          OutVoltage:=UpDown_Voltage.position /10;
          OutValue:=round(OutVoltage*4095/10);
          Edit_PortOutValue.text:=inttostr(OutValue);
     end;

     //电压输出
     if OutValue>4095 then OutValue:=4095;

     OutValueH:=OutValue div 256;
     OutValueL:=OutValue mod 256;

     PortNum:=$240;
     PortOut(PortNum,OutValueH);
     PortNum:=$241;
     PortOUt(PortNum,OutValueL);

end;

procedure TForm1.UpDown_VoltageClick(Sender: TObject; Button: TUDBtnType);
begin
     Edit_PortOutVoltage.text:=FormatFloat('0.0',UpDown_Voltage.Position / 10);
     OutVoltage:=UpDown_Voltage.position /10;
     OutValue:=round(OutVoltage*4095/10);
     Edit_PortOutValue.text:=inttostr(OutValue);
     //直接输出
     if RadioBtn_AutoIO.checked then Btn_PortOutClick(Sender);
end;

procedure TForm1.RadioBtn_AutoIOClick(Sender: TObject);
begin
     if not Timer_IO.Enabled then Timer_IO.Enabled :=true;
end;

procedure TForm1.RadioBtn_ManIOClick(Sender: TObject);
begin
     if Timer_IO.Enabled then Timer_IO.Enabled :=false;
end;

procedure TForm1.UpDown_IntervalClick(Sender: TObject; Button: TUDBtnType);
begin
     Timer_IO.interval:=UpDown_Interval.position*10;
     Edit_Interval.text:=inttostr(Timer_IO.interval);
end;

procedure TForm1.Timer_IOTimer(Sender: TObject);
begin
     Btn_PortInClick(Sender);
end;

end.

⌨️ 快捷键说明

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