{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit UExecFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, Buttons, ExtCtrls, ComCtrls, Tabs, StdCtrls, CheckLst, ImgList,
  Math;

type
  TExecFrame = class(TFrame)
    Shape8: TShape;
    Shape9: TShape;
    Panel7: TPanel;
    HelpImage: TImage;
    Panel1: TPanel;
    GeneSpeedButton: TSpeedButton;
    CompSpeedButton: TSpeedButton;
    TestSpeedButton: TSpeedButton;
    ExecSpeedButton: TSpeedButton;
    ListeSTCB: TCheckListBox;
    CalibMonSpeedButton: TSpeedButton;
    Shape1: TShape;
    Panel2: TPanel;
    Splitter1: TSplitter;
    GroupBox1: TGroupBox;
    InfosRE: TRichEdit;
    GroupBox2: TGroupBox;
    BinsSourcesLV: TListView;
    ImageList1: TImageList;
    SpeedButton1: TSpeedButton;
    procedure HelpImageClick(Sender: TObject);
    procedure CalibMonSpeedButtonClick(Sender: TObject);
    procedure GeneSpeedButtonClick(Sender: TObject);
    procedure CompSpeedButtonClick(Sender: TObject);
    procedure TestSpeedButtonClick(Sender: TObject);
    procedure BinsSourcesLVDblClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure BinsSourcesLVKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ExecSpeedButtonClick(Sender: TObject);
  private
    { Déclarations privées }
    procedure GeneCode(numST : integer);
    function Compile(numST : integer):boolean;
  public
    { Déclarations publiques }
    procedure MAJIHM;
    procedure MAJBinsSourcesLV;
  end;

implementation

uses UDocometre, XMLClasses, UOptionsConf, UCalibMonitor, UGlobal,
  URunSujet;

{$R *.dfm}

procedure TExecFrame.HelpImageClick(Sender: TObject);
begin
  DocoForm.ShowHelp((Sender as TImage).Tag);
end;

procedure TExecFrame.MAJBinsSourcesLV;
var sr : TSearchRec;
    NewListItem : TlistItem;
    S : string;
begin
  BinsSourcesLV.Items.Clear;
  BinsSourcesLV.ViewStyle := vsIcon;
  S := rep_manip + 'BinSources';

  if DirectoryExists(S) then
  begin
    if SysUtils.FindFirst(S + '\*.bas', faAnyFile, Sr) = 0 then
    begin
      repeat
        if UpperCase(Sr.Name) <> 'ADBASIC.BAS' then
        begin
          NewListItem := BinsSourcesLV.Items.Add;
          NewListItem.Caption := Sr.Name;
          NewListItem.ImageIndex := 0;
          NewListItem.StateIndex := -1;
          S := ExtractFileName(Sr.Name);
          delete(S,length(S) - 2,3);
          S := rep_manip + 'BinSources\' + S + 't' + cpuID + inttostr(num_Process);
          NewListItem := BinsSourcesLV.Items.Add;
          NewListItem.ImageIndex := 1;
          NewListItem.StateIndex := -1;
          if FileExists(S)
          then NewListItem.Caption := ExtractFileName(S)
          else NewListItem.Caption := '?';
        end;
      until SysUtils.FindNext(Sr) <> 0;
      SysUtils.FindClose(Sr);
    end;
  end;   
end;

procedure TExecFrame.MAJIHM;
var i : integer;
begin
  ListeSTCB.Clear;
  for i := 0 to DocDOCOMETRE.ST.Count - 1
  do ListeSTCB.Items.Add(DocDOCOMETRE.ST[i].NOM);
  MAJBinsSourcesLV;
end;

procedure TExecFrame.SpeedButton1Click(Sender: TObject);
var i : integer;
begin
  for i := 0 to ListeSTCB.Count - 1
  do ListeSTCB.Checked[i] := not ListeSTCB.Checked[i];
end;

procedure TExecFrame.CalibMonSpeedButtonClick(Sender: TObject);
begin
  DocoForm.RefreshIHMTimer.Enabled := false;
{
  if Level_Appli = 'Idle'
	then SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
  if Level_Appli = 'Normal'
	then SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  if Level_Appli = 'High'
	then SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
  if Level_Appli = 'Realtime'
	then SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
                                                       }
  CalibMonitorForm := TCalibMonitorForm.Create(self);
  CalibMonitorForm.ShowModal;
  DocoForm.RefreshIHMTimer.Enabled := true;

  SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);

end;

