Import fichier txt dans synoptique pcvue

Sabrina -  
 cyrilM -
Bonjour,
Je souhaite importer un fichier txt et l'afficher dans une listbox d'un synoptique pcvue, j'essai de le faire avec un script vba mais je n'y arrive.. Quelqu'un serait-il comment pouvoir le faire????

Je vous remercie d'avance.

1 réponse

  1. cyrilM
     
    Bonjour je ne sais pas si cela peu t'aider mais a partir de fichier dans PCVUE j'ai un exemple pour créer un fichier excel *.CSV

    'Version 1
    '************************************************************************
    'Ce programme permet d'obtenir des rapports journaliers et mensuels
    'au format csv directement exploitables sous EXCEL .
    '************************************************************************

    ' ** Utilisation ** :

    'Ce programme lit pour chaque rapport un fichier contenant la liste des variables .
    ' Un fichier facultatif pourra contenir le titre du rapport .
    'Ces fichiers seront dans la directory TP du projet .
    'On pourra les modifier dynamiquement en run time.

    'Par exemple le fichier report1 contiendra les noms de variables :
    'DATE
    'TIME
    'MOTEUR1.TEMPS
    'MOTEUR2.TEMPS

    'Le fichier facultatif Report1_Title contiendrait par exemple
    'les titres de chaque colonne :
    'Date;Heure;Temps Moteur1;Temps Moteur2
    'Ne pas oublier d'effectuer un saut de ligne en fin de ce fichier.

    'On obtiendra automatiquement un exemplaire de ces deux fichiers en executant le main( )
    'avec une variable creation_option valant 1 .

    'En sortie pour le rapport report1 par exemple on obtiendra les fichiers suivants :
    ' un journalier report1_Jour_Mois_Annee_D.csv
    ' un mensuel report1_Mois_Annee_M.csv
    'Le nom de ces fichiers est configurable en modifiant les variables Day_Format et
    'Month_Format

    'Ils seront dans la directory TP du projet mais on pourra changer le chemin
    'en modifiant la variable Path_reports du programme.

    ' Lancement:

    'Le programme Report.prg pourrait se lancer en utilisant l'instruction
    'PROGRAM ("START","/UTILITIES/Report_v1.prg" , "");
    'Il faut par contre l'adapter et donc le recopier dans la directory P de votre projet.
    'L'instruction devient alors :
    'PROGRAM ("START","Report_v1.prg" , "");

    'Il faut modifier la fonction main en ajoutant un appel Add_report pour chaque rapport voulu .
    'Par exemple pour obtenir deux rapports report1 et report2 la fonction contiendra
    'les deux lignes suivantes :
    'Add_report( "report1" ) ;
    'Add_report( "report2" ) ;

    'Execution :

    'Chaque appel de la fonction write_reports ( ) se traduira par une nouvel enregistrement
    'des valeurs des variables .

    '************************************************************************
    '************************************************************************
    '************************************************************************

    '************************************************************************
    '************************************************************************

    const Type_Bit=1 ;
    const Type_Register=2;
    const Type_Text=4 ;
    const Type_Alarm=8 ;
    const MAX_REPORTS=64 ;
    Dim Number_reports as integer ;
    Dim list_report[64] as str ;
    dim Path_reports as str; 'Path for the reports
    dim separator as str; 'separator
    Dim Day_Format as str; 'Configurable format of the day file report
    Dim Month_Format as str; 'Configurable format of the Month file report
    Dim title_extension as str; 'extension for the title file
    Dim creation_option as integer ; ' Option to create a sample files =1 create
    Dim main_executed as integer; ' ( =0 main program not executed ) (=1 executed )
    '*************************************************************

    '-------------------------------------------------------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------------------------------------------------------

    sub main ( )

    main_executed =1 ; 'The main program has been executed once

    '*************************************************************
    '**********Locals variables which can be modified ******
    '*************************************************************
    ' path for the generated files
    ' Path_reports ="d:\\Reports\\" ; 'to store in d:\reports for instance

    Path_reports ="" ; 'By default inside the TP directory of the project

    Path_reports ="D:\\IMP_MEASURE\\" ; 'to store in d:\reports for instance

    separator =";"; 'separator between values
    Title_extension ="_title" ; 'extension to get the report title Example report1_title

    'for the meaning of the # caracters check the scada basic datetimestring instruction
    Day_Format ="_#D_#M_###Y_Jour.csv" ; 'Format for the daily reports
    Month_Format ="_#M_###Y_Mois.csv" ; 'Format for the monthly reports

    creation_option =1; ' if 1 an automatic sample will be created .

    '*************************************************************

    Add_report( "CAR1" ) ;
    Add_report( "CAR2" ) ;
    Add_report( "CAR3" ) ;
    Add_report( "CAR4" ) ;
    Add_report( "CAR5" ) ;
    Add_report( "DEGREMONT AA 6" ) ;
    Add_report( "DEGREMONT AD 7" ) ;
    Add_report( "DEGREMONT BX 9" ) ;
    Add_report( "DEGREMONT BZ 10" ) ;
    Add_report( "DEGREMONT CZ 11" ) ;
    Add_report( "DEGREMONT DC 12" ) ;
    Add_report( "DEGREMONT DX 13" ) ;
    Add_report( "DEGREMONT DZ 14" ) ;
    Add_report( "DEGREMONT EN 15" ) ;
    Add_report( "DEGREMONT EZ 16" ) ;
    Add_report( "DEGREMONT FE 17" ) ;
    Add_report( "DEGREMONT FZ 18" ) ;
    Add_report( "DEGREMONT GW 19" ) ;
    Add_report( "CAR6" ) ;

    '*************************************************************
    ' Example to program a scheduler to write the reports each hour
    ' Crontab ("ADDPROG","EACH_HOUR","","00","Report_v1.prg","","write_reports");

    end sub

    '-------------------------------------------------------------------------------------------------------------------------------

    sub write_reports ( )
    dim i_report as integer;
    ' this function must be called to write a new record inside the differents reports
    'for example you can use a scheduled action with a frequency of each hour :
    '
    if ( main_executed ==0 ) then 'security
    main ( ); ' Execute the main program because it was not executed
    end if
    for ( i_report = 0 ; i_report < Number_reports ; i_report++ )
    'print ( i_report , " " , List_report[ i_report] );
    Write_record ( List_report[ i_report] ) ;
    next
    end sub

    '-------------------------------------------------------------------------------------------------------------------------------

    sub Add_report (which_report)
    ' This function adds a new report to the list

    dim ret as integer;
    dim sample_title as str;
    dim report_title_file as str;

    fclose(which_report );
    ret=fopen (which_report, "r") ;
    fclose(which_report );
    if (ret ==0 ) then 'report does not exist
    if ( creation_option ) then 'Create a sample for the report
    ' create a sample variable file
    create_file (which_report , "DATE\nTIME\n\SYSTEM.STATION_NUMBER") ;
    ' create a sample title file
    sample_title = concat("Example title ", which_report,";time;Number of the station\n") ;

    report_title_file=addstring (which_report,Title_extension) ;
    create_file (report_title_file,sample_title ) ;
    else
    print ("Unknown report file " , which_report );
    return ( 0) ;
    end if
    end if
    ' add to the list
    if (Number_reports < MAX_REPORTS ) then
    List_report[Number_reports]=which_report ;
    Number_reports=Number_reports +1 ;
    'print (Number_reports , " reports " ,which_report );
    else
    print (Number_reports , " Too many reports for " ,which_report );
    end if
    end sub

    '-------------------------------------------------------------------------------------------------------------------------------

    sub Write_record (which_report)
    ' This function adds a new record to the report named which_report
    '
    dim j_nbvar as integer;
    dim info as str;
    dim Value_Register as single; 'Register value
    dim Value_Bit as integer; 'Bit value
    dim Value_Text as str; 'Text value
    dim report_title_file as str ; 'Title for the report
    dim model_buffer as long;
    dim eof as integer;
    dim variable_name as str; 'variable name
    dim Destination_File as str;
    dim Day_file as str;
    dim Month_file as str;
    dim File_Format as str;
    dim ret as integer;
    dim Type as integer; 'variable type
    j_nbvar=0; 'variable index

    fclose(which_report );
    ret=fopen (which_report, "r") ;
    if (ret ==0 ) then 'file does not exist
    print ("Unknown file " , which_report );
    return ( 0 );
    end if

    Destination_File=addstring(Path_reports ,which_report ); 'adds the path

    File_Format=datetimestring (datetimevalue ( ), Day_Format );
    Day_file=addstring (Destination_File,File_Format); 'Day file name
    File_Format=datetimestring (datetimevalue ( ), Month_Format );
    Month_file=addstring (Destination_File,File_Format); 'Month file name

    report_title_file=addstring (which_report,Title_extension) ;
    model_buffer=0;
    model_buffer=filetobuf(report_title_file);
    if ( model_buffer ) then 'there is a title file
    ' Copy_model ( model_buffer,Day_file ) ; 'copies a model to Day_file
    Copy_model ( model_buffer,Month_file ) ; 'copies a model to the Month_file
    free_buffer (model_buffer); 'free the allocated buffer
    end if

    'fclose (Day_file);
    fclose (Month_file);
    'fopen ( Day_file , "a" ); 'open to write at the end (append mode)
    fopen ( Month_file , "a" ); 'open to write at the end (append mode)

    'print (" files ", Day_file, " " ,Month_file);
    eof=0;
    while (eof==0)
    variable_name=fgets( which_report, 128);
    variable_name=ltrim( rtrim ( variable_name)); 'remove spaces
    variable_name=ucase(variable_name) ; 'majuscules
    eof=feof( which_report);

    if ( variable("STATUS",variable_name,"exist" ) )then
    Type=variable("GET_TYPE",variable_name);
    if (Type==Type_Register )then

    if ( cmpstring (right(variable_name,4),".CTM" )==0 ) then
    Value_Register=(?variable_name/3600); 'Register value

    else
    Value_Register=?variable_name; 'Register value

    end if
    info=toc(Value_Register);
    else
    if (Type==Type_Bit || Type==Type_Alarm )then
    Value_Bit=?variable_name; 'Bit value
    info=toc(Value_Bit);
    else
    if (Type==Type_Text )then
    info=?variable_name; 'Text value
    end if
    end if
    end if
    if ( variable("STATUS",variable_name,"VALID" ) ==0) then
    info="?" ;
    end if
    else 'the variable does not exist
    if ( cmpstring ( variable_name, "" )==0) then
    info=""; 'empty variable reserved one ?
    else
    info=addstring("???? unexisting variable " ,variable_name);
    print ( "???? unexisting variable " ,variable_name );
    end if
    end if
    if ( j_nbvar ) then
    info=addstring(separator ,info );
    end if
    'fputs ( Day_file,info);
    fputs ( Month_file,info);
    j_nbvar++;
    ' print ( j_nbvar," variable " ,variable_name , " " , Type );

    wend

    'fputs ( Day_file,"\n"); 'adds a rclf ( go to next line )
    fputs ( Month_file,"\n"); 'adds a rclf ( go to next line )

    fclose ( which_report);
    'fclose (Day_file);
    fclose (Month_file);

    end sub
    '-------------------------------------------------------------------------------------------------------------------------------

    sub Copy_model ( model_file, file )
    'This function will copy a buffer model file to an empty file
    fclose (file );
    if (fopen ( file, "r" )==0 ) then ' new file copy the model if exist
    buftofile ( model_file,file); 'copy the model title
    end if
    fclose (file);
    end sub

    '-------------------------------------------------------------------------------------------------------------------------------

    sub concat ( str1,str2,str3 )
    ' This function returns a string equals to str1+str2+str3
    dim ch as str;
    ch=addstring( str1,str2) ;
    ch=addstring( ch,str3) ;
    return ( ch );
    end sub

    '-------------------------------------------------------------------------------------------------------------------------------

    sub create_file ( file , content )
    fclose (file );
    if ( fopen (file, "r") ==0 ) then 'file does not exist
    fopen (file , "w"); 'create file
    fputs (file ,content );
    fclose (file );
    print ( "file " ,file ," does not exist ??? a sample file has been created " );
    end if
    end sub
    -1