unit Unit1;

interface
                                
uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, ExtCtrls, StdCtrls, Buttons, Spin, Mask, 
  ComPort, ComCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    btnClose: TBitBtn;
    ParamPanel: TPanel;
    rgPortIndex: TRadioGroup;
    GroupBox1: TGroupBox;
    bRead: TSpeedButton;
    btnClearReadBuf: TSpeedButton;
    GroupBox2: TGroupBox;
    btnSend: TSpeedButton;
    btnOpenPort: TSpeedButton;
    btnClosePort: TSpeedButton;
    StatusBar: TStatusBar;
    ESendStr: TEdit;
    lbBufRead: TListBox;
    FPort: TComPort;
    btnComProperty: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure bReadClick(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
    procedure btnClearReadBufClick(Sender: TObject);
    procedure btnOpenPortClick(Sender: TObject);
    procedure btnClosePortClick(Sender: TObject);
    procedure FPortReadPacket(const Packet: Pointer; const Size: Integer;
      const ErrCode: Cardinal);
    procedure btnComPropertyClick(Sender: TObject);
    procedure rgPortIndexClick(Sender: TObject);
  private
  public
   procedure SetEnabledControls; 
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ Создание компонента доступа к портам при создании формы }
procedure TForm1.FormCreate(Sender: TObject);
begin
 SetEnabledControls;
end;

{Изменение номера порта}
procedure TForm1.rgPortIndexClick(Sender: TObject);
begin
 FPort.ComNumber:= rgPortIndex.ItemIndex+1;
end;

{Кнопка открытия порта}
procedure TForm1.btnOpenPortClick(Sender: TObject);
begin
 FPort.Close;
 FPort.ComNumber:= rgPortIndex.ItemIndex+1;
 FPort.Open;
 If FPort.Connected then
  StatusBar.Panels[0].Text:= 'Порт успешно открыт'
 Else
  StatusBar.Panels[0].Text:= 'Ошибка открытия порта';
 SetEnabledControls;
end;

procedure TForm1.btnClosePortClick(Sender: TObject);
begin
 FPort.Close;
 StatusBar.Panels[0].Text:= '';
 SetEnabledControls;
 bRead.Down:= False;
end;

procedure TForm1.SetEnabledControls;
Var Active : Boolean;
begin
 Active:= FPort.Connected;
 btnClosePort.Enabled:= Active;
 rgPortIndex.Enabled := not Active;
 btnOpenPort.Enabled := not Active;
 btnComProperty.Enabled:= Active;
 btnSend.Enabled:= Active;
end;


{Кнопка закрытия программы}
procedure TForm1.btnCloseClick(Sender: TObject);
begin
 Close;
end;

{Кнопка опроса порта}
procedure TForm1.bReadClick(Sender: TObject);
begin
 FPort.ReadActive:= bRead.Down;
end;

procedure TForm1.FPortReadPacket(const Packet: Pointer;
  const Size: Integer; const ErrCode: Cardinal);
Var S : String; i : Integer;
begin
 S:= IntToStr(Size) + '>';

 For i:= 0 to Size-1 do begin
  S:= S + Char(Pointer(LongInt(Packet)+i)^);
 End;

 If ErrCode <> 0 then S:= '[!]' + S;
 lbBufRead.Items.Add(S);
end;

{Очистить буфер чтения}
procedure TForm1.btnClearReadBufClick(Sender: TObject);
begin
 lbBufRead.Items.Clear;
end;

{Отправка байта}
procedure TForm1.btnSendClick(Sender: TObject);
begin
 FPort.WriteString(ESendStr.Text);
end;

{Настройка порта}
procedure TForm1.btnComPropertyClick(Sender: TObject);
begin
 FPort.CommDialog;
end;


end.