procedure TExecFrame.GeneCode(numST : integer);
var CurentXMLST : IXMLSTType;
    Code_INCLUDE : TStringList;
    Code_IMPORT  : TStringList;
    Code_VAR     : TStringList;
    Code_LOWINIT : TStringList;
    Code_CODE    : TStringList;
    Code_FINISH  : TStringList;
    Code_FCTSSP  : TStringList;
    Code_LIBRARY : TStringList;
    Code_Generation : TStringList;
    Code         : TStringList;
    InfoCanaux   : TInfoCanaux;
    i,n,N1,N2,num,B1,B2,B3,B4 : integer;
    S : string;
    AEAN : IXMLEANType;
    ASAN : IXMLSANType;
    AENUM : IXMLENUMType;
    ASNUM : IXMLSNUMType;
    ACANINT : IXMLCANINTType;


    procedure TraiterPSerie(APSerie : IXMLPserieType);
    var BAUDRATE,PARITE,NBBITDATA,NBBITSTOP,FLUX,NUMERO,RF,NOM,SParam : string;
        i_local : integer;
    begin
      if SystemType = 'ADWINPRO'
      then Code_INCLUDE.Add('#INCLUDE ADWPEXT.INC');
      if SystemType = 'ADWINGOLD'
      then Code_INCLUDE.Add('#INCLUDE ADWGCAN.INC');

      if APSerie.SYSTEME = 'ICE' then
      begin
        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards'))  then
        begin
          if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\ConvIEEE754SendICE.INC') = -1 then
          begin
            Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\ConvIEEE754SendICE.INC');
            Code_LIBRARY.Add('');
          end;
          if Code_LOWINIT.IndexOf('rs_reset(1)') = -1
          then Code_LOWINIT.Add('rs_reset(1)');
        end;

        if SystemType = 'ADWINGOLD' then
        begin
          if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\ConvIEEE754SendICEGold.INC') = -1 then
          begin
            Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\ConvIEEE754SendICEGold.INC');
            Code_LIBRARY.Add('');
          end;
          if Code_LOWINIT.IndexOf('rs_reset()') = -1
          then Code_LOWINIT.Add('rs_reset()');
        end;

        RF := floattostr(round(DocDOCOMETRE.EXPERIENCE.FEG /APSerie.FREQUENCE));
        NUMERO := floattostr(APSerie.NUMERO);
        BAUDRATE := floattostr(APSerie.BAUDRATE);
        if APSerie.PARITE = 'Sans'
        then PARITE := '0';
        if APSerie.PARITE = 'Paire'
        then PARITE := '1';
        if APSerie.PARITE = 'Impaire'
        then PARITE := '2';
        NBBITDATA := floattostr(APSerie.NBBITDATA);
        if APSerie.NBBITSTOP = 1
        then NBBITSTOP := '0';
        if APSerie.NBBITSTOP = 1.5
        then NBBITSTOP := '1';
        if APSerie.NBBITSTOP = 2
        then NBBITSTOP := '2';
        if APSerie.FLUX = 'no handshake (DTR/DSR)'
        then FLUX := '0';
        if APSerie.FLUX = 'Hardware handshake (RTS/CTS)'
        then FLUX := '1';
        if APSerie.FLUX = 'software handshake (Xon/Xoff)'
        then FLUX := '2';
        if APSerie.FLUX = 'RS485'
        then FLUX := '3';

        Code_LOWINIT.Add('''Init port serie ICE');
        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin
          Code_LOWINIT.Add('rs_init(1,' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');
        end;

        if SystemType = 'ADWINGOLD' then
        begin
          Code_LOWINIT.Add('rs_init(' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');
        end;

      end;

      if APSerie.SYSTEME = 'SummaGrid IV' then
      begin
        if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\SummaGridIV.INC') = -1 then
        begin
          Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\SummaGridIV.INC');
          Code_LIBRARY.Add('');
        end;

        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin
          if Code_LOWINIT.IndexOf('rs_reset(1)') = -1
          then Code_LOWINIT.Add('rs_reset(1)');
        end;

        if SystemType = 'ADWINGOLD' then
        begin
          if Code_LOWINIT.IndexOf('rs_reset()') = -1
          then Code_LOWINIT.Add('rs_reset()');
        end;

        RF := floattostr(round(DocDOCOMETRE.EXPERIENCE.FEG /APSerie.FREQUENCE));
        NUMERO := floattostr(APSerie.NUMERO);
        BAUDRATE := floattostr(APSerie.BAUDRATE);
        NOM := APSerie.NOM;
        if APSerie.PARITE = 'Sans'
        then PARITE := '0';
        if APSerie.PARITE = 'Paire'
        then PARITE := '1';
        if APSerie.PARITE = 'Impaire'
        then PARITE := '2';
        NBBITDATA := floattostr(APSerie.NBBITDATA);
        if APSerie.NBBITSTOP = 1
        then NBBITSTOP := '0';
        if APSerie.NBBITSTOP = 1.5
        then NBBITSTOP := '1';
        if APSerie.NBBITSTOP = 2
        then NBBITSTOP := '2';
        if APSerie.FLUX = 'no handshake (DTR/DSR)'
        then FLUX := '0';
        if APSerie.FLUX = 'Hardware handshake (RTS/CTS)'
        then FLUX := '1';
        if APSerie.FLUX = 'software handshake (Xon/Xoff)'
        then FLUX := '2';
        if APSerie.FLUX = 'RS485'
        then FLUX := '3';

        Code_LOWINIT.Add('''Init port serie SUMA');
				Code_LOWINIT.Add('rs_init(1,' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');


        Code_VAR.Add('DIM SUMMA_SWITCHES_' + NUMERO + ' AS LONG');
        Code_VAR.Add('DIM error AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_X AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_Y AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_BOUTON1 AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_BOUTON2 AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_BOUTON3 AS FLOAT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_X AS LONG');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_Y AS LONG');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_BOUTON1 AS LONG');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_BOUTON2 AS LONG');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_BOUTON3 AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_TEMPS AS FLOAT');
        Code_VAR.Add('DIM acquisition_SUMMA_'+ NUMERO + ' AS FLOAT');


        Code_LOWINIT.Add('''Initialisation de la Tablette');
        Code_LOWINIT.Add('''Mode binaire : <ESC>MB');
        Code_LOWINIT.Add('error=write_fifo(1,'+ NUMERO + ',27) ''ESC');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',77) ''M');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',66) ''B');
        Code_LOWINIT.Add('''Résolution : <ESC>Cn');
        Code_LOWINIT.Add('''       n       |     resolution');
        Code_LOWINIT.Add('''-------------------------------- ');
        Code_LOWINIT.Add(''' 1 (ASCII 49)  |   100 lignes/cm **');
        Code_LOWINIT.Add(''' 3 (ASCII 50)  |   400 lignes/cm');
        Code_LOWINIT.Add(''' B (ASCII 66)  |   800 lignes/cm');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',27)   ''ESC');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',67)   ''C');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',48+1) ''n');
        Code_LOWINIT.Add('''Relocate origin : <ESC>Fn');
        Code_LOWINIT.Add('''       n       |     origine');
        Code_LOWINIT.Add('''-------------------------------- ');
        Code_LOWINIT.Add(''' 0 (ASCII 48)  |   bas/gauche**');
        Code_LOWINIT.Add(''' 1 (ASCII 49)  |   à definir');
        Code_LOWINIT.Add(''' 2 (ASCII 50)  |   centre');
        Code_LOWINIT.Add(''' 3 (ASCII 51)  |   haut/gauche');
        Code_LOWINIT.Add(''' 4 (ASCII 52)  |   bas/droite');
        Code_LOWINIT.Add(''' 5 (ASCII 53)  |   haut/droite');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',27)   ''ESC');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',70)   ''F');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',48+0) ''n');
        Code_LOWINIT.Add('''Always transmit : <ESC>W1');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',27) ''ESC');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',87) ''W');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',49) ''1');
        Code_LOWINIT.Add('''Report mode : <ESC>M3');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',27) ''ESC');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',77) ''M');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',51) ''3');
        Code_LOWINIT.Add('''Vide la FIFO');
        Code_LOWINIT.Add('do');
        Code_LOWINIT.Add('  error=read_fifo(1,'+ NUMERO + ')');
        Code_LOWINIT.Add('until (error = -1)');
        Code_LOWINIT.Add(NOM + '_X =0');
        Code_LOWINIT.Add(NOM + '_X =0');
        Code_LOWINIT.Add(NOM + '_BOUTON1 =0');
        Code_LOWINIT.Add(NOM + '_BOUTON2 =0');
        Code_LOWINIT.Add(NOM + '_BOUTON3 =0');
        Code_LOWINIT.Add(NOM + '_TEMPS =0');
        Code_LOWINIT.Add('acquisition_SUMMA_' + NUMERO + ' = 1');

        Code_CODE.Add('Nouveau_' + NOM + '_X = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_Y = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_BOUTON1 = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_BOUTON2 = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_BOUTON3 = 0');
        Code_CODE.Add('IF (index=acquisition_SUMMA_'+ NUMERO + ') THEN');
        Code_CODE.Add('	IF (Call_ReadSUMMA(' + NOM + '_X,' + NOM + '_Y,SUMMA_SWITCHES_' + NUMERO + ',' + NUMERO + ') = 255) then');
        Code_CODE.Add('		' + NOM + '_BOUTON1 = 0');
        Code_CODE.Add('		' + NOM + '_BOUTON2 = 0');
        Code_CODE.Add('		' + NOM + '_BOUTON3 = 0');
        Code_CODE.Add('		if (SUMMA_SWITCHES_'+NUMERO+' = 1) then ' + NOM + '_BOUTON1 = 1');
        Code_CODE.Add('		if (SUMMA_SWITCHES_'+NUMERO+' = 2) then ' + NOM + '_BOUTON2 = 1');
        Code_CODE.Add('		if (SUMMA_SWITCHES_'+NUMERO+' = 3) then ' + NOM + '_BOUTON3 = 1');
        Code_CODE.Add('		Nouveau_' + NOM + '_X = 1');
        Code_CODE.Add('  	Nouveau_' + NOM + '_Y = 1');
        Code_CODE.Add('  	Nouveau_' + NOM + '_BOUTON1 = 1');
        Code_CODE.Add('  	Nouveau_' + NOM + '_BOUTON2 = 1');
        Code_CODE.Add('  	Nouveau_' + NOM + '_BOUTON3 = 1');
        Code_CODE.Add('	ENDIF');
        Code_CODE.Add('	acquisition_SUMMA_'+NUMERO+' = acquisition_SUMMA_'+NUMERO+' + ' + RF);
        Code_CODE.Add('ENDIF');
        Code_CODE.Add(NOM + '_TEMPS = temps + (READ_TIMER() - IN_TIMER)*25*10^(-9)');
      end;

      if APSerie.SYSTEME = 'Polhemus' then
      begin

        //if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\Polhemus.INC') = -1 then
        //begin
        //  Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\Polhemus.INC');
        //  Code_LIBRARY.Add('');
        //end;
        

        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin
          if Code_LOWINIT.IndexOf('rs_reset(1)') = -1
          then Code_LOWINIT.Add('rs_reset(1)');
        end;

        if SystemType = 'ADWINGOLD' then
        begin
          if Code_LOWINIT.IndexOf('rs_reset()') = -1
          then Code_LOWINIT.Add('rs_reset()');
        end;

        RF := floattostr(round(DocDOCOMETRE.EXPERIENCE.FEG /APSerie.FREQUENCE));
        NUMERO := floattostr(APSerie.NUMERO);
        BAUDRATE := floattostr(APSerie.BAUDRATE);
        NOM := APSerie.NOM;
        if APSerie.PARITE = 'Sans'
        then PARITE := '0';
        if APSerie.PARITE = 'Paire'
        then PARITE := '1';
        if APSerie.PARITE = 'Impaire'
        then PARITE := '2';
        NBBITDATA := floattostr(APSerie.NBBITDATA);
        if APSerie.NBBITSTOP = 1
        then NBBITSTOP := '0';
        if APSerie.NBBITSTOP = 1.5
        then NBBITSTOP := '1';
        if APSerie.NBBITSTOP = 2
        then NBBITSTOP := '2';
        if APSerie.FLUX = 'no handshake (DTR/DSR)'
        then FLUX := '0';
        if APSerie.FLUX = 'Hardware handshake (RTS/CTS)'
        then FLUX := '1';
        if APSerie.FLUX = 'software handshake (Xon/Xoff)'
        then FLUX := '2';
        if APSerie.FLUX = 'RS485'
        then FLUX := '3';

        Code_LOWINIT.Add('''Init port serie POLHEMUS');
				Code_LOWINIT.Add('rs_init(1,' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');



        Code_VAR.Add('REM *********************************************');
        Code_VAR.Add('REM ******** DEBUT déclaration variables Polhemus');
        Code_VAR.Add('DIM Tampon_ps_' + NUMERO + '[109] as SHORT');
        Code_VAR.Add('DIM fin_trame_' + NUMERO + ' as SHORT');
        Code_VAR.Add('DIM indice_ps_' + NUMERO + ' as SHORT');
        Code_VAR.Add('DIM coordonnees_fifo[' + inttostr(6*APSerie.NBCAPTEURS) + '] as FLOAT');
        Code_VAR.Add('DIM error as SHORT');
        Code_VAR.Add('DIM i AS INTEGER');
        Code_VAR.Add('DIM ' + NOM + '_TEMPS AS FLOAT');


        if APSerie.NBCAPTEURS >=1 then
        begin
          Code_VAR.Add('DIM ' + NOM + '_C1_X AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C1_Y AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C1_Z AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C1_THETAX AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C1_THETAY AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C1_THETAZ AS FLOAT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C1_X AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C1_Y AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C1_Z AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C1_THETAX AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C1_THETAY AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C1_THETAZ AS SHORT');
        end;
        if APSerie.NBCAPTEURS >= 2 then
        begin
          Code_VAR.Add('DIM ' + NOM + '_C2_X AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C2_Y AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C2_Z AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C2_THETAX AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C2_THETAY AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C2_THETAZ AS FLOAT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C2_X AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C2_Y AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C2_Z AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C2_THETAX AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C2_THETAY AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C2_THETAZ AS SHORT');
        end;
        if APSerie.NBCAPTEURS >= 3 then
        begin
          Code_VAR.Add('DIM ' + NOM + '_C3_X AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C3_Y AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C3_Z AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C3_THETAX AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C3_THETAY AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C3_THETAZ AS FLOAT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C3_X AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C3_Y AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C3_Z AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C3_THETAX AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C3_THETAY AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C3_THETAZ AS SHORT');
        end;
        if APSerie.NBCAPTEURS >= 4 then
        begin
          Code_VAR.Add('DIM ' + NOM + '_C4_X AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C4_Y AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C4_Z AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C4_THETAX AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C4_THETAY AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C4_THETAZ AS FLOAT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C4_X AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C4_Y AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C4_Z AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C4_THETAX AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C4_THETAY AS SHORT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C4_THETAZ AS SHORT');
        end;
        Code_VAR.Add('DIM ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + ' AS SHORT');
        Code_VAR.Add('REM ******** FIN déclaration variables Polhemus');
        Code_VAR.Add('REM *******************************************');

        Code_LOWINIT.Add('indice_ps_' + NUMERO + ' = 1');
        Code_LOWINIT.Add('fin_trame_' + NUMERO + ' = 0');
        Code_LOWINIT.Add('''Envoie au polhemus des commandes :');
        Code_LOWINIT.Add('''O1,2,4');
        Code_LOWINIT.Add('''O2,2,4');
        Code_LOWINIT.Add('''O3,2,4');
        Code_LOWINIT.Add('''O4,2,4,1');
        Code_LOWINIT.Add('error=0');
        Code_LOWINIT.Add('FOR i=1 TO ' + inttostr(APSerie.NBCAPTEURS));
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',79)	''O	: commande de config.');
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',48+i)''Oi	: capteur n°i');
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',44)	''Oi,');
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',50)	''Oi,2	: positions x,y,z');
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',44)	''Oi,2,');
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',52)	''Oi,2,4	: angles selon z,y,x');
        Code_LOWINIT.Add('	 IF (i=' + inttostr(APSerie.NBCAPTEURS)+') THEN');
        Code_LOWINIT.Add('       error=write_fifo(1,' + NUMERO + ',44)	''On,2,4,');
        Code_LOWINIT.Add('       error=write_fifo(1,' + NUMERO + ',49)	''On,2,4,1 : CR+LF');
        Code_LOWINIT.Add('    ENDIF');
        Code_LOWINIT.Add('    error=write_fifo(1,' + NUMERO + ',13)	''On,2,4(,1)<>');
        Code_LOWINIT.Add('NEXT i');
        Code_LOWINIT.Add('''Envoie au polhemus des commandes :');
        Code_LOWINIT.Add('''u');
        Code_LOWINIT.Add('''f');
        Code_LOWINIT.Add('''C');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',117)  ''u        : unité métrique');
        Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',102)  ''f        : binaire');
        if APSerie.MODE = 'Continu (120,60,40,30 Hz)'
        then Code_LOWINIT.Add('error=write_fifo(1,' + NUMERO + ',67) ''C        : mode continu');
        Code_LOWINIT.Add('IF (error<>0) THEN');
        Code_LOWINIT.Add('   error=1');
        Code_LOWINIT.Add('   exit');
        Code_LOWINIT.Add('ENDIF');
