unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.Samples.Spin, Vcl.ComCtrls;

type
  TKeller_Demo_Form = class(TForm)
    ComPort_Combo: TComboBox;
    status_Shape: TShape;
    open_close_btn: TButton;
    baud_rate_Combo: TComboBox;
    Basic_group: TGroupBox;
    Label1: TLabel;
    dev_lbl: TLabel;
    sn_lbl: TLabel;
    f48_Button: TButton;
    addr_ed: TSpinEdit;
    get_serial_Button: TButton;
    read_pressure_group: TGroupBox;
    Timer1: TTimer;
    start_stop_Button: TButton;
    P1_title: TLabel;
    P1_display_lbl: TLabel;
    TOB1_title: TLabel;
    TOB1_display_lbl: TLabel;
    meas_rate_TrackBar: TTrackBar;
    fast_title: TLabel;
    slow_title: TLabel;
    conductivity_raw_title: TLabel;
    conductivityraw_display_lbl: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure open_close_btnClick(Sender: TObject);
    procedure baud_rate_ComboChange(Sender: TObject);
    procedure f48_ButtonClick(Sender: TObject);
    procedure get_serial_ButtonClick(Sender: TObject);
    procedure start_stop_ButtonClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure meas_rate_TrackBarChange(Sender: TObject);
  private
    { Private declarations }
    _bEcho: Boolean;
    f_port_opened : Boolean;
    f_com_speed : Integer;

     f_class : Byte;
     f_group : byte;
     f_year : Byte;
     f_week : Byte;

    procedure show_port_open_status;
    function  get_answer_code(stat : Integer) : String;
  public
    { Public declarations }
  end;

var
  Keller_Demo_Form: TKeller_Demo_Form;

implementation

{$R *.dfm}

uses Interface_S30c;

//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.FormCreate(Sender: TObject);
//------------------------------------------------------------------------------
VAR
 i : Integer;
begin
   _bEcho:=False;
   f_port_opened :=false;

   ComPort_Combo.Items.Clear;
   for I := 1 to 99 do
     begin
      ComPort_Combo.Items.Add('COM'+IntToStr(i));
     end;

    ComPort_Combo.ItemIndex:=0;

    case baud_rate_Combo.ItemIndex OF
     0 : f_com_speed:=9600;
     1 : f_com_speed:=115200;
    end;

end;



//------------------------------------------------------------------------------
procedure  TKeller_Demo_Form.show_port_open_status;
//------------------------------------------------------------------------------
 begin
  case f_port_opened of
  True :  Begin
            open_close_btn.Caption:='Close';
            status_Shape.Brush.Color:=clLime;
          End;

  False : Begin
             open_close_btn.Caption:='Open';
             status_Shape.Brush.Color:=clGray;
          End;
  end;


 end;




//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.baud_rate_ComboChange(Sender: TObject);
//------------------------------------------------------------------------------
begin
    case baud_rate_Combo.ItemIndex OF
     0 : f_com_speed:=9600;
     1 : f_com_speed:=115200;
    end;
end;

//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.open_close_btnClick(Sender: TObject);
//------------------------------------------------------------------------------
VAR
 ans : Integer;
begin

 case f_port_opened of
  True :  Begin
           CloseComPort;

           f_port_opened:=False;
           show_port_open_status;
          End;

  False : Begin

          ans:= OpenComExt(ComPort_Combo.ItemIndex+1,500,f_com_speed,0);

           if ans=RS_OK then
            begin
              f_port_opened:=True;
              show_port_open_status;
            end;
          End;

 end;
end;

//------------------------------------------------------------------------------
function TKeller_Demo_Form.get_answer_code(stat: Integer): String;
//------------------------------------------------------------------------------
begin
  case stat of
       RS_EX1 : Result:='RS_EX1';
       RS_EX2 : Result:='RS_EX2';
       RS_EX3 : Result:='RS_EX3';
       RS_EX32 : Result:='RS_EX32';
       RS_ERROR : Result:='RS_ERROR';
       RS_TXERROR  : Result:='RS_TXERROR';
       RS_RXERROR  : Result:='RS_RXERROR';
       RS_TIMEOUT :  Result:='RS_TIMEOUT';
       RS_BADDATA :  Result:='RS_BADDATA';
      end;
end;




//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.f48_ButtonClick(Sender: TObject);
//------------------------------------------------------------------------------
 Var
  answer : SmallInt;
  buf, status : Byte;
begin
if f_port_opened then
 begin
  answer:=F48(addr_ed.Value,f_class,f_group,f_year,f_week, buf, status);

   if answer=RS_OK then
    begin
     dev_lbl.Caption:='Class:'+IntToStr(f_class)+' Group:'+IntToStr(f_group)+' Year:'+IntToStr(f_year)+' Week:'+IntToStr(f_week) +' StatusByte='+IntToStr(status);
    end
     else
      begin
        dev_lbl.Caption:='Error communication '+get_answer_code(answer);
      end;


 end
  else  Showmessage('COM Port is closed!');

end;

//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.get_serial_ButtonClick(Sender: TObject);
//------------------------------------------------------------------------------
var
   b0, b1, b2, b3, b4, b5: byte;
   answer : SmallInt;
   SN: longint;
begin
  if f_port_opened then
    begin
     answer:=F100(addr_ed.Value,1, b0, b1, b2, b3, b4);
      if answer=RS_OK then
        begin
          SN:= b3+256*b2+256*256*b1+256*256*256*b0;
          sn_lbl.Caption:='SN : '+IntToStr(SN);
        end
         else
          Begin
           sn_lbl.Caption:='Error communication '+get_answer_code(answer);
          End;
    end
    else  Showmessage('COM Port is closed!');
end;




//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.start_stop_ButtonClick(Sender: TObject);
//------------------------------------------------------------------------------
begin
  Timer1.Enabled:=Not(Timer1.Enabled);

  case Timer1.Enabled of
   True :  start_stop_Button.Caption:='Stop';
   False : start_stop_Button.Caption:='Start';
  end;


end;

//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.Timer1Timer(Sender: TObject);
//------------------------------------------------------------------------------
var
   status : byte;
   answer : SmallInt;
   measurement_value : Single;
begin
   // Read pressure channel P1 from device
   answer:=F73(addr_ed.Value,1,measurement_value,status);
     if answer=RS_OK then
        begin
          P1_display_lbl.Caption:=FloatToStrF(measurement_value,ffFixed,12,3);
        end
         ELSE
          P1_display_lbl.Caption:='Error communication '+get_answer_code(answer);



  // Read temperature channel TOB1 from device
   answer:=F73(addr_ed.Value,4,measurement_value,status);
     if answer=RS_OK then
        begin
          TOB1_display_lbl.Caption:=FloatToStrF(measurement_value,ffFixed,12,1);
        end
         ELSE
          TOB1_display_lbl.Caption:='Error communication '+get_answer_code(answer);

  // read conductivity raw value ( CH# 11 )

   answer:=F73(addr_ed.Value,11,measurement_value,status);
     if answer=RS_OK then
        begin
          conductivityraw_display_lbl.Caption:=FloatToStrF(measurement_value,ffFixed,12,3);
        end
         ELSE
          conductivityraw_display_lbl.Caption:='Error communication '+get_answer_code(answer);


end;


//------------------------------------------------------------------------------
procedure TKeller_Demo_Form.meas_rate_TrackBarChange(Sender: TObject);
//------------------------------------------------------------------------------
begin
 Timer1.Interval:= meas_rate_TrackBar.Position*100;
end;

end.
