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

📄 unit2.pas

📁 手机串口通信的源代码
💻 PAS
字号:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CPort, WinSkinData, StdCtrls, Buttons, CPortCtl, ExtCtrls,
  ComCtrls, Gauges;

type
  TForm1 = class(TForm)
    GroupBox2: TGroupBox;
    Gauge1: TGauge;
    RichEdit1: TRichEdit;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    sibox: TComComboBox;
    Edit1: TEdit;
    Open_btn: TButton;
    Write_btn: TBitBtn;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Stop_btn: TBitBtn;
    CXport: TComPort;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure Write_btnClick(Sender: TObject);
    procedure Open_btnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure showText(Cl: Tcolor; text: string; bold: boolean = false);
    procedure SetTimeout(RI, RM, RC, WM, WC: integer);
  end;

var
  Form1: TForm1;
  CX_Exit: Boolean;

implementation

{$R *.dfm}

procedure tform1.SetTimeout(RI, RM, RC, WM, WC: integer);
begin
  CXport.Timeouts.ReadInterval := RI; //RI  读取间隙
  CXport.Timeouts.ReadTotalMultiplier := RM; //RM 读多倍
  cxport.Timeouts.ReadTotalConstant := RC; //RC  读总常量  000
  CXport.Timeouts.WriteTotalMultiplier := WM; //WM 写多倍
  Cxport.Timeouts.WriteTotalConstant := WC; //WC 写总常量   0000
end;

procedure TForm1.showText(Cl: Tcolor; text: string; bold: boolean = false);
var
  i, ct, fi: integer;
  s: string;
begin
  try
    ct := form1.RichEdit1.Lines.Count - 1;
  // if ct >= 0 then //>>提示
    for fi := 0 to ct do
    begin
      s := form1.RichEdit1.Lines.Strings[fi];
      if copy(s, 1, 2) = '>>' then
        form1.RichEdit1.Lines.Strings[fi] := '  ' + copy(s, 3, length(s));
    end;

    i := form1.RichEdit1.Lines.Count + 1;
    form1.RichEdit1.SelStart := SendMessage(form1.RichEdit1.Handle,
      EM_LINEINDEX, i, 0);
    form1.RichEdit1.Perform(EM_SCROLLCARET, 0, 0); //}
    form1.RichEdit1.SelAttributes.color := cl; //color

    if bold then
      form1.RichEdit1.SelAttributes.Style := [fsbold] else
      form1.RichEdit1.SelAttributes.Style := [];

    form1.RichEdit1.Lines.Add('>>' + trim(text));
    application.ProcessMessages;
  except
    form1.RichEdit1.Lines.Add('CX ERRO');
  end;
end;

procedure TForm1.Write_btnClick(Sender: TObject);
const data80: array[0..80] of byte = ($55, $4D, $54, $53, $20, $44, $4F, $57, $4E, $4C,
    $4F, $41, $44, $45, $52, $7E, $00, $80, $56, $53, $48, $30, $00, $00, $00, $00,
    $00, $00, $00, $00, $00, $00, $00, $00, $55, $6E, $69, $74, $65, $64, $20, $53,
    $74, $61, $74, $65, $73, $00, $00, $00, $FD, $00, $05, $03, $02, $00, $00, $00,
    $00, $00, $00, $00, $00, $00, $00, $00, $C0, $A8, $14, $28, $00, $00, $00, $00,
    $AA, $49, $6A, $48, $02, $CB, $7E);
var
  mem: tmemorystream;
  filemem: tmemorystream;
  rdbuf, wrbuf: array[0..$FFFF] of byte;
  readbuf: byte;
  size: cardinal;
  i: dword;
  rdi, count: integer;

begin
  CX_Exit := false;
  richedit1.Clear;
 { if not fileexists(edit1.Text) then
  begin
    shows := langs.ReadString('Hint', 'H0', '您选择的字库资料不存在!');
    showtext(clred, shows);
    exit;
  end;
  shows := langs.ReadString('Hint', 'H15', '请一直按住开机键,直到引导下载完成...');
  showtext(clred, shows); }

  try
    CXport.BaudRate := br115200;
    CXport.Port := sibox.Text; {设置com口}
    CXport.DataBits := dbeight; {8个数据位}

    CXport.Connected := true;

    SetTimeout(1, 0, 0, 0, 5000);
    CXport.ClearBuffer(true, true);
    CXport.BaudRate := br9600;
    CXport.CustomBaudRate := 1382400;
    CXport.BaudRate := brCustom;

   // mem := tmemorystream.Create;
    //mem.LoadFromStream(gl_boot);
    //size := mem.Size;
    //filemem := tmemorystream.Create;
    //filemem.LoadFromFile(edit1.Text);


    CXport.ClearBuffer(true, true);
    repeat
      CXport.Write(data80, 81);
      sleep(50);
      rdi := CXport.Read(rdbuf, 6);
      Application.ProcessMessages;
      if CX_Exit then
      begin
        showText(clRed, '中断退出!');
        exit;
      end;
    until (rdi = 6) and (rdbuf[1] = 0);

    count := 0;
    if rdbuf[1] <> 0 then
    begin
      //7E 03 C0 74 36 7E
      wrbuf[0] := $7E;
      wrbuf[1] := $03;
      wrbuf[2] := $C0;
      wrbuf[3] := $74;
      wrbuf[4] := $36;
      wrbuf[5] := $7E;

      repeat

        CXport.Write(wrbuf, 6);
        Sleep(30);
        CXport.Read(rdbuf, 6);
        inc(count);

      until (rdbuf[1] <> 0) or (count > 10);
    end;
                //  7E 03 70 FF 83 7E
    wrbuf[0] := $7E;
    wrbuf[1] := $03;
    wrbuf[2] := $70;
    wrbuf[3] := $FF;
    wrbuf[4] := $83;
    wrbuf[5] := $7E;
    CXport.Write(wrbuf, 6);
    cxport.Read(rdbuf, 130);

      //7E 02 EE 00 00 10 00 00 4C 07 03 49 21 7E

    wrbuf[0] := $7E;
    wrbuf[1] := $02;
    wrbuf[2] := $EE;
    wrbuf[3] := 0;
    wrbuf[4] := 0;
    wrbuf[5] := $10;
    wrbuf[6] := 0;
    wrbuf[7] := 0;
    wrbuf[8] := $4C;
    wrbuf[9] := $07;
    wrbuf[10] := $03;
    wrbuf[11] := $49;
    wrbuf[12] := $21;
    wrbuf[13] := $7E;
    CXport.Write(wrbuf, 14);

    CXport.Read(rdbuf, 6);

  finally
    begin
      CXport.ClearBuffer(true, true);
      CXport.Connected := false;
    end;


  end;
end;

procedure TForm1.Open_btnClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    edit1.Text := OpenDialog1.FileName;
  end;
end;

end.

⌨️ 快捷键说明

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