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

📄 unit2.pas

📁 在Delphi 環境下編寫的串口調試程序 ,能與下位機(MSP430F147)實現串口485通訊.完成對下位機狀態的檢測.校準. 對於使用Delphi的串口編程有一定的作用.
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, SPComm;

type
  TForm2 = class(TForm)
    Label1: TLabel;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    ComboBox5: TComboBox;
    ComboBox6: TComboBox;
    ComboBox7: TComboBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Comm1: TComm;
    ComboBox1: TComboBox;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation
  uses    Unit1, Unit3,MATH;
{$R *.dfm}


procedure TForm2.BitBtn2Click(Sender: TObject);
var BaudRate :integer;
begin

      Comm1.CommName:=ComboBox1.Text;                    //1

      ComboBox2.ItemIndex := 0;                          //2

      Comm1.ByteSize :=  TByteSize(ComboBox3.ItemIndex); //3

      Comm1.BaudRate := StrToInt(ComboBox4.Text);        //4

      Comm1.StopBits := TStopBits(ComboBox5.ItemIndex);  //5

      ComboBox6.ItemIndex := 0;                          //6
      Comm1.Parity := None;

      if TryStrToInt(ComboBox4.Text,BaudRate) then
           Comm1.BaudRate := BaudRate;

       // Comm1.ByteSize :=  TByteSize(ComboBox3.ItemIndex);
       // Comm1.StopBits := TStopBits(ComboBox5.ItemIndex);
       // Comm1.Parity := TParity(ComboBox4.ItemIndex);

      //  ComboBox1.ItemIndex := 0;
      //  Comm1.CommName := ComboBox1.Text;

      //  ComboBox6.ItemIndex := 2;
        //.BaudRate := StrToInt(ComboBox6.Text);

       // ComboBox4.ItemIndex := 0;
      //  Comm1.Parity := None;

      //  ComboBox3.ItemIndex := 0;
        //Comm1.ByteSize := _8;

        //ComboBox5.ItemIndex := 0;
       // Comm1.StopBits := _1;


         
  if not TryStrToInt(ComboBox4.Text,BaudRate) then
     begin
     Application.MessageBox('波特率设定有误'+#13+
                             ' 请重新输入','警告',MB_ICONWARNING or MB_OK);
    // ComboBox6.SetFocus;
     exit;
     end;

    Comm1.StartComm;
    ComHaveSet:=55;
    close;
end;

procedure TForm2.BitBtn1Click(Sender: TObject);
begin

        ComboBox1.ItemIndex := 0;
        Comm1.CommName := ComboBox1.Text;

        ComboBox2.ItemIndex := 0;

        ComboBox3.ItemIndex := 3;
        Comm1.ByteSize := _8;

        ComboBox4.ItemIndex := 1;
     //   Comm1.BaudRate := 9600;

        ComboBox5.ItemIndex := 0;
        Comm1.StopBits := _1;

        ComboBox6.ItemIndex := 0;
        Comm1.Parity := None;

        ComboBox7.ItemIndex := 0;




end;

procedure TForm2.FormCreate(Sender: TObject);
begin
ComboBox1.ItemIndex := 0;
        Comm1.CommName := ComboBox1.Text;

        ComboBox2.ItemIndex := 0;

        ComboBox3.ItemIndex := 3;
        Comm1.ByteSize := _8;

        ComboBox4.ItemIndex := 1;
     //   Comm1.BaudRate := 9600;

        ComboBox5.ItemIndex := 0;
        Comm1.StopBits := _1;

        ComboBox6.ItemIndex := 0;
        Comm1.Parity := None;

        ComboBox7.ItemIndex := 0;

end;

       function StrToHexStr(const S:string):string;
//字符串转换成16进制字符串
var
  I:Integer;
begin
  for I:=1 to Length(S) do
  begin
    if I=1 then
      Result:=IntToHex(Ord(S[1]),2)
    else Result:=Result+' '+IntToHex(Ord(S[I]),2);
  end;
end;

// #define CoefficientV   0.000404       /*2V电池电压的系数*********************/
//#define COEFFICIENT_6V   0.001221       /*6V电池电压的系数*********************/
//#define COEFFICIENT_12V  0.002394       /*12V电池电压的系数********************/
//#define COEFFICIENT_T    0.045754       /*温度的系数***************************/

Const COEFFICIENT_2V=0.000404;
Const COEFFICIENT_6V = 0.001221;
Const COEFFICIENT_12V = 0.002394;
Const COEFFICIENT_T =   0.045754 ;


