unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, nrclasses, nrcomm, nrbarcode, StdCtrls, nrcommbox;

type
  TForm1 = class(TForm)
    Button1: TButton;
    nrComm1: TnrComm;
    nrDeviceBox1: TnrDeviceBox;
    CheckBox1: TCheckBox;
    ListBox1: TListBox;
    Label1: TLabel;
    eSend: TEdit;
    bSend: TButton;
    procedure nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
      Received: Cardinal);
    procedure Button1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure bSendClick(Sender: TObject);
  private    { Private declarations }
  public     { Public declarations }
    procedure DoSomethingWithPacket(S:string);
  end;

var
  Form1: TForm1;
  DataPacket:Ansistring='';
  EndOfPacket:Ansistring=#13#10;
  idxEOP:integer=1;

implementation

{$R *.dfm}

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  nrComm1.Active:=CheckBox1.Checked;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  nrComm1.ConfigDialog;
end;

procedure TForm1.DoSomethingWithPacket(S:string);
begin
  ListBox1.Items.Add(S);
end;

procedure TForm1.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);
var i:integer;
    ch:AnsiChar;
begin
  for i := 0 to Received - 1 do begin
    ch := PAnsiChar(Buffer)[I];
    if ch = EndOfPacket[idxEOP] then begin
      if idxEOP >= Length(EndOfPacket) then begin
        DoSomethingWithPacket(DataPacket);
        DataPacket := '';
        idxEOP := 1;
      end;
      Inc(idxEOP);
    end else begin
      idxEOP := 1;
      DataPacket := DataPacket + ch;
    end;
  end;
end;

procedure TForm1.bSendClick(Sender: TObject);
begin
  nrComm1.SendString( eSend.Text + EndOfPacket );
end;

end.
