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

📄 unit1.pas

📁 串口通信 基本的运算 与下位机通信实现对下位机的控制与监控
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, Buttons, StdCtrls, Spin, ToolWin, OleCtrls,
  MSCommLib_TLB;

type
  TMainForm = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Panel2: TPanel;
    Panel3: TPanel;
    voltage: TEdit;
    current: TEdit;
    filum: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    StatusBar1: TStatusBar;
    shpgas: TShape;
    shptrans: TShape;
    shpabnormal: TShape;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    MSComm: TMSComm;
    Timer1: TTimer;
    BitBtn2: TBitBtn;
    shpfilum: TShape;
    Panel7: TPanel;
    heap: TEdit;
    Label12: TLabel;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Timer2: TTimer;
    Label10: TLabel;
    Label11: TLabel;
    ToolBar1: TToolBar;
    chckbxSerial: TCheckBox;
    BitBtn1: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    Edit2: TEdit;
    Edit3: TEdit;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    BitBtn5: TBitBtn;
    BitBtn6: TBitBtn;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    procedure MSCommComm(Sender: TObject);
    procedure chckbxSerialClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure BitBtn3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn3MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn4MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn4MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn6Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
      InputString:String;
      OutputString :Char;
      timerflag    :boolean;
      old_set_voltage,old_set_current : real;
  end;

var
  MainForm: TMainForm;

//绘图
  step:integer;
  dot:integer;         // 当前绘图点

//串口
  serialopen:boolean;
  receive_data:boolean;
implementation

{$R *.dfm}
//====================================================================
//********处理控件的该事件,获取底层交换的数据和连线的状态************
//====================================================================
procedure TMainForm.MSCommComm(Sender: TObject);
var
i :  integer;
h :  double;
s :  String;
begin
    if  MSComm.CommEvent=ComEvReceive then     //发生接受事件
    begin
        InputString := InputString+MSComm.Input;
//        MainForm.mmReceive.Text :=MainForm.mmReceive.Text +InputString;
       if (Length(InputString)=14) then
       begin
//-------------------------------------------------------------------------
//故障处理及有效数据显示
//-------------------------------------------------------------------------

         for  i:=1 to 14 do
            begin
              if (InputString[i]='g' ) then
               shpgas.Brush.Color := clred
               else
               if (InputString[i]='f')  then
               shpfilum.Brush.Color := clred;
            end;
            
         case InputString[1] of

          'g' :   shpgas.Brush.Color := clred;

          'f' :   shpfilum.Brush.Color := clred;
          's' :
             begin

             filum.Text :=InputString[2] ;
             filum.Text :=  filum.Text+InputString[3] ;
             filum.Text :=  filum.Text+InputString[4] ;
             filum.Text :=  filum.Text+InputString[5] ;
             filum.Text :=  filum.Text+InputString[6] ;

             voltage.Text :=InputString[7] ;
             voltage.Text :=  voltage.Text+InputString[8] ;
             voltage.Text :=  voltage.Text+InputString[9] ;
             voltage.Text :=  voltage.Text+InputString[10] ;

             current.Text :=InputString[11] ;
             current.Text :=  current.Text+InputString[12] ;
             current.Text :=  current.Text+InputString[13] ;
             current.Text :=  current.Text+InputString[14] ;


             OutputString := 'u';
             //------------------
          h := strtofloat(filum.text);
          h := h*162.86;  //焊丝堆积量cm3/h
          s := floattostr(h);
          heap.Text := s[1];
          heap.Text := heap.Text+s[2];
          heap.Text := heap.Text+s[3];
          heap.Text := heap.Text+s[4];
          //-------------------
             end;
          end;
          InputString :='';

          end;
          MSComm.Output:= OutputString;
     end;
end;

//=========================================================================
//窗体创建时的默认列程
//=========================================================================
 procedure TMainForm.FormCreate(Sender: TObject);
begin

//***********故障标志为白色**********************
shptrans.Brush.Color := clWhite;
shpgas.Brush.Color := clWhite;
shpfilum.Brush.Color := clWhite;
shpabnormal.Brush.Color := clWhite;
dot := round(image1.left);
step := 1;
edit2.Text := '24';
edit3.Text := '230' ;
end;

//============================================================================
//串口开关控制
//============================================================================
procedure TMainForm.chckbxSerialClick(Sender: TObject);
begin
    if chckbxSerial.checked then
        begin
        //打开串口
        MSComm.CommPort :=1;
        //默认值为 '9600,N,8,1'
        MSComm.Settings:='9600,N,8,1';
        MSComm.PortOpen:=True;
//        shptrans.Brush.Color := clred;
        serialopen:=true;
        receive_data:=false;
        OutputString := 'u';
        MSComm.Output:= OutputString;
        old_set_voltage:=0;
        end
    else begin
        MSComm.PortOpen:=False;       //关闭串口
        shptrans.Brush.Color := clWhite;
        serialopen:=false;
    end;
end;
{
//===========================================================================
//清显示区
//===========================================================================
procedure TMainForm.Button1Click(Sender: TObject);
begin
    mmReceive.Text:='';
end;
}
//============================================================================
//绘图程序,节拍有定时器1确定
//============================================================================
procedure TMainForm.Timer1Timer(Sender: TObject);
var
tempv,tempc:single;
x1,y1,sc,sv      :integer;
begin