//        'sleep(149500)
        if APSerie.NBCAPTEURS >=1 then
        begin
          Code_LOWINIT.Add(NOM + '_C1_X = 0');
          Code_LOWINIT.Add(NOM + '_C1_Y = 0');
          Code_LOWINIT.Add(NOM + '_C1_Z = 0');
          Code_LOWINIT.Add(NOM + '_C1_THETAX = 0');
          Code_LOWINIT.Add(NOM + '_C1_THETAY = 0');
          Code_LOWINIT.Add(NOM + '_C1_THETAZ = 0');
        end;
        if APSerie.NBCAPTEURS >=2 then
        begin
          Code_LOWINIT.Add(NOM + '_C2_X = 0');
          Code_LOWINIT.Add(NOM + '_C2_Y = 0');
          Code_LOWINIT.Add(NOM + '_C2_Z = 0');
          Code_LOWINIT.Add(NOM + '_C2_THETAX = 0');
          Code_LOWINIT.Add(NOM + '_C2_THETAY = 0');
          Code_LOWINIT.Add(NOM + '_C2_THETAZ = 0');
        end;
        if APSerie.NBCAPTEURS >=3 then
        begin
          Code_LOWINIT.Add(NOM + '_C3_X = 0');
          Code_LOWINIT.Add(NOM + '_C3_Y = 0');
          Code_LOWINIT.Add(NOM + '_C3_Z = 0');
          Code_LOWINIT.Add(NOM + '_C3_THETAX = 0');
          Code_LOWINIT.Add(NOM + '_C3_THETAY = 0');
          Code_LOWINIT.Add(NOM + '_C3_THETAZ = 0');
        end;
        if APSerie.NBCAPTEURS >=4 then
        begin
          Code_LOWINIT.Add(NOM + '_C4_X = 0');
          Code_LOWINIT.Add(NOM + '_C4_Y = 0');
          Code_LOWINIT.Add(NOM + '_C4_Z = 0');
          Code_LOWINIT.Add(NOM + '_C4_THETAX = 0');
          Code_LOWINIT.Add(NOM + '_C4_THETAY = 0');
          Code_LOWINIT.Add(NOM + '_C4_THETAZ = 0');
        end;
        Code_LOWINIT.Add('ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + '=1');

        Code_CODE.Add('REM *****************************');
        Code_CODE.Add('REM ***DEBUT acquisition polhemus');

        Code_CODE.Add('IF (INDEX=ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + ') THEN');

        if APSerie.MODE <> 'Continu (120,60,40,30 Hz)'
        then Code_CODE.Add('        error=write_fifo(1,' + NUMERO + ',80)''P : mode à la demande');


        Code_CODE.Add('     Call_lecture_fifo_' + NUMERO + '()');


        if APSerie.NBCAPTEURS >=1 then
        begin
          Code_CODE.Add('     Nouveau_' + NOM + '_C1_X = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C1_Y = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C1_Z = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C1_THETAX = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C1_THETAY = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C1_THETAZ = 0');
        end;
        if APSerie.NBCAPTEURS >=2 then
        begin
          Code_CODE.Add('     Nouveau_' + NOM + '_C2_X = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C2_Y = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C2_Z = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C2_THETAX = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C2_THETAY = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C2_THETAZ = 0');
        end;
        if APSerie.NBCAPTEURS >=3 then
        begin
          Code_CODE.Add('     Nouveau_' + NOM + '_C3_X = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C3_Y = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C3_Z = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C3_THETAX = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C3_THETAY = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C3_THETAZ = 0');
        end;
        if APSerie.NBCAPTEURS >=4 then
        begin
          Code_CODE.Add('     Nouveau_' + NOM + '_C4_X = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C4_Y = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C4_Z = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C4_THETAX = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C4_THETAY = 0');
          Code_CODE.Add('     Nouveau_' + NOM + '_C4_THETAZ = 0');
        end;


        Code_CODE.Add('     IF (fin_trame_' + NUMERO + '=1) THEN');
        Code_CODE.Add('        Call_Calcul_x_y_z_ro_teta_phi_portserie_' + NUMERO + '()');

        if APSerie.NBCAPTEURS >=1 then
        begin
          Code_CODE.Add('        ' + NOM + '_C1_X = coordonnees_fifo[1]');
          Code_CODE.Add('        ' + NOM + '_C1_Y = coordonnees_fifo[2]');
          Code_CODE.Add('        ' + NOM + '_C1_Z = coordonnees_fifo[3]');
          Code_CODE.Add('        ' + NOM + '_C1_THETAX = coordonnees_fifo[6]');
          Code_CODE.Add('        ' + NOM + '_C1_THETAY = coordonnees_fifo[5]');
          Code_CODE.Add('        ' + NOM + '_C1_THETAZ = coordonnees_fifo[4]');
          Code_CODE.Add('        Nouveau_' + NOM + '_C1_X = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C1_Y = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C1_Z = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C1_THETAX = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C1_THETAY = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C1_THETAZ = 1');
        end;
        if APSerie.NBCAPTEURS >=2 then
        begin
          Code_CODE.Add('        ' + NOM + '_C2_X = coordonnees_fifo[7]');
          Code_CODE.Add('        ' + NOM + '_C2_Y = coordonnees_fifo[8]');
          Code_CODE.Add('        ' + NOM + '_C2_Z = coordonnees_fifo[9]');
          Code_CODE.Add('        ' + NOM + '_C2_THETAX = coordonnees_fifo[12]');
          Code_CODE.Add('        ' + NOM + '_C2_THETAY = coordonnees_fifo[11]');
          Code_CODE.Add('        ' + NOM + '_C2_THETAZ = coordonnees_fifo[10]');
          Code_CODE.Add('        Nouveau_' + NOM + '_C2_X = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C2_Y = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C2_Z = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C2_THETAX = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C2_THETAY = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C2_THETAZ = 1');
        end;
        if APSerie.NBCAPTEURS >=3 then
        begin
          Code_CODE.Add('        ' + NOM + '_C3_X = coordonnees_fifo[13]');
          Code_CODE.Add('        ' + NOM + '_C3_Y = coordonnees_fifo[14]');
          Code_CODE.Add('        ' + NOM + '_C3_Z = coordonnees_fifo[15]');
          Code_CODE.Add('        ' + NOM + '_C3_THETAX = coordonnees_fifo[18]');
          Code_CODE.Add('        ' + NOM + '_C3_THETAY = coordonnees_fifo[17]');
          Code_CODE.Add('        ' + NOM + '_C3_THETAZ = coordonnees_fifo[16]');
          Code_CODE.Add('        Nouveau_' + NOM + '_C3_X = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C3_Y = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C3_Z = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C3_THETAX = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C3_THETAY = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C3_THETAZ = 1');
        end;
        if APSerie.NBCAPTEURS >=4 then
        begin
          Code_CODE.Add('        ' + NOM + '_C4_X = coordonnees_fifo[19]');
          Code_CODE.Add('        ' + NOM + '_C4_Y = coordonnees_fifo[20]');
          Code_CODE.Add('        ' + NOM + '_C4_Z = coordonnees_fifo[21]');
          Code_CODE.Add('        ' + NOM + '_C4_THETAX = coordonnees_fifo[24]');
          Code_CODE.Add('        ' + NOM + '_C4_THETAY = coordonnees_fifo[23]');
          Code_CODE.Add('        ' + NOM + '_C4_THETAZ = coordonnees_fifo[22]');
          Code_CODE.Add('        Nouveau_' + NOM + '_C4_X = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C4_Y = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C4_Z = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C4_THETAX = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C4_THETAY = 1');
          Code_CODE.Add('        Nouveau_' + NOM + '_C4_THETAZ = 1');
        end;

        Code_CODE.Add('        ''' + NOM + '_TEMPS = temps + (READ_TIMER() - IN_TIMER)*25*10^(-9)');
        Code_CODE.Add('     ENDIF');
        Code_CODE.Add('     ' + NOM+ '_TEMPS = temps + (READ_TIMER() - IN_TIMER)*25*10^(-9)');

        if APSerie.MODE = 'Continu (120,60,40,30 Hz)'
        then Code_CODE.Add('     ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + '=ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + ' + 1''Mode continue')
        ELSE Code_CODE.Add('     ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + '=ACQUISITION_POLHEMUS_PORTSERIE_' + NUMERO + ' + XX_PARAM9_XX''Mode à la demande');
        Code_CODE.Add('ENDIF');

        Code_CODE.Add('REM ***FIN acquisition polhemus');
        Code_CODE.Add('REM ***************************');

        if APSerie.MODE = 'Continu (120,60,40,30 Hz)'
        then Code_FINISH.Add('error=write_fifo(1,' + NUMERO + ',99)  ''c : fin mode continu');

        Code_FCTSSP.Add('REM **************************************');
        Code_FCTSSP.Add('REM ******** DEBUT Sous prog pour polhemus');
        Code_FCTSSP.Add('SUB Call_Lecture_fifo_' + NUMERO + '()');
        Code_FCTSSP.Add('');
        Code_FCTSSP.Add('DIM compteur_de_boucle as INTEGER');
        Code_FCTSSP.Add('DIM octet_courant as SHORT');
        Code_FCTSSP.Add('DIM etat_fifo as SHORT');
        Code_FCTSSP.Add('');
        Code_FCTSSP.Add('''DIM register_ps as INTEGER');
        Code_FCTSSP.Add('''register_ps=(XX_PARAM1_XX-1)*8+5');
        Code_FCTSSP.Add('''etat_fifo=get_rs(XX_PARAM1_XX,register_ps) and 1');
        Code_FCTSSP.Add('''IF (etat_fifo<>0) THEN');
        Code_FCTSSP.Add('');
        Code_FCTSSP.Add('   FOR compteur_de_boucle=1 to 64');
        Code_FCTSSP.Add('       octet_courant=read_fifo(1,'+ NUMERO + ')');
        Code_FCTSSP.Add('		    IF (octet_courant<>-1) THEN');
        Code_FCTSSP.Add('          Tampon_ps_' + NUMERO + '[indice_ps_' + NUMERO + ']=octet_courant');
        Code_FCTSSP.Add('          inc(indice_ps_' + NUMERO + ')');
        Code_FCTSSP.Add('          IF ((Tampon_ps_' + NUMERO + '[indice_ps_' + NUMERO + '-1]=10) and (Tampon_ps_' + NUMERO + '[indice_ps_ ' + NUMERO + '-2]=13)) then');
        Code_FCTSSP.Add('             fin_trame_' + NUMERO + '=1');
        Code_FCTSSP.Add('    	  	   compteur_de_boucle=64');
        Code_FCTSSP.Add('   		    ENDIF');
        Code_FCTSSP.Add('        ELSE');
        if APSerie.MODE = 'Continu (120,60,40,30 Hz)'
        then Code_FCTSSP.Add('   		    compteur_de_boucle=64')
        else Code_FCTSSP.Add('   		    compteur_de_boucle=1');
        Code_FCTSSP.Add('        ENDIF');
        Code_FCTSSP.Add('   NEXT compteur_de_boucle');
        Code_FCTSSP.Add('''ENDIF');
        Code_FCTSSP.Add('ENDSUB');
        Code_FCTSSP.Add('SUB Call_Calcul_x_y_z_ro_teta_phi_portserie_' + NUMERO + '()');
        Code_FCTSSP.Add('DIM compteur_de_boucle_1,compteur_de_boucle_2 as INTEGER');
        Code_FCTSSP.Add('DIM frac_calcul as INTEGER');
        Code_FCTSSP.Add('DIM Dummy_calcul as INTEGER');
        Code_FCTSSP.Add('DIM exposant_calcul,by0,by1,by2,by3,signe_calcul as INTEGER');
        Code_FCTSSP.Add('DIM num_capteur AS SHORT');
        Code_FCTSSP.Add('    fin_trame_'+ NUMERO +'=0');
        Code_FCTSSP.Add('    indice_ps_'+ NUMERO +'=1');
        Code_FCTSSP.Add('    IF (Tampon_ps_'+ NUMERO +'[1]=48) THEN');
        Code_FCTSSP.Add('       FOR compteur_de_boucle_1=1 TO ' +  inttostr(APSerie.NBCAPTEURS));
        Code_FCTSSP.Add('           num_capteur = Tampon_ps_'+ NUMERO +'[2+27*(compteur_de_boucle_1-1)] - 1 - 48');
        Code_FCTSSP.Add('           FOR compteur_de_boucle_2=0 to 5');
        Code_FCTSSP.Add('               by0=Tampon_ps_'+ NUMERO +'[27*num_capteur+4+4*compteur_de_boucle_2]');
        Code_FCTSSP.Add('               by1=Tampon_ps_'+ NUMERO +'[27*num_capteur+5+4*compteur_de_boucle_2]');
        Code_FCTSSP.Add('               by2=Tampon_ps_'+ NUMERO +'[27*num_capteur+6+4*compteur_de_boucle_2]');
        Code_FCTSSP.Add('               by3=Tampon_ps_'+ NUMERO +'[27*num_capteur+7+4*compteur_de_boucle_2]');
        Code_FCTSSP.Add('               frac_calcul=by0+256*by1+ (by2 and 127)*65536');
        Code_FCTSSP.Add('               exposant_calcul=(by2 and 128)/128 + (by3 and 127)*2');
        Code_FCTSSP.Add('               coordonnees_fifo[compteur_de_boucle_2+1+6*num_capteur]=2^(exposant_calcul-127)*(frac_calcul/2^23+1)');
        Code_FCTSSP.Add('               signe_calcul=(-1)^((by3 and 128)/128)');
        Code_FCTSSP.Add('               coordonnees_fifo[compteur_de_boucle_2+1+6*num_capteur]=coordonnees_fifo[compteur_de_boucle_2+1+6*num_capteur]*signe_calcul');
        Code_FCTSSP.Add('           NEXT compteur_de_boucle_2');
        Code_FCTSSP.Add('        NEXT i');
        Code_FCTSSP.Add('    ENDIF');
        Code_FCTSSP.Add('ENDSUB');
        Code_FCTSSP.Add('REM ******** FIN Sous prog pour polhemus');
        Code_FCTSSP.Add('REM ************************************');
        Code_FCTSSP.Add('');
      end;

      if APSerie.SYSTEME = 'Bird' then
      begin

        RF := floattostr(round(DocDOCOMETRE.EXPERIENCE.FEG /APSerie.FREQUENCE));
        NUMERO := floattostr(APSerie.NUMERO);
        BAUDRATE := floattostr(APSerie.BAUDRATE);
        NOM := APSerie.NOM;
        if APSerie.PARITE = 'Sans'
        then PARITE := '0';
        if APSerie.PARITE = 'Paire'
        then PARITE := '1';
        if APSerie.PARITE = 'Impaire'
        then PARITE := '2';
        NBBITDATA := floattostr(APSerie.NBBITDATA);
        if APSerie.NBBITSTOP = 1
        then NBBITSTOP := '0';
        if APSerie.NBBITSTOP = 1.5
        then NBBITSTOP := '1';
        if APSerie.NBBITSTOP = 2
        then NBBITSTOP := '2';
        if APSerie.FLUX = 'no handshake (DTR/DSR)'
        then FLUX := '0';
        if APSerie.FLUX = 'Hardware handshake (RTS/CTS)'
        then FLUX := '1';
        if APSerie.FLUX = 'software handshake (Xon/Xoff)'
        then FLUX := '2';
        if APSerie.FLUX = 'RS485'
        then FLUX := '3';
        
        Code_VAR.Add('REM *******************************************');
        Code_VAR.Add('REM ******** DEBUT déclaration variables Bird');
        Code_VAR.Add('DIM ' + NOM + '_X AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_Y AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_Z AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_THETAX AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_THETAY AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_THETAZ AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_TEMPS AS FLOAT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_X AS SHORT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_Y AS SHORT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_Z AS SHORT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_THETAX AS SHORT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_THETAY AS SHORT');
        Code_VAR.Add('DIM Nouveau_' + NOM + '_THETAZ AS SHORT');
        Code_VAR.Add('DIM ACQUISITION_FOB_PORTSERIE_' + NUMERO + ' AS SHORT');
        Code_VAR.Add('DIM error_FOB_' + NUMERO + ' AS INTEGER');

        Code_VAR.Add('REM ******** FIN déclaration variables Bird');
        Code_VAR.Add('REM *******************************************');

        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin

          if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\FOB.INC') = -1 then
          begin
            Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\FOB.INC');
            Code_LIBRARY.Add('');
          end;

          if Code_LOWINIT.IndexOf('rs_reset(1)') = -1
          then Code_LOWINIT.Add('rs_reset(1)');
        end;

        if SystemType = 'ADWINGOLD' then
        begin

          if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\FOBGold.INC') = -1 then
          begin
            Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\FOBGold.INC');
            Code_LIBRARY.Add('');
          end;

          if Code_LOWINIT.IndexOf('rs_reset()') = -1
          then Code_LOWINIT.Add('rs_reset()');

        end;

        Code_LOWINIT.Add('''Init port serie pour BIRD');
        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin
          Code_LOWINIT.Add('rs_init(1,' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');
        end;

        if SystemType = 'ADWINGOLD' then
        begin
          Code_LOWINIT.Add('rs_init(' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');
        end;

        Code_LOWINIT.add('''Initialisation du FOB');
        SParam := NOM + '_X,' + NOM + '_Y,' + NOM + '_Z,' + NOM + '_THETAX,' ;
        Sparam := Sparam + NOM + '_THETAY,' + NOM + '_THETAZ,' + NUMERO;
        Code_LOWINIT.add('InitFOB(' + SParam + ')');

        Code_LOWINIT.Add('ACQUISITION_FOB_PORTSERIE_' + NUMERO + '=1');


        Code_CODE.Add('REM *****************************');
        Code_CODE.Add('REM ***DEBUT acquisition Bird');
        Code_CODE.Add('Nouveau_' + NOM + '_X = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_Y = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_Z = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_THETAX = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_THETAY = 0');
        Code_CODE.Add('Nouveau_' + NOM + '_THETAZ = 0');
        Code_CODE.Add('IF (INDEX=ACQUISITION_FOB_PORTSERIE_' + NUMERO + ') THEN');
        Code_CODE.Add('  IF (ReadFOB(' + SParam + ') = 255) THEN');
        Code_CODE.Add('     Nouveau_' + NOM + '_X = 1');
        Code_CODE.Add('     Nouveau_' + NOM + '_Y = 1');
        Code_CODE.Add('     Nouveau_' + NOM + '_Z = 1');
        Code_CODE.Add('     Nouveau_' + NOM + '_THETAX = 1');
        Code_CODE.Add('     Nouveau_' + NOM + '_THETAY = 1');
        Code_CODE.Add('     Nouveau_' + NOM + '_THETAZ = 1');
        Code_CODE.Add('  ENDIF');
        Code_CODE.Add('  ACQUISITION_FOB_PORTSERIE_' + NUMERO + ' = ACQUISITION_FOB_PORTSERIE_' + NUMERO + ' + FEG/100');
        Code_CODE.Add('ENDIF');
        Code_CODE.Add(NOM + '_TEMPS = temps + (READ_TIMER() - IN_TIMER)*25*10^(-9)');
        Code_CODE.Add('REM ***************************');
        Code_CODE.Add('REM ***FIN acquisition Bird');


        Code_FINISH.Add('''Arrêt de la communication');
        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin
          Code_FINISH.Add('error_FOB_' + NUMERO + ' = write_fifo(1,' + NUMERO + ',63)');
        end;
        if SystemType = 'ADWINGOLD' then
        begin
          Code_FINISH.Add('error_FOB_' + NUMERO + ' = write_fifo(' + NUMERO + ',63)');
        end;
      end;

      if APSerie.SYSTEME = 'CODAmotion' then
      begin  {
        if Code_LIBRARY.IndexOf('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\ConvIEEE754SendICE.INC') = -1 then
        begin
          Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\ConvIEEE754SendICE.INC');
          Code_LIBRARY.Add('');
        end;               }

        if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II') or (SystemType = 'ADWINPRO II Old Cards')) then
        begin
          if Code_LOWINIT.IndexOf('rs_reset(1)') = -1
          then Code_LOWINIT.Add('rs_reset(1)');
        end;

        if SystemType = 'ADWINGOLD' then
        begin
          if Code_LOWINIT.IndexOf('rs_reset()') = -1
          then Code_LOWINIT.Add('rs_reset()');
        end;

        NOM := APSerie.NOM;
        RF := floattostr(round(DocDOCOMETRE.EXPERIENCE.FEG /APSerie.FREQUENCE));
        NUMERO := floattostr(APSerie.NUMERO);
        BAUDRATE := floattostr(APSerie.BAUDRATE);
        if APSerie.PARITE = 'Sans'
        then PARITE := '0';
        if APSerie.PARITE = 'Paire'
        then PARITE := '1';
        if APSerie.PARITE = 'Impaire'
        then PARITE := '2';
        NBBITDATA := floattostr(APSerie.NBBITDATA);
        if APSerie.NBBITSTOP = 1
        then NBBITSTOP := '0';
        if APSerie.NBBITSTOP = 1.5
        then NBBITSTOP := '1';
        if APSerie.NBBITSTOP = 2
        then NBBITSTOP := '2';
        if APSerie.FLUX = 'no handshake (DTR/DSR)'
        then FLUX := '0';
        if APSerie.FLUX = 'Hardware handshake (RTS/CTS)'
        then FLUX := '1';
        if APSerie.FLUX = 'software handshake (Xon/Xoff)'
        then FLUX := '2';
        if APSerie.FLUX = 'RS485'
        then FLUX := '3';

        Code_LOWINIT.Add('''Init port serie CODAMOTION');
				Code_LOWINIT.Add('rs_init(1,' + NUMERO + ',' +	BAUDRATE + ',' + PARITE + ',' +
																	NBBITDATA + ',' + NBBITSTOP + ',' + FLUX + ')');

        Code_LOWINIT.Add('''Vidage tampons serie CODAmotion et inits');
        Code_LOWINIT.Add('DO');
        Code_LOWINIT.Add('  ' + NOM + '_repSerial = read_fifo(1,' + NUMERO + ')');
        Code_LOWINIT.Add('UNTIL (' + NOM + '_repSerial = -1)');
        Code_LOWINIT.Add(NOM + '_frameStartFound = 0');
        Code_LOWINIT.Add(NOM + '_byteCounter = 1');
        Code_LOWINIT.Add(NOM + '_valuesCounter = 1');

        Code_VAR.Add('REM ***********************************************');
        Code_VAR.Add('REM ******** DEBUT déclaration variables CODAmotion');
        Code_VAR.Add('DIM ' + NOM + '_values[' + inttostr(APSerie.NBCAPTEURS*3+1) + '] AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_repSerial AS LONG');
        for i_local := 0 to APSerie.NBCAPTEURS - 1 do
        begin
          Code_VAR.Add('DIM ' + NOM + '_C' + inttostr(i_local + 1) + '_X AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C' + inttostr(i_local + 1) + '_Y AS FLOAT');
          Code_VAR.Add('DIM ' + NOM + '_C' + inttostr(i_local + 1) + '_Z AS FLOAT');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C' + inttostr(i_local + 1) + '_X AS LONG');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C' + inttostr(i_local + 1) + '_Y AS LONG');
          Code_VAR.Add('DIM Nouveau_' + NOM + '_C' + inttostr(i_local + 1) + '_Z AS LONG');
        end;
        Code_VAR.Add('DIM ' + NOM + '_FRAMEID AS FLOAT');
        Code_VAR.Add('DIM ' + NOM + '_frameStartFound AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_dataSerial[2] AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_byteCounter AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_valuesCounter AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_signBit AS LONG');
        Code_VAR.Add('DIM ' + NOM + '_frameSynchroCode[2] AS LONG');

        Code_VAR.Add('REM ******** FIN déclaration variables CODAmotion');
        Code_VAR.Add('REM *********************************************');

        Code_CODE.Add('REM *************************************');
        Code_CODE.Add('REM ***DEBUT acquisition frame CODAmotion');

        Code_CODE.Add('DO');
        Code_CODE.Add('  ' + NOM + '_repSerial = read_fifo(1,' + NUMERO + ')');
        Code_CODE.Add('  IF(' + NOM + '_repSerial > -1) THEN');
        Code_CODE.Add('    IF(' + NOM + '_frameStartFound = 1) THEN');
        Code_CODE.Add('      ' + NOM + '_dataSerial[' + NOM + '_byteCounter] = ' + NOM + '_repSerial');
        Code_CODE.Add('      INC(' + NOM + '_byteCounter)');
        Code_CODE.Add('      IF(' + NOM + '_byteCounter = 3) THEN');
        Code_CODE.Add('        ' + NOM + '_byteCounter = 1');
        Code_CODE.Add('        ' + NOM + '_signBit =  ' + NOM + '_dataSerial[2] AND 10000000b');
        Code_CODE.Add('        ' + NOM + '_dataSerial[2] = ' + NOM + '_dataSerial[2] AND 01111111b');
        Code_CODE.Add('        ' + NOM + '_values[' + NOM + '_valuesCounter] = shift_left(' + NOM + '_dataSerial[2],8) + ' + NOM + '_dataSerial[1]');
        Code_CODE.Add('        IF(' + NOM + '_signBit>0) THEN');
        Code_CODE.Add('          ' + NOM + '_values[' + NOM + '_valuesCounter] = ' + NOM + '_values[' + NOM + '_valuesCounter] - 32768');        
        Code_CODE.Add('        ENDIF');
        Code_CODE.Add('        INC(' + NOM + '_valuesCounter)');
        Code_CODE.Add('        IF(' + NOM + '_valuesCounter = ' + inttostr(3*APSerie.NBCAPTEURS + 2) + ') THEN');
        Code_CODE.Add('          ' + NOM + '_frameStartFound = 0');
        Code_CODE.Add('          ' + NOM + '_valuesCounter = 1');

        Code_CODE.Add('          ' + NOM + '_frameID = ' + NOM + '_values[1]');
        for i_local := 0 to APSerie.NBCAPTEURS - 1 do
        begin
          Code_CODE.Add('          ' + NOM + '_C'+ IntToStr(i_local + 1) + '_X = ' + NOM + '_values[' + inttostr( 3*i_local + 2) + ']/100.0');
          Code_CODE.Add('          ' + NOM + '_C'+ IntToStr(i_local + 1) + '_Y = ' + NOM + '_values[' + inttostr( 3*i_local + 2 + 1) + ']/100.0');
          Code_CODE.Add('          ' + NOM + '_C'+ IntToStr(i_local + 1) + '_Z = ' + NOM + '_values[' + inttostr( 3*i_local + 2 + 1 +1) + ']/100.0');
          Code_CODE.Add('          Nouveau_' + NOM + '_C'+ IntToStr(i_local + 1) + '_X = 1');
          Code_CODE.Add('          Nouveau_' + NOM + '_C'+ IntToStr(i_local + 1) + '_Y = 1');
          Code_CODE.Add('          Nouveau_' + NOM + '_C'+ IntToStr(i_local + 1) + '_Z = 1');
        end;
        Code_CODE.Add('        ENDIF');
        Code_CODE.Add('      ENDIF');
        Code_CODE.Add('    ELSE');
        Code_CODE.Add('      ' + NOM + '_frameSynchroCode[2] = ' + NOM + '_frameSynchroCode[1]');
        Code_CODE.Add('      ' + NOM + '_frameSynchroCode[1] = ' + NOM + '_repSerial');
        Code_CODE.Add('      IF(' + NOM + '_frameSynchroCode[1] = 80h) THEN');
        Code_CODE.Add('        IF(' + NOM + '_frameSynchroCode[2] = 00h) THEN');
        Code_CODE.Add('          ' + NOM + '_frameStartFound = 1');
        Code_CODE.Add('        ENDIF');
        Code_CODE.Add('      ENDIF');
        Code_CODE.Add('    ENDIF');
        Code_CODE.Add('  ENDIF');
        Code_CODE.Add('UNTIL (' + NOM + '_repSerial = -1)');    

        Code_CODE.Add('REM ***FIN acquisition frame CODAmotion');
        Code_CODE.Add('REM ***********************************');


      end;

    end;


    procedure GENECODE_BLOC(ABloc : IXMLBLOCType;Niveau : integer);
    var Local_i,Local_j : integer;
        AFonction : IXMLFONCTIONType;
        indent : string;
    begin
      indent := '';
      for Local_i := 0 to Niveau - 1
      do indent := indent + '   ';
      //Code condition du bloc
      if ABloc.GENRE = 'DEBUT'
      then Code_LOWINIT.Add(ABloc.ADBASICCODE1)
      else if ABloc.GENRE = 'FIN'
           then Code_FINISH.Insert(0,ABloc.ADBASICCODE1)
           else Code_CODE.Add(indent + ABloc.ADBASICCODE1);

      //Code des fonctions du bloc
      for Local_i := 0 to ABloc.FONCTION.Count - 1 do
      begin
        AFonction := ABloc.FONCTION[Local_i];

        InfosRE.Paragraph.Alignment := taLeftJustify;
        InfosRE.SelAttributes.Style := [];
        InfosRE.SelAttributes.Size := 8;
        InfosRE.SelAttributes.Color := clBlack;
        InfosRE.Lines.Add(indent + '        Traitement de la fonction : ' + AFonction.TITRE +
                               ' - ' + AFonction.GENRE + ' - ' + AFonction.FICHIER);

        //Si la fonction est "AnIn" =>
        //Utilisation en mode asynchrone
        //Traitement particulier pour recup calib num entree et gain
        if (AFonction.FICHIER = 'AnIn') or (AFonction.FICHIER = 'AnalogWait') then
        begin

          for Local_j := 0 to DocDOCOMETRE.EAN.Count - 1 do
          begin
            if DocDOCOMETRE.EAN[Local_j].NOM = AFonction.ENTREE[0].NOM then
            begin
              AEAN := DocDOCOMETRE.EAN[Local_j];
              break;
            end;
          end;

          if AFonction.FICHIER = 'AnIn'
          then S := AFonction.ENTREE[0].NOM + ' = Call_AnIn(';
          if AFonction.FICHIER = 'AnalogWait'
          then S := 'UNTIL (Call_AnIn(';
          S := S + inttostr(AEAN.NUMERO) + ',' + inttostr(AEAN.GAIN) + ',';
          S := S + floattostr(AEAN.AMIN) + ',' + floattostr(AEAN.AMAX) + ',';
          S := S + floattostr(AEAN.UMIN) + ',' + floattostr(AEAN.UMAX) + ')';
          if AFonction.FICHIER = 'AnalogWait'
          then S := S + AFonction.PARAM[0].NOM + AFonction.PARAM[1].NOM + ')';

          if AFonction.FICHIER = 'AnIn'
          then AFonction.ADBASIC.CODE[0].TEXT := S;
          if AFonction.FICHIER = 'AnalogWait'
          then AFonction.ADBASIC.CODE[2].TEXT := S

        end;

        //INCLUDE
        for Local_j := 0 to AFonction.ADBASIC.INCLUDE.Count - 1
        do Code_INCLUDE.Add(AFonction.ADBASIC.INCLUDE[Local_j].TEXT);
        //IMPORT
        for Local_j := 0 to AFonction.ADBASIC.IMPORT.Count - 1
        do Code_IMPORT.Add(AFonction.ADBASIC.IMPORT[Local_j].TEXT);
        //VAR
        for Local_j := 0 to AFonction.ADBASIC.VAR_.Count - 1 do
        begin
          if Code_VAR.IndexOf(AFonction.ADBASIC.VAR_[Local_j].TEXT) = -1
          then Code_VAR.Add(AFonction.ADBASIC.VAR_[Local_j].TEXT + ' ''Utilisé par ' + AFonction.FICHIER);
        end;
        if AFonction.ADBASIC.VAR_.Count > 0 then Code_VAR.Add('');
        //LOWINIT
        for Local_j := 0 to AFonction.ADBASIC.LOWINIT.Count - 1
        do Code_LOWINIT.Add(AFonction.ADBASIC.LOWINIT[Local_j].TEXT);
        if AFonction.ADBASIC.LOWINIT.Count > 0 then Code_LOWINIT.Add('');
        //CODE
        if (ABloc.GENRE <> 'DEBUT') and (ABloc.GENRE <> 'FIN') then
        begin
          if (ABloc.GENRE <> 'NORMAL') then
          begin
            for Local_j := 0 to AFonction.ADBASIC.CODE.Count - 1
            do Code_CODE.Add(indent+ '   ' + AFonction.ADBASIC.CODE[local_j].TEXT);
          end else
          begin
            for Local_j := 0 to AFonction.ADBASIC.CODE.Count - 1
            do Code_CODE.Add(indent + AFonction.ADBASIC.CODE[local_j].TEXT);
          end;
        end;
        if (ABloc.GENRE = 'DEBUT' ) then
        begin
          for Local_j := 0 to AFonction.ADBASIC.CODE.Count - 1
          do Code_LOWINIT.Add(AFonction.ADBASIC.CODE[Local_j].TEXT);
        end;
        if (ABloc.GENRE = 'FIN') then
        begin
          for Local_j := 0 to AFonction.ADBASIC.CODE.Count - 1
          do Code_FINISH.Add(AFonction.ADBASIC.CODE[Local_j].TEXT);
        end;
        //FINISH
        for Local_j := 0 to AFonction.ADBASIC.FINISH.Count - 1
        do Code_FINISH.Add(AFonction.ADBASIC.FINISH[Local_j].TEXT);
        if AFonction.ADBASIC.FINISH.Count > 0 then Code_FINISH.Add('');
        //FCTSSP
        for Local_j := 0 to AFonction.ADBASIC.FCTSSP.Count - 1
        do Code_FCTSSP.Add(AFonction.ADBASIC.FCTSSP[Local_j].TEXT);
        //LIBRARY
        for Local_j := 0 to AFonction.ADBASIC.LIBRARY_.Count - 1
        do Code_LIBRARY.Add(AFonction.ADBASIC.LIBRARY_[Local_j].TEXT);
      end;
      //Appel recursif des blocs fils
      for Local_i := 0 to ABloc.BLOC.Count - 1 do
      begin
        InfosRE.Paragraph.Alignment := taLeftJustify;
        InfosRE.SelAttributes.Style := [];
        InfosRE.SelAttributes.Size := 8;
        InfosRE.SelAttributes.Color := clBlack;
        InfosRE.Lines.Add(indent + '        Traitement du bloc : ' + inttostr(local_i));

        GENECODE_BLOC(ABloc.BLOC[Local_i],Niveau+1);
      end;
      //Cloture code condition bloc
      if (ABloc.GENRE <> 'DEBUT' ) and (ABloc.GENRE <> 'FIN')
      then Code_CODE.Add(indent + ABloc.ADBASICCODE2);
    end;

begin
  Code_INCLUDE := TStringList.Create;
  Code_INCLUDE.Sorted := true;//Sinon duplicates n'a pas d'effet
  Code_INCLUDE.CaseSensitive := false;
  Code_INCLUDE.Duplicates := dupIgnore;//pour eviter les declarations multiples

  Code_IMPORT  := TStringList.Create;

  //il faut gerer les doublons à la "main" car pas de tri possible
  Code_VAR     := TStringList.Create;
  Code_VAR.CaseSensitive := false;

  Code_LOWINIT := TStringList.Create;
  Code_CODE    := TStringList.Create;
  Code_FINISH  := TStringList.Create;
  Code_FCTSSP  := TStringList.Create;
  Code_LIBRARY := TStringList.Create;
  Code_Generation := TStringList.Create;
  Code         := TStringList.Create;

  InfosRE.Paragraph.Alignment := taLeftJustify;
  InfosRE.SelAttributes.Style := [fsBold,fsUnderline];
  InfosRE.SelAttributes.Size := 10;
  InfosRE.SelAttributes.Color := clBlack;
  InfosRE.Lines.Add('Traitement de la séquence type : "' +
                    DocDOCOMETRE.ST[numST].NOM + '"');
  InfosRE.Lines.Add('');

  CurentXMLST := DocDOCOMETRE.ST[numST];

  if SystemType = 'ADWINPRO'
  then Code_INCLUDE.Add('#INCLUDE ADWINPRO.INC');

  if SystemType = 'ADWINPRO II'
  then Code_INCLUDE.Add('#INCLUDE ADwinPro1.inc');

  if SystemType = 'ADWINPRO II Old Cards'
  then Code_INCLUDE.Add('#INCLUDE ADwinPro1.inc');

  Code_INCLUDE.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\DOCOLIB.INC');

  Code_VAR.Add('');
  Code_VAR.Add('#DEFINE temps fpar_' + inttostr(num_process));


  Code_VAR.Add('');
  Code_VAR.Add('DIM FEG as INTEGER');
  Code_VAR.Add('DIM INDEX as INTEGER');
  Code_VAR.Add('');


  Code_LOWINIT.Add('LOWINIT :');
  Code_LOWINIT.Add('FEG = ' +   inttostr(DocDOCOMETRE.EXPERIENCE.FEG));

  if((SystemType = 'ADWINPRO') or (SystemType = 'ADWINGOLD'))
  then  Code_LOWINIT.Add('GLOBALDELAY = 10^9/(FEG * 25)');

  if(SystemType  ='ADWINPRO II')
  then  Code_LOWINIT.Add('GLOBALDELAY = 10^9/(FEG * 3.3)');

  if(SystemType  ='ADWINPRO II Old Cards')
  then  Code_LOWINIT.Add('GLOBALDELAY = 10^9/(FEG * 3.3)');

  Code_LOWINIT.Add('INDEX = 0');
  Code_LOWINIT.Add('');

  Code_CODE.Add('EVENT : ');
  if DocDOCOMETRE.PSERIE.Count > 0 then
  begin
    Code_VAR.Add('DIM IN_TIMER AS LONG'); 
    Code_CODE.Add('IN_TIMER = READ_TIMER()');
  end;
  Code_CODE.Add('temps = INDEX/FEG');

  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;

  //Creation de la declaration du tableau de params si necessaire
  if DocDOCOMETRE.CANINT.Count > 0 then
  begin
    n := 0;
    for i := 0 to DocDOCOMETRE.CANINT.Count - 1
    do if DocDOCOMETRE.CANINT[i].GENRE = 'PARAMETRE'
       then inc(n);
    if n > 0 then
    begin
      Code_VAR.Add('REM ******** Tableau de parametres');
      Code_VAR.Add('#DEFINE TAB_PARAM DATA_200');
      Code_VAR.Add('DIM TAB_PARAM[' + inttostr(n) + '] AS FLOAT');
      Code_VAR.Add('');
    end;
  end;

  //Entrees analogiques
  if DocDOCOMETRE.EAN.Count > 0 then
  begin
    if SystemType = 'ADWINPRO' then
    begin
      Code_INCLUDE.Add('#INCLUDE ADWPAD.INC');
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANIN.INC');
    end;
    if SystemType = 'ADWINGOLD' then
    begin
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANINGold.INC');
    end;
    if SystemType = 'ADWINPRO II' then
    begin
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANINPro2.INC');
    end;
    if SystemType = 'ADWINPRO II Old Cards' then
    begin
     Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANINPro2OldCards.INC');
    end;
    Code_LIBRARY.Add('');
    
    Code_VAR.Add('REM ******** Dynamique Entrees analogiques');
    if DocDOCOMETRE.CTRLEAN.AMPLITUDE = '-10-10V' then
    begin
      Code_VAR.Add('#DEFINE Analog_In_Max 10.0');
      Code_VAR.Add('#DEFINE Analog_In_Min -10.0');
    end;
    if DocDOCOMETRE.CTRLEAN.AMPLITUDE = '-5-5V' then
    begin
      Code_VAR.Add('#DEFINE Analog_In_Max 5.0');
      Code_VAR.Add('#DEFINE Analog_In_Min -5.0');
    end;
    if DocDOCOMETRE.CTRLEAN.AMPLITUDE = '0-10V' then
    begin
      Code_VAR.Add('#DEFINE Analog_In_Max 10.0');
      Code_VAR.Add('#DEFINE Analog_In_Min 0.0');
    end;
    Code_VAR.Add('');

    if SystemType = 'ADWINPRO' then
    begin
      if DocDOCOMETRE.CTRLEAN.SEDIFF = 'SE'
      then Code_LOWINIT.Add('SE_DIFF(1,0) ''******** Mode entrées analogiques')
      else Code_LOWINIT.Add('SE_DIFF(1,1)''******** Mode entrées analogiques');
    end;

    if SystemType = 'ADWINPRO II' then
    begin
      if DocDOCOMETRE.CTRLEAN.SEDIFF = 'SE'
      then Code_LOWINIT.Add('SE_DIFF(1,0) ''******** Mode entrées analogiques')
      else Code_LOWINIT.Add('SE_DIFF(1,1)''******** Mode entrées analogiques');
    end;

    if SystemType = 'ADWINPRO II Old Cards' then
    begin
      if DocDOCOMETRE.CTRLEAN.SEDIFF = 'SE'
      then Code_LOWINIT.Add('SE_DIFF(1,0) ''******** Mode entrées analogiques')
      else Code_LOWINIT.Add('SE_DIFF(1,1)''******** Mode entrées analogiques');
    end;
    
    for i := 0 to DocDOCOMETRE.EAN.Count - 1 do
    begin
      n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.EAN[i].NOM);
      AEAN := DocDOCOMETRE.EAN[i];

      Code_VAR.Add('REM ******** Entree analogique : ' + AEAN.NOM);
      Code_VAR.Add('DIM ' + InfoCanaux.Nom[n] + ' AS FLOAT');
      Code_VAR.Add('DIM Acquisition_' + InfoCanaux.Nom[n] + ' AS LONG');
      Code_VAR.Add('DIM Nouveau_' + InfoCanaux.Nom[n] + ' AS LONG');
      Code_VAR.Add('');

      Code_LOWINIT.Add('Acquisition_' + InfoCanaux.Nom[n] + ' = 0 ''******** init acquisition ' + AEAN.NOM);

      Code_CODE.Add('');
      Code_CODE.Add('Nouveau_' + InfoCanaux.Nom[n] + ' = 0');
      Code_CODE.Add('IF (INDEX = Acquisition_' + InfoCanaux.Nom[n] + ') THEN');
      S := '   ' + InfoCanaux.Nom[n] + ' = Call_AnIn(';
      S := S + inttostr(AEAN.NUMERO) + ',' + inttostr(AEAN.GAIN) + ',';
      S := S + floattostr(AEAN.AMIN) + ',' + floattostr(AEAN.AMAX) + ',';
      S := S + floattostr(AEAN.UMIN) + ',' + floattostr(AEAN.UMAX) + ')';
      Code_CODE.Add(S);
      Code_CODE.Add('   Acquisition_' + InfoCanaux.Nom[n] + ' = Acquisition_' + InfoCanaux.Nom[n] + ' + ' + InfoCanaux.RappFreq[n]);
      Code_CODE.Add('   Nouveau_' + InfoCanaux.Nom[n] + ' = 1');
      Code_CODE.Add('ENDIF');
    end;
    Code_CODE.Add('');
    Code_LOWINIT.Add('');
  end;

  //Sorties analogiques
  if DocDOCOMETRE.SAN.Count > 0 then
  begin
    if SystemType = 'ADWINPRO' THEN
    begin
      Code_INCLUDE.Add('#INCLUDE ADWPDA.INC');
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANOUT.INC');
    end;
    if SystemType = 'ADWINGOLD' then
    begin
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANOUTGold.INC');
    end;
    if SystemType = 'ADWINPRO II' then
    begin
       Code_INCLUDE.Add('#INCLUDE ADwinPro2.inc');
       Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANOUTPro2.INC');
    end;
    if SystemType = 'ADWINPRO II Old Cards' then
    begin
       Code_INCLUDE.Add('#INCLUDE ADwinPro2.inc');
       Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLANOUT.INC');
    end;

    Code_LIBRARY.Add('');

    Code_VAR.Add('''******** Dynamique Sorties analogiques');
    Code_VAR.Add('#DEFINE Analog_Out_Max 10.0');
    Code_VAR.Add('#DEFINE Analog_Out_Min -10.0');
    Code_VAR.Add('');

    for i := 0 to DocDOCOMETRE.SAN.Count - 1 do
    begin
      n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.SAN[i].NOM);
      ASAN := DocDOCOMETRE.SAN[i];

      Code_VAR.Add('REM ******** Sortie analogique : ' + ASAN.NOM);
      Code_VAR.Add('DIM ' + InfoCanaux.Nom[n] + ' AS FLOAT');
      Code_VAR.Add('DIM Generation_' + InfoCanaux.Nom[n] + ' AS LONG');
      Code_VAR.Add('');

      Code_LOWINIT.Add('Generation_' + InfoCanaux.Nom[n] + ' = 0 ''******** init generation ' + ASAN.NOM);

      Code_Generation.Add('IF (INDEX = Generation_' + InfoCanaux.Nom[n] + ') THEN');
      Code_Generation.Add('    Call_AnOut(' + inttostr(ASAN.NUMERO) + ',' + InfoCanaux.Nom[n] + ')');
      Code_Generation.Add('    Generation_' + InfoCanaux.Nom[n] + ' = Generation_' + InfoCanaux.Nom[n] + ' + ' + 
InfoCanaux.RappFreq[n]);
      Code_Generation.Add('ENDIF');
      Code_Generation.Add('');
    end;
    Code_LOWINIT.Add('');
  end;

  if (DocDOCOMETRE.ENUM.Count > 0) or (DocDOCOMETRE.SNUM.Count > 0) then
  begin
   if ((SystemType = 'ADWINPRO') or (SystemType = 'ADWINPRO II Old Cards')) then
   begin
     N1 := 0;
     N2 := 0;
     for i := 0 to DocDOCOMETRE.SNUM.Count - 1 do
     begin
       if DocDOCOMETRE.SNUM[i].NUMERO <= 16
       then N1 := N1 + trunc(power(2,DocDOCOMETRE.SNUM[i].NUMERO-1))
       else N2 := N2 + trunc(power(2,DocDOCOMETRE.SNUM[i].NUMERO-1-16));
     end;
     Code_LOWINIT.Add('REM ******** Configuration Entrees/Sorties numeriques');
     Code_LOWINIT.Add('DIGPROG1(1,' + inttostr(N1) + ')');
     Code_LOWINIT.Add('DIGPROG2(1,' + inttostr(N2) + ')');
   end;
   if SystemType = 'ADWINGOLD' then
   begin
     Code_LOWINIT.Add('REM ******** Configuration Entrees/Sorties numeriques');
     Code_LOWINIT.Add('CONF_DIO(1100b)');
   end;
   if SystemType = 'ADWINPRO II' then
   begin
     B1 := 0;
     B2 := 0;
     B3 := 0;
     B4 := 0;
     for i := 0 to DocDOCOMETRE.SNUM.Count - 1 do
     begin
       num := DocDOCOMETRE.SNUM[i].NUMERO;
       if (num > 24) then B4 := 8;
       if ((num > 16) and (num <= 24)) then B3 := 4;
       if ((num > 8) and (num <= 16)) then B2 := 2;
       if (num <= 8) then B1 := 1;
     end;

     N2 := B4+B3+B2+B1;

     Code_LOWINIT.Add('REM ******** Configuration Entrees/Sorties numeriques');
     Code_LOWINIT.Add('P2_DIGPROG(1,' + inttostr(N2) + ')');

   end;
   Code_LOWINIT.Add('');



  end;

  //Entrees numeriques
  if DocDOCOMETRE.ENUM.Count > 0 then
  begin
    if SystemType = 'ADWINPRO' then
    begin
      Code_INCLUDE.Add('#INCLUDE ADWPDIO.INC');
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGIN.INC');
    end;
    if SystemType = 'ADWINGOLD' then
    begin
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGINGold.INC');
    end;
    if SystemType = 'ADWINPRO II' then
    begin
       Code_INCLUDE.Add('#INCLUDE ADwinPro2.inc');
       Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGINPro2.INC');
    end;
    if SystemType = 'ADWINPRO II Old Cards' then
    begin
       Code_INCLUDE.Add('#INCLUDE ADwinPro2.inc');
       Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGINPro2OldCards.INC');
    end;
    Code_LIBRARY.Add('');

    Code_LOWINIT.Add('Acquisition_ENUM = 0 ''******** init acquisition ENUM');

    Code_VAR.Add('REM ******** Entrees numeriques');
    Code_VAR.Add('DIM Acquisition_ENUM AS LONG');

    Code_CODE.Add('IF (INDEX = Acquisition_ENUM) THEN');
    n := 0;
    for i := 0 to DocDOCOMETRE.ENUM.Count - 1 do
    begin
      n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.ENUM[i].NOM);
      AENUM := DocDOCOMETRE.ENUM[i];
      Code_VAR.Add('DIM ' + InfoCanaux.Nom[n] + ' AS LONG');
      S := '   ' + InfoCanaux.Nom[n] + ' = Call_DigIn(' + inttostr(AENUM.NUMERO) + ')';
      Code_CODE.Add(S);
    end;
    Code_VAR.Add('');
    Code_CODE.Add('   Acquisition_ENUM = Acquisition_ENUM' + ' + ' + InfoCanaux.RappFreq[n]);
    Code_CODE.Add('ENDIF');
    Code_CODE.Add('');
    Code_LOWINIT.Add('');
  end;

  //Sorties numeriques
  if DocDOCOMETRE.SNUM.Count > 0 then
  begin
    if SystemType = 'ADWINPRO' then
    begin
      Code_INCLUDE.Add('#INCLUDE ADWPDIO.INC');
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGOUT.INC');
    end;
    if SystemType = 'ADWINGOLD' then
    begin
      Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGOUTGold.INC');
    end;
    if SystemType = 'ADWINPRO II' then
    begin
       Code_INCLUDE.Add('#INCLUDE ADwinPro2.inc');
       Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGOUTPro2.INC');
    end;
    if SystemType = 'ADWINPRO II Old Cards' then
    begin
       Code_INCLUDE.Add('#INCLUDE ADwinPro2.inc');
       Code_LIBRARY.Add('#INCLUDE ' + Rep_Racine + 'Fonctions\Inf\CALLDIGOUT.INC');
    end;
    Code_LIBRARY.Add('');

    Code_LOWINIT.Add('Generation_SNUM = 0 ''******** init generation SNUM');

    Code_VAR.Add('REM ******** Sorties numeriques');
    Code_VAR.Add('DIM Generation_SNUM AS LONG');

    Code_Generation.Add('IF (INDEX = Generation_SNUM) THEN');
    n := 0;
    for i := 0 to DocDOCOMETRE.SNUM.Count - 1 do
    begin
      n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.SNUM[i].NOM);
      ASNUM := DocDOCOMETRE.SNUM[i];
      Code_VAR.Add('DIM ' + InfoCanaux.Nom[n] + ' AS LONG');
      S := '   ' + 'Call_DigOut(' + inttostr(ASNUM.NUMERO) + ',' + InfoCanaux.Nom[n] +')';
      Code_Generation.Add(S);
    end;
    Code_VAR.Add('');
    Code_Generation.Add('   Generation_SNUM = Generation_SNUM' + ' + ' + InfoCanaux.RappFreq[n]);
    Code_Generation.Add('ENDIF');
    Code_Generation.Add('');
    Code_LOWINIT.Add('');
  end;

  //Canaux internes
  if DocDOCOMETRE.CANINT.Count > 0 then
  begin
    for i := 0 to DocDOCOMETRE.CANINT.Count - 1 do
    begin
      ACANINT := DocDOCOMETRE.CANINT[i];
      n := InfoCanaux.Nom.IndexOf(ACANINT.NOM);
      //if DocDOCOMETRE.CANINT[i].TAILLE = 1
      //then n := InfoCanaux.Nom.IndexOf(ACANINT.NOM)
      //On recupere que le premier element du tableur c'est suffisant
      //else n := InfoCanaux.Nom.IndexOf(ACANINT.NOM + '[1]');

      if InfoCanaux.purORparam[n] = 'PUR' then
      begin
        //Si c'est pas un paramètre
        if ACANINT.TAILLE = 1 then
        begin
          Code_VAR.Add('REM ******** Canal interne (scalaire) : ' + ACANINT.NOM);
          if ACANINT.FORMAT = 'Flottant'
          then Code_VAR.Add('DIM ' + ACANINT.NOM + ' AS FLOAT')
          else Code_VAR.Add('DIM ' + ACANINT.NOM  + ' AS LONG');
        end else
        begin
          Code_VAR.Add('REM ******** Canal interne (tableau) : ' + ACANINT.NOM);
          if ACANINT.FORMAT = 'Flottant'
          then Code_VAR.Add('DIM ' + ACANINT.NOM + '[' + inttostr(ACANINT.TAILLE) + '] AS FLOAT')
          else Code_VAR.Add('DIM ' + ACANINT.NOM  + '[' + inttostr(ACANINT.TAILLE) + '] AS LONG');
        end;
        Code_VAR.Add('DIM Acquisition_' + ACANINT.NOM  + ' AS LONG');
        Code_VAR.Add('DIM Nouveau_' + ACANINT.NOM  + ' AS LONG');

        Code_LOWINIT.Add('Acquisition_' + ACANINT.NOM + ' = 0 ''******** init acquisition ' + ACANINT.NOM);

        Code_CODE.Add('');
        Code_CODE.Add('Nouveau_' + ACANINT.NOM + ' = 0');
        Code_CODE.Add('IF (INDEX = Acquisition_' + ACANINT.NOM + ') THEN');
        Code_CODE.Add('   Acquisition_' + ACANINT.NOM + ' = Acquisition_' + ACANINT.NOM + ' + ' + InfoCanaux.RappFreq[n]);
        Code_CODE.Add('   Nouveau_' + ACANINT.NOM + ' = 1');

        {if InfoCanaux.ISArray[n] = '1' then
        begin
          for j := 1 to ACANINT.TAILLE do
          begin
            Code_VAR.Add('DIM Nouveau_' + ACANINT.NOM + inttostr(j) + ' AS LONG');
            Code_CODE.Add('   Nouveau_' + ACANINT.NOM + inttostr(j) + ' = 1');
          end;
        end;  }


        Code_CODE.Add('ENDIF');
      end else
      begin
        //Si c'est un paramètre
        Code_VAR.Add('REM ******** Paramètre : ' + ACANINT.NOM);
        if InfoCanaux.floatORint[n] = 'float'
        then Code_VAR.Add('DIM ' + InfoCanaux.Nom[n] + ' AS FLOAT')
        else Code_VAR.Add('DIM ' + InfoCanaux.Nom[n] + ' AS LONG');

        Code_LOWINIT.Add(InfoCanaux.Nom[n] + ' = TAB_PARAM[' + floattostr(ACANINT.FENUMPAR) + '] ''******** init param ' + ACANINT.NOM);
        Code_FINISH.Add('TAB_PARAM[' + floattostr(ACANINT.FENUMPAR) + '] = ' + InfoCanaux.Nom[n]+ '''******** maj param ' + ACANINT.NOM);

      end;
    end;
    Code_VAR.Add('');
    Code_CODE.Add('');
    Code_LOWINIT.Add('');
  end;
  
  //Ports série
  for i := 0 to DocDOCOMETRE.PSERIE.Count - 1
  do TraiterPSerie(DocDOCOMETRE.PSERIE[i]);

  //Generation code des blocs
  for i := 0 to CurentXMLST.BLOC.Count - 1  do
  begin
    InfosRE.Paragraph.Alignment := taLeftJustify;
    InfosRE.SelAttributes.Style := [];
    InfosRE.SelAttributes.Size := 8;
    InfosRE.SelAttributes.Color := clBlack;
    InfosRE.Lines.Add('   Traitement du bloc : ' + inttostr(i));

    GENECODE_BLOC(CurentXMLST.BLOC[i],0);
    Code_CODE.Add('');
  end;

  Code_FINISH.Insert(0,'FINISH :');
  Code_FINISH.Insert(0,'');
  Code_FINISH.Insert(0,'INC(index)');

  Code_VAR.Add('DIM TERMINER_SEQUENCE_TYPE AS LONG');
  Code_VAR.Add('');
  Code_LOWINIT.Add('TERMINER_SEQUENCE_TYPE = 0');
  Code_LOWINIT.Add('');
  Code_Generation.add('IF (TERMINER_SEQUENCE_TYPE=1) THEN');
  Code_Generation.add('    END');
  Code_Generation.add('ENDIF');
   Code_Generation.add('');


  Code.AddStrings(Code_INCLUDE);
  Code.AddStrings(Code_IMPORT);
  Code.AddStrings(Code_VAR);
  Code.AddStrings(Code_FCTSSP);
  Code.AddStrings(Code_LIBRARY);
  Code.AddStrings(Code_LOWINIT);
  Code.AddStrings(Code_CODE);
  Code.AddStrings(Code_Generation);
  Code.AddStrings(Code_FINISH);

  //Insertion de l'entête du fichier ADBasic pour manipulation num process
  if(adBasicVersion = 4) then
  begin
    Code.Insert(0,'****************************************');
    Code.Insert(0,'*	 SAVECOMPIL = 0');
    Code.Insert(0,'*	 OPT_LEVEL = 1');
    Code.Insert(0,'*	 ATSRAM = 0');
    Code.Insert(0,'*	 AdbasicVersion = 4000001');
    Code.Insert(0,'*	 FastStop = 0');
    Code.Insert(0,'*	 Version = 1');
    Code.Insert(0,'*	 Priorität = 0');
    Code.Insert(0,'*	 Number of Loops = 0');
    Code.Insert(0,'*	 Eventsource = 0');
    Code.Insert(0,'*	 Delay = 1000');
    Code.Insert(0,'*	 Prozeßnummer = ' + inttostr(Num_Process));
    Code.Insert(0,'****************************************');
  end;

  if(adBasicVersion = 5) then
  begin
    Code.Insert(0,'''<ADbasic Header, Headerversion 001.001>');
    Code.Insert(1,''' Process_Number                 = ' + inttostr(Num_Process));
    Code.Insert(2,''' Initial_Processdelay           = 1000');
    Code.Insert(3,''' Eventsource                    = Timer');
    Code.Insert(4,''' Control_long_Delays_for_Stop   = No');
    Code.Insert(5,''' Priority                       = High');
    Code.Insert(6,''' Version                        = 1');
    Code.Insert(7,''' ADbasic_Version                = 5.0.2');
    Code.Insert(8,''' Optimize                       = Yes');
    Code.Insert(9,''' Optimize_Level                 = 1');
    Code.Insert(10,''' Info_Last_Save                 = PC  PC\anonymous');
    Code.Insert(11,'''<Header End>');
  end;
  
  ForceDirectories(Rep_manip + 'BinSources');
  Code.SaveToFile(Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS');

  InfoCanaux.Free;
  Code_INCLUDE.Free;
  Code_IMPORT.Free;
  Code_VAR.Free;
  Code_LOWINIT.Free;
  Code_CODE.Free;
  Code_FINISH.Free;
  Code_FCTSSP.Free;
  Code_LIBRARY.Free;
  Code_Generation.Free;
  Code.Free;

  InfosRE.Lines.Add('');
  InfosRE.SelStart := length(InfosRE.Text);
  InfosRE.ScrollBy(0,InfosRE.Lines.Count*InfosRE.Font.Height);
  //InfosRE.SelStart := InfosRE.Lines.Count - 1;
  //SendMessage(InfosRE.Handle, EM_SCROLLCARET, InfosRE.Lines.Count - 1,0);
end;


procedure TExecFrame.GeneSpeedButtonClick(Sender: TObject);
var i : integer;
    buffer,buffer2     :   LPTSTR;
    StartupInfo : TStartupInfo;
    ProcessInfo : TProcessInformation;
    LigneDeCmde : TStringList;
    S : string;
    checked : boolean;
begin

  checked := false;
  for i := 0 to ListeSTCB.Count - 1
  do checked := checked or ListeSTCB.Checked[i];
  if checked then
  begin
    ForceDirectories(Rep_manip + 'BinSources');
    //Suppression de tous les .bas et .t9x sélectionné
    S := '';
    LigneDeCmde := TStringList.Create;
    for i := 0 to ListeSTCB.Count - 1 do
    begin
      if ListeSTCB.Checked[i] then
      begin
        S := 'del ' + ListeSTCB.Items[i] + '.bas';
        LigneDeCmde.Add(S);
        if ListeSTCB.Checked[i] then S := 'del ' + ListeSTCB.Items[i] + '.T' + cpuID + inttostr(num_Process);
        LigneDeCmde.Add(S);
      end;
    end;
    LigneDeCmde.Add('del *.ERR');
    LigneDeCmde.SaveToFile(Rep_manip + 'BinSources\deleteBAS.bat');
    LigneDeCmde.free;

    if S <> '' then
    begin
      buffer := LPCTSTR(Rep_manip + 'BinSources\deleteBAS.bat');
      buffer2 := LPCTSTR(Rep_manip + 'BinSources');
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := SW_HIDE;
      //StartupInfo.wShowWindow := SW_SHOW;

      if not CreateProcess(buffer,
                              nil,   { pointer to command line string }
                              nil,   { pointer to process security attributes }
                              nil,   { pointer to thread security attributes }
                              false, { handle inheritance flag }
                              CREATE_NEW_CONSOLE or
                              NORMAL_PRIORITY_CLASS, { creation flags }
                              nil,   { pointer to new environment block }
                              buffer2,   { pointer to current directory name }
                              StartupInfo,  { pointer to STARTUPINFO }
                              ProcessInfo)  { pointer to PROCESS_INF }
      then showmessage('Impossible de supprimer les fichiers sources !');
      WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
    end;

    InfosRE.Clear;

    InfosRE.Paragraph.Alignment := taCenter;
    InfosRE.SelAttributes.Style := [fsBold];
    InfosRE.SelAttributes.Size := 12;
    InfosRE.SelAttributes.Color := clREd;
    //InfosRE.Lines.Add(DateToStr(Date) + ' - ' + TimeToStr(Time));
    InfosRE.Lines.Add('GENERATION DU CODE ADBASIC');
    InfosRE.Lines.Add('');
    for i := 0 to ListeSTCB.Count - 1
    do if ListeSTCB.Checked[i] then GeneCode(i);

    MAJBinsSourcesLV;
  end else showmessage('Aucune séquence type de sélectionné !');
end;


function TExecFrame.Compile(numST : integer):boolean;
var buffer     :   LPTSTR;
    StartupInfo : TStartupInfo;
    ProcessInfo : TProcessInformation;
    LigneDeCmde : TStringList;
    S : string;
    rep : boolean;
    numProcessString : String;
begin
  //Creation de la ligne de commande
  //c:\adwin\adbasic\adbasic.exe /M c:\XXX\YYY\nomst.bas /SP /P9
  //Sauvegarde ds Compile.bat
  numProcessString := IntToStr(Num_Process);
  if(adBasicVersion = 4)
  then S := ADBasicIDE + ' /M "' + Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS" /SP /P9 /PN' + Num_Process
  else begin
    if(SystemType = 'ADWINPRO')
    then  S := adBasicCompiler + ' /M "' + Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS" /SP /P9 /PN' + Num_Process;
    if(SystemType = 'ADWINGOLD')
    then  S := adBasicCompiler + ' /M "' + Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS" /SG /P9 /PN' + Num_Process;
    if(SystemType = 'ADWINPRO II')
    then  S := adBasicCompiler + ' /M "' + Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS" /SPII /P11 /PN' + Num_Process;
    if(SystemType = 'ADWINPRO II Old Cards')
    then  S := adBasicCompiler + ' /M "' + Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS" /SPII /P11 /PN' + Num_Process;
  end;
  LigneDeCmde := TStringList.Create;
  LigneDeCmde.Add(S);
  LigneDeCmde.SaveToFile(Rep_Manip + 'BinSources\Compile.bat');
  LigneDeCmde.free;
  //Execution de Compile.bat
  buffer := LPCTSTR(Rep_manip + 'BinSources\Compile.bat');

  FillChar(StartupInfo,Sizeof(StartupInfo),#0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_HIDE;
  //StartupInfo.wShowWindow := SW_SHOW;

  rep := CreateProcess(buffer,
                          nil,   { pointer to command line string }
                          nil,   { pointer to process security attributes }
                          nil,   { pointer to thread security attributes }
                          false, { handle inheritance flag }
                          CREATE_NEW_CONSOLE or
                          NORMAL_PRIORITY_CLASS, { creation flags }
                          nil,   { pointer to new environment block }
                          nil,   { pointer to current directory name }
                          StartupInfo,  { pointer to STARTUPINFO }
                          ProcessInfo) ; { pointer to PROCESS_INF }

  if rep
  then WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
  if not FileExists(Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[numST].NOM + '.BAS') then
  begin
    InfosRE.Paragraph.Alignment := taLeftJustify;
    InfosRE.SelAttributes.Style := [fsBold];
    InfosRE.SelAttributes.Size := 10;
    InfosRE.SelAttributes.Color := clRed;
    InfosRE.Lines.Add('Le fichier "' +  Rep_manip + 'BinSources\' +
            DocDOCOMETRE.ST[numST].NOM + '.BAS' + '" n''existe pas');
    InfosRE.Lines.Add('');
  end;

  result := rep;

end;

procedure TExecFrame.CompSpeedButtonClick(Sender: TObject);
var i,j : integer;
    errorFile : TStringList;
    S : string;
    checked : boolean;
begin
  checked := false;
  for i := 0 to ListeSTCB.Count - 1
  do checked := checked or ListeSTCB.Checked[i];
  if checked then
  begin

    ForceDirectories(Rep_manip + 'BinSources');

    InfosRE.SelStart := length(InfosRE.Text);

    InfosRE.Paragraph.Alignment := taCenter;
    InfosRE.SelAttributes.Style := [fsBold];
    InfosRE.SelAttributes.Size := 12;
    InfosRE.SelAttributes.Color := clREd;
    //InfosRE.Lines.Add(DateToStr(Date) + ' - ' + TimeToStr(Time));
    InfosRE.Lines.Add('COMPILATION DU CODE ADBASIC');
    InfosRE.Lines.Add('');

    for i := 0 to ListeSTCB.Count - 1 do
    begin
      if ListeSTCB.Checked[i] then
      begin
        InfosRE.Paragraph.Alignment := taLeftJustify;
        InfosRE.SelAttributes.Style := [fsBold,fsUnderline];
        InfosRE.SelAttributes.Size := 10;
        InfosRE.SelAttributes.Color := clBlack;
        //InfosRE.Lines.Add(DateToStr(Date) + ' - ' + TimeToStr(Time));
        InfosRE.Lines.Add('Traitement de la séquence type : "' + ListeSTCB.Items[i] + '"');
        InfosRE.Lines.Add('');

        if not Compile(i) then
        begin
          showmessage('le fichier "ADBasic.exe" à exécuter n''existe pas');
          break;
        end else
        begin
          if FileExists(Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[i].NOM + '.ERR') then
          begin
            errorFile := TStringList.Create;
            errorFile.LoadFromFile(Rep_manip + 'BinSources\' + DocDOCOMETRE.ST[i].NOM + '.ERR');
            InfosRE.Paragraph.Alignment := taLeftJustify;
            InfosRE.SelAttributes.Style := [];
            InfosRE.SelAttributes.Size := 8;
            InfosRE.SelAttributes.Color := clRed;
            //InfosRE.Lines.Add(DateToStr(Date) + ' - ' + TimeToStr(Time));
            InfosRE.Lines.Add('   ***   Une ou des erreurs lors de la compilation avec les messages suivants :');
            InfosRE.Lines.Add('');

            for j := 0 to errorFile.Count - 1 do
            begin
              InfosRE.Paragraph.Alignment := taLeftJustify;
              InfosRE.SelAttributes.Style := [fsBold];
              InfosRE.SelAttributes.Size := 8;
              InfosRE.SelAttributes.Color := clRed;
              S := errorFile[j];
              repeat
              if pos(#9,S) <> 0
                then insert(#$D+#$A,S,pos(#9,S));
                delete(S,pos(#9,S),1);
              until pos(#9,S) = 0;
              InfosRE.Lines.Add(S);
            end;
            InfosRE.Lines.Add('');

            InfosRE.Paragraph.Alignment := taLeftJustify;
            InfosRE.SelAttributes.Style := [];
            InfosRE.SelAttributes.Size := 8;
            InfosRE.SelAttributes.Color := clRed;
            InfosRE.Lines.Add('   ***   Compilation KO');
            InfosRE.Lines.Add('');
          end else
          begin
            InfosRE.Paragraph.Alignment := taLeftJustify;
            InfosRE.SelAttributes.Style := [];
            InfosRE.SelAttributes.Size := 8;
            InfosRE.SelAttributes.Color := clgreen;
            //InfosRE.Lines.Add(DateToStr(Date) + ' - ' + TimeToStr(Time));
            InfosRE.Lines.Add('   ***   Compilation OK');
            InfosRE.Lines.Add('');
          end;
          InfosRE.SelStart := length(InfosRE.Text);
          InfosRE.ScrollBy(0,InfosRE.Lines.Count*InfosRE.Font.Height);
        end;
      end;
    end;

    MAJBinsSourcesLV;
  end else showmessage('Aucune séquence type de sélectionnée !');
end;

procedure TExecFrame.BinsSourcesLVDblClick(Sender: TObject);
var buffer,buffer2     :   LPTSTR;
    StartupInfo : TStartupInfo;
    ProcessInfo : TProcessInformation;
begin
  if BinsSourcesLV.Selected <> nil then
  begin
    if ExtractFileExt(BinsSourcesLV.Selected.Caption) = '.BAS' then
    begin
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := SW_SHOW;
      buffer := LPCTSTR(ADBasicIDE);//LPCTSTR(ADBasic + ' ' + Rep_Manip + 'BinSources\' + BinsSourcesLV.Selected.Caption);
      buffer2 := LPCTSTR(' "' + Rep_Manip + 'BinSources\' + BinsSourcesLV.Selected.Caption +'"' );
      if not CreateProcess(buffer,
        buffer2,                 { pointer to command line string }
        nil,                           { pointer to process security attributes }
        nil,                           { pointer to thread security attributes }
        false,                         { handle inheritance flag }
        CREATE_NEW_CONSOLE or          { creation flags }
        NORMAL_PRIORITY_CLASS,
        nil,                           { pointer to new environment block }
        nil,                           { pointer to current directory name }
        StartupInfo,                   { pointer to STARTUPINFO }
        ProcessInfo) then              { pointer to PROCESS_INF }
        showmessage('le fichier à exécuter n'+' existe pas');
    end;
  end;
end;

procedure TExecFrame.BinsSourcesLVKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var S,S1,S2 : string;
begin
  if Key = VK_DELETE then
  begin
    if mrYes = messageDLG('Etes-vous certain de vouloir supprimer ce fichier ?',mtConfirmation,[mbYes,mbNo],0) then
    begin
      S := BinsSourcesLV.Selected.Caption;
      deletefile(Rep_manip + 'BinSources\' + S);
      if ExtractFileExt(S) = '.BAS' then
      begin
        delete(S,pos('.BAS',S),4);
        S1 := S + '.t9' + inttostr(num_Process);
        deletefile(Rep_manip + 'BinSources\' + S1);
        S2 := S + '.ERR';
        deletefile(Rep_manip + 'BinSources\' + S2);
      end;
      MAJBinsSourcesLV;
    end;
  end;
end;

procedure TExecFrame.TestSpeedButtonClick(Sender: TObject);
var checked : boolean;
    i,n : integer;
    buffer     :   LPTSTR;
    StartupInfo : TStartupInfo;
    ProcessInfo : TProcessInformation;
    LigneDeCmde : TStringList;
    S : string;
begin
  n := -1;
  checked := false;
  for i := 0 to ListeSTCB.Count - 1 do
  begin
    checked := checked or ListeSTCB.Checked[i];
    if ListeSTCB.Checked[i] then
    begin
      n := i;
      break;
    end;
  end;
  if checked then
  begin
    if DocoForm.XMLDoco.Modified
    then DocoForm.EnregistrerActionExecute(Self);
    //Creation de la ligne de commande
    //Execute.exe TEST 0
    //Sauvegarde ds Run.bat
    S := '"' + Rep_racine + 'Execute.exe" "' + DocoForm.XMLDoco.FileName + '" TEST ' + inttostr(n);
    LigneDeCmde := TStringList.Create;
    LigneDeCmde.Add(S);
    LigneDeCmde.SaveToFile(Rep_manip + 'Run.bat');
    LigneDeCmde.free;
    //Execution de Run.bat
    buffer := LPCTSTR(Rep_manip + 'Run.bat');

    FillChar(StartupInfo,Sizeof(StartupInfo),#0);
    StartupInfo.cb := Sizeof(StartupInfo);
    StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
    StartupInfo.wShowWindow := SW_HIDE;
    //StartupInfo.wShowWindow := SW_SHOW;
    DocoForm.FermerActionExecute(Self);


    CreateProcess(buffer,
                            nil,   { pointer to command line string }
                            nil,   { pointer to process security attributes }
                            nil,   { pointer to thread security attributes }
                            false, { handle inheritance flag }
                            CREATE_NEW_CONSOLE or
                            NORMAL_PRIORITY_CLASS, { creation flags }
                            nil,   { pointer to new environment block }
                            nil,   { pointer to current directory name }
                            StartupInfo,  { pointer to STARTUPINFO }
                            ProcessInfo) ; { pointer to PROCESS_INF }


    saveMainFormPositions((DocoForm.WindowState = wsMaximized),DocoForm.left,DocoForm.Top, DocoForm.Width, DocoForm.Height);
    Application.Terminate;

  end else showmessage('Aucune séquence type de sélectionnée !');

end;

procedure TExecFrame.ExecSpeedButtonClick(Sender: TObject);
var numSujet, numSession,i : integer;
    buffer     :   LPTSTR;
    StartupInfo : TStartupInfo;
    ProcessInfo : TProcessInformation;
    LigneDeCmde : TStringList;
    S : string;
    OK : boolean;
begin
  if RunSujetForm.ShowModal = mrOK then
  begin

    OK := false;
    numSujet := RunSujetForm.SujetsCB.ItemIndex;
    if RunSujetForm.runSessionRG.ItemIndex = 0 then
    begin
      //Une seule session
      numSession := RunSujetForm.SessionsCB.ItemIndex;
      OK := (numSujet > -1) and (numSession > -1) and (RunSujetForm.essaiDoneGauge.Progress < 100);
      if OK
      then  S := '"' + Rep_racine + 'Execute.exe" "' + DocoForm.XMLDoco.FileName
                 + '" EXEC ' + inttostr(numSujet) + ' ' + inttostr(numSession);
    end else
    begin
      if numSujet > - 1 then
      begin
        S := '';
        //Plusieurs sessions
        for i := 0 to runSujetForm.usedSessionsLB.Count - 1 do
        begin
         numSession := strtoint(copy(runSujetForm.usedSessionsLB.Items[i],1,pos('-',runSujetForm.usedSessionsLB.Items[i])-1)) - 1;
         S := S + inttostr(numSession) + '-';
        end;

        delete(S,length(S),1);
        if runSujetForm.usedSessionsLB.Count > 1 then S := '(' + S + ')';
        OK := S <> '';
        if OK
        then S := '"' + Rep_racine + 'Execute.exe" "' + DocoForm.XMLDoco.FileName
                 + '" EXEC ' + inttostr(numSujet) + ' ' + S;
        //showmessage(S);
      end;
    end;

    if OK and DocoForm.XMLDoco.Modified
    then DocoForm.EnregistrerActionExecute(Self);
    OK := OK and not (DocoForm.XMLDoco.Modified);

    if OK then
    begin
      LigneDeCmde := TStringList.Create;
      LigneDeCmde.Add(S);
      LigneDeCmde.SaveToFile(Rep_manip + 'Run.bat');
      LigneDeCmde.free;
      //Execution de Run.bat
      buffer := LPCTSTR(Rep_manip + 'Run.bat');

      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := SW_HIDE;
      //StartupInfo.wShowWindow := SW_SHOW;
      DocoForm.FermerActionExecute(Self);

      CreateProcess(buffer,
                            nil,   { pointer to command line string }
                            nil,   { pointer to process security attributes }
                            nil,   { pointer to thread security attributes }
                            false, { handle inheritance flag }
                            CREATE_NEW_CONSOLE or
                            NORMAL_PRIORITY_CLASS, { creation flags }
                            nil,   { pointer to new environment block }
                            nil,   { pointer to current directory name }
                            StartupInfo,  { pointer to STARTUPINFO }
                            ProcessInfo) ; { pointer to PROCESS_INF }

      Application.Terminate;

    end else
    begin
      if RunSujetForm.essaiDoneGauge.Progress = 100
      then showmessage('La session est totalement effectuée !')
    end;
  end;
end;

end.
