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

📄 memo_01.pas

📁 演示SPCOMM控件在Delphi中的使用。
💻 PAS
字号:
unit memo_01;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, SPComm, Inifiles;

type
  TForm1 = class(TForm)
    Memo: TMemo;
    BtnSend: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    CBComNO: TComboBox;
    CBBaud: TComboBox;
    CBByteSize: TComboBox;
    CBStopBit: TComboBox;
    CBParity: TComboBox;
    CBFlowControl: TComboBox;
    BtnOpenCom: TButton;
    BtnHelp: TButton;
    CKBDTR: TCheckBox;
    CKBRTS: TCheckBox;
    CKBTimerSend: TCheckBox;
    EdTimerValue: TEdit;
    Label7: TLabel;
    CKBHEXSend: TCheckBox;
    CKBSendNewLine: TCheckBox;
    Label8: TLabel;
    EdInput: TEdit;
    StatusBar1: TStatusBar;
    BtnOpenFile: TButton;
    EdFilename: TEdit;
    BtnSendFile: TButton;
    BtnSaveWindow: TButton;
    BtnClearWindow: TButton;
    CheckBox6: TCheckBox;
    DlgOpenFile: TOpenDialog;
    Comm1: TComm;
    Label9: TLabel;
    DlgSaveWindow: TSaveDialog;
    DlgBackgroundColor: TColorDialog;
    BtnBackgroundColor: TButton;
    procedure BtnSendClick(Sender: TObject);
    procedure BtnClearWindowClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnOpenComClick(Sender: TObject);
    procedure BtnOpenFileClick(Sender: TObject);
    procedure BtnSaveWindowClick(Sender: TObject);
    procedure BtnBackgroundColorClick(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnSendFileClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

var
  Initfile : TIniFile;
  sendfileopenflg : Boolean;
  commopenflg : Boolean;
{$R *.DFM}
procedure TForm1.BtnSendClick(Sender: TObject);
var
   str: string;
begin
  str:=EdInput.Text;
  Comm1.WriteCommData(PChar(str),Length(str));
end;

procedure TForm1.BtnClearWindowClick(Sender: TObject);
begin
  Memo.Lines.Clear();
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  filename:String;
  Str:string;
begin
  filename := ExtractFilePath(paramstr(0)) + 'Startup.ini';
  InitFile:=TIniFile.Create(filename);
  try
    if not FileExists(filename) then
    begin
      InitFile.WriteInteger('Form','Top',self.Top);
      InitFile.WriteInteger('Form','Left',self.Left);
      //InitFile.WriteInteger('Form','Height',self.Height);
      //InitFile.WriteInteger('Form','Width',self.Width);
    end
    else
    begin
        self.Top:=InitFile.ReadInteger('Form','Top',0);
        self.Left:=InitFile.ReadInteger('Form','Left',0);
        //self.Height:=InitFile.ReadInteger('Form','Height',Screen.Height);
        //self.Width:=InitFile.ReadInteger('Form','Width',Screen.Width);
        CBComNO.Text := InitFile.ReadString('Port','NO.',Str);
        CBBaud.Text := InitFile.ReadString('Port','BaudRate',Str);
        CBByteSize.Text := InitFile.ReadString('Port','ByteSize',Str);
        CBStopBit.Text := InitFile.ReadString('Port','Stopbit',Str);
        CBParity.Text := InitFile.ReadString('Port','Parity',Str);
        CBFlowControl.Text := InitFile.ReadString('Port','FlowControl',Str);
    end;
  finally
    InitFile.Free;
  end;
  Memo.Lines.Clear();//清除接收显示窗口
  Comm1.StopComm; //关闭串口
  commopenflg := False;
  Btnsend.Enabled := False;
  Btnsendfile.Enabled := False;
  StatusBar1.Panels[3].Text := CBComNO.Text + ',' + CBBaud.Text + ','
                                + CBByteSize.Text + ',' + CBStopBit.Text + ','
                                + CBParity.Text;
end;

procedure TForm1.BtnOpenComClick(Sender: TObject);
begin
  if BtnOpenCom.Caption = 'Open' then
    begin
      Comm1.CommName := CBComNo.Text;
      if CBparity.Text = 'None' then
        Comm1.Parity := None
      else if CBparity.Text = 'Odd' then
        Comm1.Parity := Odd
      else if CBParity.Text = 'Even' then
        Comm1.Parity := Even;
      Comm1.BaudRate := StrtoInt(CBBaud.Text);
      Comm1.StopBits := TStopBits(StrtoInt(CBStopBit.Text));
      Comm1.ByteSize := TByteSize(StrtoInt(CBByteSize.Text));
      //Comm1.Parity := Tparity(CBParity.Text);
      Comm1.StartComm; //Open Com
      CBComNO.Enabled := False;
      CBBaud.Enabled := False;
      CBByteSize.Enabled := False;
      CBStopBit.Enabled := False;
      CBParity.Enabled := False;
      CBFlowControl.Enabled := False;
      CKBDTR.Enabled := False;
      CKBRTS.Enabled := False;
      Btnsend.Enabled := True;
      if sendfileopenflg then
        BtnSendfile.Enabled := True;
      BtnOpenCom.Caption := 'Close';
      commopenflg := True;
      StatusBar1.Panels[3].Text := CBComNO.Text + ',' + CBBaud.Text + ','
                                + CBByteSize.Text + ',' + CBStopBit.Text + ','
                                + CBParity.Text;
    end
  else
    begin
      Comm1.StopComm;   //Close Com
      commopenflg := False;
      BtnOpenCom.Caption := 'Open';
      CBComNO.Enabled := True;
      CBBaud.Enabled := True;
      CBByteSize.Enabled := True;
      CBStopBit.Enabled := True;
      CBParity.Enabled := True;
      CBFlowControl.Enabled := True;
      CKBDTR.Enabled := True;
      CKBRTS.Enabled := True;
      Btnsend.Enabled := False;
      Btnsendfile.Enabled := False;
    end
end;

procedure TForm1.BtnOpenFileClick(Sender: TObject);
begin
  DlgOpenFile.FileName := '';
  if DlgOpenFile.Execute then
    begin
      //Memo.Lines.LoadFromFile(DlgOpenFile.FileName);
      sendfileopenflg := True;
      if commopenflg then
        BtnSendfile.Enabled := True;
    end
end;

procedure TForm1.BtnSaveWindowClick(Sender: TObject);
begin
  DlgSaveWindow.FileName := '';
  if DlgSaveWindow.Execute then
  begin
    Memo.Lines.SaveToFile(DlgSaveWindow.FileName);
  end
end;

procedure TForm1.BtnSendFileClick(Sender: TObject);
var
  str:string;
  inputfile : TextFile;
begin
  AssignFile(inputfile,DlgOpenFile.FileName);
  reset(inputfile);
  while not eof(inputfile) do
    begin
      readln(inputfile,str);
      Comm1.WriteCommData(PChar(str),Length(str));
    end;
  closefile(inputfile);
  //str := LoadFromFile(DlgOpenFile.FileName);

  //Comm1.WriteCommData()
end;

procedure TForm1.BtnBackgroundColorClick(Sender: TObject);
begin
  if DlgBackgroundColor.Execute then
    Form1.Color := DlgBackgroundColor.Color;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
  var S:string;
begin
  Setlength(S,BufferLength);
  Move(Buffer^,PChar(S)^,BufferLength);
  Memo.Lines.Add(S);
  //Memo.Invalidate;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  filename:string;
begin
  Comm1.StopComm;
  filename := ExtractFilePath(paramstr(0))+'Startup.ini';
  InitFile:=TIniFile.Create(filename);
  InitFile.WriteInteger('Form','Top',self.Top);
  InitFile.WriteInteger('Form','Left',self.Left);
  InitFile.WriteString('Port','No.',CBComNo.Text);
  InitFile.WriteString('Port','BaudRate',CBBaud.Text);
  InitFile.WriteString('Port','ByteSize',CBByteSize.Text);
  InitFile.WriteString('Port','Stopbit',CBStopBit.Text);
  InitFile.WriteString('Port','Parity',CBParity.Text);
  InitFile.WriteString('Port','FlowControl',CBFlowControl.Text);
  //InitFile.WriteInteger('Form','Height',self.Height);
  //InitFile.WriteInteger('Form','Width',self.Width);
  InitFile.Free;
  Action:=CaFree;
end;


end.

⌨️ 快捷键说明

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