//---------------------------------------------------------------------------
//绘电压曲线
//---------------------------------------------------------------------------

   if (serialopen=true) and (voltage.text<>'') then
   begin
   tempv := strtofloat(voltage.text);
//使用截断函数将实型转换成整形
    sv :=115- trunc(115/73*tempv);      //最高显示电压73分成115份
   image1.Canvas.Pixels[dot,sv] := cllime;

   //连接跳变点,填充曲线以下区域
   image1.Canvas.Pen.Color := cllime;
   image1.Canvas.Pen.Width := 1;
   image1.Canvas.Pen.Style := pssolid;
   image1.Canvas.LineTo(dot,sv);
   image1.Canvas.LineTo(dot,115);
  end;

//----------------------------------------------------------------------------
//绘电流曲线
//----------------------------------------------------------------------------

   if (serialopen=true) and (current.text<>'') then
   begin
     tempc := strtofloat(current.text);
//使用截断函数将实型转换成整形
    sc :=115- trunc(115/650*tempc);        //最高显示电流650分成115份
   image2.Canvas.Pixels[dot,sc] := cllime;

   //连接跳变点,填充曲线以下区域
   image2.Canvas.Pen.Color := cllime;
   image2.Canvas.Pen.Width := 1;
   image2.Canvas.Pen.Style := pssolid;
   image2.Canvas.LineTo(dot,sc);
   image2.Canvas.LineTo(dot,115);
   end;

//-----------------------------------------------------------------------------
//绘送丝速度曲线
//----------------------------------------------------------------------------
    if (serialopen=true) and (filum.text<>'') then
   begin
     tempc := strtofloat(filum.text);
//使用截断函数将实型转换成整形
    sc :=115- trunc(115/30*tempc);             //最高送丝速度15分成115份
   image3.Canvas.Pixels[dot,sc] := cllime;

   //连接跳变点,填充曲线以下区域
   image3.Canvas.Pen.Color := cllime;
   image3.Canvas.Pen.Width := 1;
   image3.Canvas.Pen.Style := pssolid;
   image3.Canvas.LineTo(dot,sc);
   image3.Canvas.LineTo(dot,115);
   end;

//-----------------------------------------------------------------------------
//绘焊丝堆积量曲线
//----------------------------------------------------------------------------
    if (serialopen=true) and (heap.text<>'') then
   begin
     tempc := strtofloat(heap.text);
//使用截断函数将实型转换成整形
    sc :=115- trunc(115/75*tempc);             //最高堆积量75分成115份
   image4.Canvas.Pixels[dot,sc] := cllime;

   //连接跳变点,填充曲线以下区域
   image4.Canvas.Pen.Color := cllime;
   image4.Canvas.Pen.Width := 1;
   image4.Canvas.Pen.Style := pssolid;
   image4.Canvas.LineTo(dot,sc);
   image4.Canvas.LineTo(dot,115);
   end;
//*************************************************************************
//判断是否越界
//*************************************************************************
   dot := dot+step;
   if dot>round(image1.Width) then
   begin
//----------------------------------------------------------------------------
//重绘电压坐标
//----------------------------------------------------------------------------
//搽除绘图区所有曲线
   dot := round(image1.left+20);
   image1.Canvas.Pen.Color := clblack;
   image1.Canvas.Pen.Width := image1.Height ;
   image1.Canvas.Pen.Style := pssolid;
   image1.Canvas.MoveTo(image1.Left ,image1.Height);
   image1.Canvas.LineTo(round(image1.width),round(image1.Height));

   image1.Canvas.Brush.Color := clblack;
   image1.Canvas.Pen.Color := clred;
   image1.Canvas.Pen.Width := 1;
   image1.Canvas.Pen.Style := pssolid;
   image1.Canvas.FillRect(rect(0,0,round(image1.width),round(image1.height)));

//重绘坐标
   x1 := round(image1.left+20);
   y1 := round(image1.height-14);
   image1.Canvas.MoveTo(x1,y1);
   image1.Canvas.LineTo(round(image1.width),y1);
   image1.Canvas.MoveTo(x1,y1);
   image1.Canvas.LineTo(x1,round(image1.top));

//写坐标值
   image1.Canvas.Font.Color := clGreen;
   image1.Canvas.Font.Size  := 8;
   image1.Canvas.TextOut(x1-20  ,y1-10 ,'00' );
   image1.Canvas.TextOut(x1-20  ,y1-30 ,'15' );
   image1.Canvas.TextOut(x1-20  ,y1-50 ,'30' );
   image1.Canvas.TextOut(x1-20  ,y1-70 ,'45' );
   image1.Canvas.TextOut(x1-20  ,y1-90 ,'60' );
   image1.Canvas.TextOut(x1-20  ,y1-110 ,'75' );

   //重定位
   image1.Canvas.MoveTo(image1.left+20,image1.height-14);

//----------------------------------------------------------------------------
//重绘电流坐标

⌨️ 快捷键说明

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