procedure TForm2.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
type
   IArr = ^integer;
var
  aa: array [1..2048] of byte;
 // liao:     array [1..500] of byte;
  //i, n, nFileNum: integer;
  i: integer;
  //OpenMode:Word;
 // buf: Pointer;
 // bFlag: Boolean;
 // aFile: TFileStream;
  CommrevStr: shortstring;
  liaoxumingtest :integer;

   DianChiData:double;
   DianChiDataXiao:double;
   CoefficientV:double;

  viewString: string;
begin
  try
    viewString := '';
    CoefficientV:=0;
   // Memo1.Lines.Clear;
    Move(IArr(buffer)^, aa, bufferLength);

    viewString := '接收:  ';
    for I := 1 to BufferLength do
    begin
      CommrevStr[i] := Char(aa[i]);
     // liao[i]:= Char(aa[i]);

      viewstring := viewstring + ' '+ IntToHex(aa[i], 2)+'';
    end;
  //  Form1.Memo1.Lines.Add('Recv Length:' + IntToStr(BufferLength));

    //  Form1.Label2.Caption:= IntToHex(aa[1],);
   // Form1.Label2.Caption:= IntToHex(aa[1], 2)+IntToHex(aa[3], 2);
    //将整数转换为十六进制字符串,          IntToHex(int Value, int Digits)
  //   liaoxumingtest:= aa[1] *256 +1  ;// StrToInt(  IntToHex(aa[1], 2));
     // Form1.Label3.Caption:= IntToStr(aa[1] *256 +1);

       if  JiaoYangJianChe=55 then
           begin
               JiaoYangJianChe:=0;

               liaoxumingtest:= aa[10] *256 +aa[9];
               Form1.Label2.Caption:= IntToStr(liaoxumingtest);
               if (liaoxumingtest>386)or (liaoxumingtest<176)  then
                       begin
                              Form1.Label2.Font.Color:=clRed;
                    Application.MessageBox('不得了 不得了'+#13+
                             ' 出轨了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
                       end
               else
                       begin
                              Form1.Label2.Font.Color:=clWindowText;
                       end;


               liaoxumingtest:= aa[12] *256 +aa[11];
               Form1.Label3.Caption:= IntToStr(liaoxumingtest);
               if (liaoxumingtest>3918)or (liaoxumingtest<3780)  then
                       begin
                              Form1.Label3.Font.Color:=clRed;
                              Application.MessageBox('不得了 不得了'+#13+
                             ' 出轨了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
                       end
               else
                       begin
                              Form1.Label3.Font.Color:=clWindowText;
                       end;


               liaoxumingtest:= aa[14] *256 +aa[13];
               Form1.Label4.Caption:= IntToStr(liaoxumingtest);
               if (liaoxumingtest>306)or (liaoxumingtest<96)  then
                       begin
                              Form1.Label4.Font.Color:=clRed;
                              Application.MessageBox('不得了 不得了'+#13+
                             ' 出轨了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
                       end
               else
                       begin
                              Form1.Label4.Font.Color:=clWindowText;
                       end;


               liaoxumingtest:= aa[16] *256 +aa[15];
               Form1.Label5.Caption:= IntToStr(liaoxumingtest);
               if (liaoxumingtest>3999)or (liaoxumingtest<3789)  then
                       begin
                              Form1.Label5.Font.Color:=clRed;
                              Application.MessageBox('不得了 不得了'+#13+
                             ' 出轨了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
                       end
               else
                       begin
                              Form1.Label5.Font.Color:=clWindowText;
                       end;


               liaoxumingtest:= aa[18] *256 +aa[17];
               Form1.Label6.Caption:= IntToStr(liaoxumingtest);
               if (liaoxumingtest>324)or (liaoxumingtest<104)  then
                       begin
                              Form1.Label6.Font.Color:=clRed;
                              Application.MessageBox('不得了 不得了'+#13+
                             ' 出轨了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
                       end
               else
                       begin
                              Form1.Label6.Font.Color:=clWindowText;
                       end;


               liaoxumingtest:= aa[20] *256 +aa[19];
               Form1.Label7.Caption:= IntToStr(liaoxumingtest);
               if (liaoxumingtest>3900)or (liaoxumingtest<3790)  then
                       begin
                              Form1.Label7.Font.Color:=clRed;
                              Application.MessageBox('不得了 不得了'+#13+
                             ' 出轨了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
                       end

⌨️ 快捷键说明

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