* * * *

Privacy Policy

Blog italiano

Clicca qui se vuoi andare al blog italiano su Lazarus e il pascal.

Forum ufficiale

Se non siete riusciti a reperire l'informazione che cercavate nei nostri articoli o sul nostro forum vi consiglio di visitare il
Forum ufficiale di Lazarus in lingua inglese.

Lazarus 1.0

Trascinare un file nel programma
DB concetti fondamentali e ZeosLib
Recuperare codice HTML da pagina web
Mandare mail con Lazarus
Stabilire il sistema operativo
Esempio lista in pascal
File INI
Codice di attivazione
Realizzare programmi multilingua
Lavorare con le directory
Utilizzare Unità esterne
TTreeView
TTreeview e Menu
Generare controlli RUN-TIME
LazReport, PDF ed immagini
Intercettare tasti premuti
Ampliare Lazarus
Lazarus e la crittografia
System Tray con Lazarus
UIB: Unified Interbase
Il file: questo sconosciuto
Conferma di chiusura di un applicazione
Liste e puntatori
Overload di funzioni
Funzioni a parametri variabili
Proprietà
Conversione numerica
TImage su Form e Panel
Indy gestiore server FTP lato Client
PopUpMenu sotto Pulsante (TSpeedButton)
Direttiva $macro
Toolbar
Evidenziare voci TreeView
Visualizzare un file Html esterno
StatusBar - aggirare l'errore variabile duplicata
Da DataSource a Excel
Le permutazioni
Brute force
Indy 10 - Invio email con allegati
La gestione degli errori in Lazarus
Pascal Script
Linux + Zeos + Firebird
Dataset virtuale
Overload di operatori
Lavorare con file in formato JSON con Lazarus
Zeos ... dietro le quinte (prima parte)
Disporre le finestre in un blocco unico (come Delphi)
Aspetto retrò (Cmd Line)
Lazarus 1.0
Come interfacciare periferica twain
Ubuntu - aggiornare free pascal e lazarus
fpcup: installazioni parallele di lazarus e fpc
Free Pascal e Lazarus sul Raspberry Pi
Cifratura: breve guida all'uso dell'algoritmo BlowFish con lazarus e free pascal.
Creare un server multithread
guida all'installazione di fpc trunk da subversion in linux gentoo
Indice
DB concetti fondamentali e connessioni standard
Advanced Record Syntax
DB concetti fondamentali e DBGrid
DB concetti fondamentali e TDBEdit, TDBMemo e TDBText
Advanced Record Syntax: un esempio pratico
Superclasse form base per programmi gestionali (e non)
Superclasse form base per programmi gestionali (e non) #2 - log, exception call stack, application toolbox
Superclasse form base per programmi gestionali (e non) #3 - traduzione delle form
Superclasse form base per programmi gestionali (e non) #4 - wait animation
Un dialog per la connessione al database:TfmSimpleDbConnectionDialog
Installare lazarus su mac osx sierra
immagine docker per lavorare con lazarus e free pascal
TDD o Test-Driven Development
Benvenuto! Effettua l'accesso oppure registrati.
Aprile 19, 2024, 01:56:53 pm

Inserisci il nome utente, la password e la durata della sessione.

388 Visitatori, 0 Utenti

Autore Topic: DLL per salvare e richiamare file txt.  (Letto 62126 volte)

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #15 il: Gennaio 02, 2013, 01:44:09 am »
Ecco è leggermente peggiorato


Codice: [Seleziona]

project1.lpr(122,22) Hint: Parameter "PInput" not used
project1.lpr(122,30) Hint: Parameter "POutput" not used
project1.lpr(122,39) Hint: Parameter "PUser" not used
project1.lpr(129,31) Hint: Parameter "POutput" not used
project1.lpr(129,40) Hint: Parameter "PUser" not used
project1.lpr(150,23) Error: Illegal qualifier
project1.lpr(151,15) Warning: Local variable "Read" does not seem to be initialized
project1.lpr(151,19) Error: Incompatible types: got "AnsiString" expected "ShortInt"
project1.lpr(151,53) Error: Illegal qualifier
project1.lpr(151,62) Error: Illegal expression
project1.lpr(154,23) Error: Illegal qualifier
project1.lpr(155,16) Warning: Local variable "Write" does not seem to be initialized
project1.lpr(155,21) Error: Incompatible types: got "AnsiString" expected "ShortInt"
project1.lpr(155,57) Error: Illegal qualifier
project1.lpr(155,66) Error: Illegal expression
project1.lpr(158,17) Error: Incompatible types: got "AnsiString" expected "ShortInt"
project1.lpr(160,19) Error: Incompatible types: got "AnsiString" expected "ShortInt"
project1.lpr(197) Fatal: There were 10 errors compiling module, stopping

Codice: [Seleziona]


  procedure CalculateEx(PInput, POutput, PUser: PDLLParams; PStrings: PStringParams);
  var
    Read: string;
    Write: string;
  begin

    if ApriSaveDialog.Execute then
      PInput^[Read].Lines.SaveToFile(ApriSaveDialog.Filename);


    if ApriOpenDialog.Execute then
      POutput^[Write].Lines.LoadFromFile(ApriOpenDialog.Filename);


    PInput^[Read] := Read;

    POutput^[Write] := Write;

  end;
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #16 il: Gennaio 02, 2013, 01:46:29 am »
Questo è tutto il codice

Codice: [Seleziona]


library dialog;

{$mode objfpc}{$H+}

uses
  Interfaces,
  Classes,
  SysUtils,
  Windows,
  FileUtil,
  Forms,
  Controls,
  Graphics,
  Dialogs;

const
  Inputs = 3;  // quantita entrata
  Outputs = 1; // quantita uscita

  {INPUTS}// nome per numero di entrata
  I0 = 0;  // valore I0 = PInput[I0] ossia PInput[0]
  I1 = 1;  // valore I1 = PInput[I1] ossia PInput[1]
  Read = 2;
  // I3 = 3;
  // ... I99 = 99;

  {OUTPUTS}// nome per numero di uscita
  Write = 0;  // valore Q0 = POutput[Q0] ossia POutput[0]
  //Q1 = 1;  // valore Q1 = POutput[Q1] ossia POutput[1]

  // Q3 = 3;
  // ... Q99 = 99;

  {USER}// nome per numero di variabile, I valori vengono memorizzati

  U0 = 0; // valore U0 = PUser[U0] ossia PUser[0]
  // U1 = 1;
  // U2 = 2;
  // U3 = 3;
  // ... U99 = 99;

  // I0,I1,I2,I3,Q0,Q1,Q2,Q3,U0,U1,U2,U3
  // I nomi possono essere qualsiasi, sono case-insensitive
var
  globalDialog: TFileDialog;

type

  TDLLParams = array[0..100] of extended; //Type of ProfiLab DLL parameters
  PDLLParams = ^TDLLParams;               // Pointer to ProfiLab DLL parameters

  TStringParams = array[0..100] of PChar;   //Type of ProfiLab DLL parameters
  PStringParams = ^TStringParams;        // Pointer to ProfiLab DLL parameters


  function ApriSaveDialog: ShortString;

  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TSaveDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function ApriOpenDialog: ShortString;
  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TOpenDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function NumInputs: byte;
  begin
    Result := Inputs; // trasferire quantita entrata
  end;

  function NumOutputs: byte;
  begin
    Result := Outputs; // trasferire quantita uscita
  end;

  function InputName(Channel: byte): ShortString; // trasferire nome di entrata
  begin
    case Channel of
      I0: Result := 'I0'; // nome di pin I0
      I1: Result := 'I1'; // nome di pin I1
      Read: Result := '$read';
    end;
  end;

  function OutputName(Channel: byte): ShortString; // trasferire nome di uscita
  begin
    case Channel of
      Write: Result := '$write'; // nome di pin Q0
      //Q1: Result := 'Q1'; // nome di pin Q1
    end;
  end;


  procedure SimStart(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo al primo avvio
  begin

  end;


  procedure Calculate(PInput, POutput, PUser: PDLLParams); // Routine è permanente

  begin

    if PInput^[I0] > 2.5 then
      ApriOpenDialog;

    if PInput^[I1] > 2.5 then
      ApriSaveDialog;

  end;




  procedure CalculateEx(PInput, POutput, PUser: PDLLParams; PStrings: PStringParams);
  var
    Read: string;
    Write: string;
  begin

    if ApriSaveDialog.Execute then
      PInput^[Read].Lines.SaveToFile(ApriSaveDialog.Filename);


    if ApriOpenDialog.Execute then
      POutput^[Write].Lines.LoadFromFile(ApriOpenDialog.Filename);


    PInput^[Read] := Read;

    POutput^[Write] := Write;

  end;




  procedure SimStop(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo in fase di chiusura
  begin

  end;


  //export methods for ProfiLab
exports
  SimStart,
  SimStop,
  NumInputs,
  NumOutputs,
  Calculate,
  InputName,
  OutputName,
  ApriOpenDialog,
  ApriSaveDialog;
begin
end.

               


A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #17 il: Gennaio 02, 2013, 01:53:20 am »
Simon ... dai ammettilo... scrivi codice a caso per prenderci in giro ...
Prima dichiari le costanti come numeri, adesso come variabili stringa ;)
Per poi usarli come numeri :D
Codice: [Seleziona]
var
    Read: string;
    Write: string;
  begin

    if ApriSaveDialog.Execute then
      PInput^[Read].Lines.SaveToFile(ApriSaveDialog.Filename);
Te lo dice anche il compilatore ;)
Codice: [Seleziona]
project1.lpr(151,15) Warning: Local variable "Read" does not seem to be initialized
project1.lpr(151,19) Error: Incompatible types: got "AnsiString" expected "ShortInt"
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #18 il: Gennaio 02, 2013, 01:59:34 am »
Si ma ho bisogno di stringhe non numeri, vuoi dire che devo convertire a monte le costanti????
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #19 il: Gennaio 02, 2013, 02:04:10 am »
No, Simon non hai affatto bisogno di stringe ...
hai bisogno di numeri.
Stai andando ad usarli come indici di un array.
Gli array vogliono la posizione ...
Metti
Read :=0;
Write := 0;

Codice: [Seleziona]
    PInput^[Read] := Read;
    POutput^[Write] := Write;

Poi se vuoi fare altro ... questo non lo so, ma dal tuo codice ... ti servono "Word".
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

Legolas

  • Global Moderator
  • Sr. Member
  • *****
  • Post: 366
  • Karma: +3/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #20 il: Gennaio 02, 2013, 10:20:29 am »
Poi ci sono almeno altri due errori:

ApriOpenDialog.filename
ApriSaveDialog.filename

Per fare quello che stai facendo dovresti modificare le funzioni per far restituire TFileDialog.

Ancora: PInput e POutput non hanno la proprietà Lines. Sono dei semplici array, che stai utilizzando come se fossero dei TMemo

Come ti ha suggerito Stilgar nell'altro thread, se non vuoi impazzire e procedere a tentativi ti conviene mettere mano a un tutorial di base ("Essential Pascal" e "Essential Delphi" di Marco Cantù potrebbero fare al caso tuo)

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #21 il: Gennaio 02, 2013, 04:13:41 pm »
Salve a tutti,  ma se devo acquisire stringhe e numeri dall'ingresso read e poi salvarle con savedialog non ho bisogno di variabili:string?
I puntatori cosi configurati non dovrebbero darmi problemi o sbaglio?
Codice: [Seleziona]

type

  TDLLParams = array[0..100] of extended; //Type of ProfiLab DLL parameters
  PDLLParams = ^TDLLParams;               // Pointer to ProfiLab DLL parameters

  TStringParams = array[0..100] of PChar;   //Type of ProfiLab DLL parameters
  PStringParams = ^TStringParams;        // Pointer to ProfiLab DLL parameters

Voglio usare read per prendere il dato dall'utente ovvero caselle nere e acquisire con le caselle grigie i dati salvati con savedialog.
« Ultima modifica: Gennaio 02, 2013, 04:15:19 pm da Simon75 »
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

nomorelogic

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2870
  • Karma: +20/-4
Re:DLL per salvare e richiamare file txt.
« Risposta #22 il: Gennaio 02, 2013, 04:59:47 pm »
per salvare un array in una lista di stringhe dovresti fare qualcosa del genere:
Codice: [Seleziona]
var i:integer;
sl: TStringList;
begin
  sl := TStringList.Create;
  try
    for i := low(PDLLParams^) to high(PDLLParams^) do
        sl.Values[ inttostr(i) ] := FloatToStr( PDLLParams^[i] );

    sl.SaveToFile(...);
  finally
    sl.Free;
  end;
end;

tieni conto che l'ho scritto di getto e non ho provato neanche a compilare, si tratta di darti un'idea

il fatto simon è che dal codice che scrivi appare chiaro che non hai acquisito le basi del pascal: usare una stringa come indice di un array o sperare che un array di extended abbia una proprietà che espone una istanza di TStrings vuol dire che probabilmente non è il momento di iniziare a scrivere codice complesso come quello che vuoi fare tu.
dovresti seguire il consiglio di legolas e stilgar e cioè apprendere le basi del pascal, IMHO le letture che ti ha consigilato legolas vanno più che bene ;)
Imagination is more important than knowledge (A.Einstein)

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #23 il: Gennaio 02, 2013, 05:28:13 pm »
Citazione
dovresti seguire il consiglio di legolas e stilgar

ASSOLUTAMENTE NO non seguo i consigli di Legols e Stilgar  poiché mi sono veramente antipatici non aiutano mai!!!!  scrivono poco codice e ti fanno sudare ;) ;) ;) ;) ;) ;D ;D ;D ;D

A parte gli scherzi, lo so che non ho le basi e sicuramente me le farò, ma ho fretta di fare questa benedetta dll poiché mi serve per terminare un progetto che ho fatto.
Quindi in qualche modo devo farla!!!
Mi sono già armato di libri tra un vostro aiuto e qualche lettura spero di tirare fuori qualcosa.
Vi saluto :)
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #24 il: Gennaio 02, 2013, 09:45:48 pm »
Allora il problema sonoo solo queste righe, ho provato a commentarle e tutto ha funzionato ho tolto il commento e mi ha generato l'errore.
Se ruscite a darmi una mano su queste righe sono a cavallo ;)
Vi ringrazio

Codice: [Seleziona]

 if ApriSaveDialog.Execute then
    PInput^[Read].Lines.SaveToFile(ApriSaveDialog.Filename);


    if ApriOpenDialog.Execute then
    POutput^[Write].Lines.LoadFromFile(ApriOpenDialog.Filename);   

Inserisco tutto il codice:

Codice: [Seleziona]


library dialog;

{$mode objfpc}{$H+}

uses
  Interfaces,
  Classes,
  SysUtils,
  Windows,
  FileUtil,
  Forms,
  Controls,
  Graphics,
  Dialogs;

const
  Inputs = 3;  // quantita entrata
  Outputs = 1; // quantita uscita

  {INPUTS}// nome per numero di entrata
  I0 = 0;  // valore I0 = PInput[I0] ossia PInput[0]
  I1 = 1;  // valore I1 = PInput[I1] ossia PInput[1]
  Read = 2;
  // I3 = 3;
  // ... I99 = 99;

  {OUTPUTS}// nome per numero di uscita
  Write = 0;  // valore Q0 = POutput[Q0] ossia POutput[0]
  //Q1 = 1;  // valore Q1 = POutput[Q1] ossia POutput[1]

  // Q3 = 3;
  // ... Q99 = 99;

  {USER}// nome per numero di variabile, I valori vengono memorizzati

  U0 = 0; // valore U0 = PUser[U0] ossia PUser[0]
  // U1 = 1;
  // U2 = 2;
  // U3 = 3;
  // ... U99 = 99;

  // I0,I1,I2,I3,Q0,Q1,Q2,Q3,U0,U1,U2,U3
  // I nomi possono essere qualsiasi, sono case-insensitive
var
  globalDialog: TFileDialog;

type

  TDLLParams = array[0..100] of extended; //Type of ProfiLab DLL parameters
  PDLLParams = ^TDLLParams;               // Pointer to ProfiLab DLL parameters

  TStringParams = array[0..100] of PChar;   //Type of ProfiLab DLL parameters
  PStringParams = ^TStringParams;        // Pointer to ProfiLab DLL parameters


  function ApriSaveDialog: ShortString;

  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TSaveDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function ApriOpenDialog: ShortString;
  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TOpenDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function NumInputs: byte;
  begin
    Result := Inputs; // trasferire quantita entrata
  end;

  function NumOutputs: byte;
  begin
    Result := Outputs; // trasferire quantita uscita
  end;

  function InputName(Channel: byte): ShortString; // trasferire nome di entrata
  begin
    case Channel of
      I0: Result := 'I0'; // nome di pin I0
      I1: Result := 'I1'; // nome di pin I1
      Read: Result := '$read';
    end;
  end;

  function OutputName(Channel: byte): ShortString; // trasferire nome di uscita
  begin
    case Channel of
      Write: Result := '$write'; // nome di pin Q0
      //Q1: Result := 'Q1'; // nome di pin Q1
    end;
  end;


  procedure SimStart(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo al primo avvio
  begin

  end;


  Procedure CalculateEx(PInput,POutput,PUser: PDLLParams; PStrings: PStringParams); // Routine è permanente

  begin


    if PInput^[I0] > 2.5 then
      ApriOpenDialog;

    if PInput^[I1] > 2.5 then
      ApriSaveDialog;




    PInput^[Read] := Read;
    POutput^[Write] := Write ;




    if ApriSaveDialog.Execute then
    PInput^[Read];Lines.SaveToFile(ApriSaveDialog.Filename);


    if ApriOpenDialog.Execute then
    POutput^[Write];Lines.LoadFromFile(ApriOpenDialog.Filename);

  end;




  procedure SimStop(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo in fase di chiusura
  begin

  end;


  //export methods for ProfiLab
exports
  SimStart,
  SimStop,
  NumInputs,
  NumOutputs,
  CalculateEx,
  InputName,
  OutputName,
  ApriOpenDialog,
  ApriSaveDialog;
begin
end.

         
« Ultima modifica: Gennaio 02, 2013, 10:15:12 pm da Simon75 »
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

nomorelogic

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2870
  • Karma: +20/-4
Re:DLL per salvare e richiamare file txt.
« Risposta #25 il: Gennaio 02, 2013, 11:22:21 pm »
farei così:

da qualche parte dichiara:
Codice: [Seleziona]
procedure SalvaArrayInFile(FileName: string);
var i:integer;
sl: TStringList;
begin
  sl := TStringList.Create;
  try
    for i := low(PDLLParams^) to high(PDLLParams^) do
        sl.Values[ inttostr(i) ] := FloatToStr( PDLLParams^[i] );

    sl.SaveToFile(FileName);
  finally
    sl.Free;
  end;
end;



poi:
if ApriSaveDialog.Execute then
    PInput^[Read];Lines.SaveToFile(ApriSaveDialog.Filename);

dovrebbe diventare:
Codice: [Seleziona]
if ApriSaveDialog.Execute then
    SalvaArrayInFile(ApriSaveDialog.Filename);

poi dovresti fare qualcosa di simile per il load
Imagination is more important than knowledge (A.Einstein)

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #26 il: Gennaio 02, 2013, 11:40:12 pm »
Ciao nomorelogic
ecco gli errori che mi da adesso

Codice: [Seleziona]

project1.lpr(122,22) Hint: Parameter "PInput" not used
project1.lpr(122,30) Hint: Parameter "POutput" not used
project1.lpr(122,39) Hint: Parameter "PUser" not used
project1.lpr(136,46) Error: type identifier not allowed here
project1.lpr(166,23) Error: Illegal qualifier
project1.lpr(167,39) Error: Illegal qualifier
project1.lpr(222) Fatal: There were 3 errors compiling module, stopping


Le linea che mi da errore è questa:

Codice: [Seleziona]

sl.Values[IntToStr(i)] := FloatToStr(PDLLParams^[i]);

Poi queste:

Codice: [Seleziona]

    if ApriSaveDialog.Execute then
      SalvaArrayInFile(ApriSaveDialog.Filename)
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

nomorelogic

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2870
  • Karma: +20/-4
Re:DLL per salvare e richiamare file txt.
« Risposta #27 il: Gennaio 04, 2013, 03:11:16 pm »
questa viene compilata e puoi testarla

Codice: [Seleziona]
library lib01;

{$mode objfpc}{$H+}

uses
  Interfaces,
  Classes,
  SysUtils,
  Windows,
  FileUtil,
  Forms,
  Controls,
  Graphics,
  Dialogs;

const
  Inputs = 3;  // quantita entrata
  Outputs = 1; // quantita uscita

  {INPUTS}// nome per numero di entrata
  I0 = 0;  // valore I0 = PInput[I0] ossia PInput[0]
  I1 = 1;  // valore I1 = PInput[I1] ossia PInput[1]
  Read = 2;
  // I3 = 3;
  // ... I99 = 99;

  {OUTPUTS}// nome per numero di uscita
  Write = 0;  // valore Q0 = POutput[Q0] ossia POutput[0]
  //Q1 = 1;  // valore Q1 = POutput[Q1] ossia POutput[1]

  // Q3 = 3;
  // ... Q99 = 99;

  {USER}// nome per numero di variabile, I valori vengono memorizzati

  U0 = 0; // valore U0 = PUser[U0] ossia PUser[0]
  // U1 = 1;
  // U2 = 2;
  // U3 = 3;
  // ... U99 = 99;

  // I0,I1,I2,I3,Q0,Q1,Q2,Q3,U0,U1,U2,U3
  // I nomi possono essere qualsiasi, sono case-insensitive
var
  globalDialog: TFileDialog;

type

  TDLLParams = array[0..100] of extended; //Type of ProfiLab DLL parameters
  PDLLParams = ^TDLLParams;               // Pointer to ProfiLab DLL parameters

  TStringParams = array[0..100] of PChar;   //Type of ProfiLab DLL parameters
  PStringParams = ^TStringParams;        // Pointer to ProfiLab DLL parameters


  function ApriSaveDialog: ShortString;

  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TSaveDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function ApriOpenDialog: ShortString;
  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TOpenDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function NumInputs: byte;
  begin
    Result := Inputs; // trasferire quantita entrata
  end;

  function NumOutputs: byte;
  begin
    Result := Outputs; // trasferire quantita uscita
  end;

  function InputName(Channel: byte): ShortString; // trasferire nome di entrata
  begin
    case Channel of
      I0: Result := 'I0'; // nome di pin I0
      I1: Result := 'I1'; // nome di pin I1
      Read: Result := '$read';
    end;
  end;

  function OutputName(Channel: byte): ShortString; // trasferire nome di uscita
  begin
    case Channel of
      Write: Result := '$write'; // nome di pin Q0
      //Q1: Result := 'Q1'; // nome di pin Q1
    end;
  end;


  procedure SimStart(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo al primo avvio
  begin

  end;


  procedure SalvaArrayInFile(FileName: string; PAParams: PDLLParams);
  var i:integer;
      sl: TStringList;
  begin
    sl := TStringList.Create;
    try
      for i := low(PAParams^) to high(PAParams^) do
          sl.Values[ inttostr(i) ] := FloatToStr( PAParams^[i] );

      sl.SaveToFile(FileName);
    finally
      sl.Free;
    end;
  end;

  procedure CaricaArrayDaFile(FileName: string; PAParams: PDLLParams);
  var i:integer;
      sl: TStringList;
      e: extended;
      s: string;
  begin
    sl := TStringList.Create;
    try
      sl.LoadFromFile(FileName);
      for i := low(PAParams^) to high(PAParams^) do
      begin
         // piuttosto che scrivere:
         // PAParams^[i] := StrToFloat(sl.Values[ inttostr(i) ]);
         // scrivendo come sotto è più facile fare il debug
         // quando l'hai testata puoi usare la forma commentata qui sopra
         s :=  sl.Values[ inttostr(i) ];
         e := StrToFloat(s);
         PAParams^[i] := e
      end;
    finally
      sl.Free;
    end;
  end;


  Procedure CalculateEx(PInput,POutput,PUser: PDLLParams; PStrings: PStringParams); // Routine è permanente
  var s: shortstring;
  begin

    if PInput^[I0] > 2.5 then
      ApriOpenDialog;

    if PInput^[I1] > 2.5 then
      ApriSaveDialog;

    PInput^[Read] := Read;
    POutput^[Write] := Write ;

    s := ApriSaveDialog;
    if s <> '' then
       SalvaArrayInFile(s, POutput);

    s := ApriOpenDialog;
    if s <> '' then
       CaricaArrayDaFile(s, POutput);

  end;




  procedure SimStop(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo in fase di chiusura
  begin

  end;


  //export methods for ProfiLab
exports
  SimStart,
  SimStop,
  NumInputs,
  NumOutputs,
  CalculateEx,
  InputName,
  OutputName,
  ApriOpenDialog,
  ApriSaveDialog;
begin
end.

Imagination is more important than knowledge (A.Einstein)

Simon75

  • Full Member
  • ***
  • Post: 139
  • Karma: +0/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #28 il: Gennaio 04, 2013, 10:27:31 pm »
Ciao nomore ti ringrazio molto, attualmente appena provo a mandare in esecuzione il programma(Profilab) le OpenDialog e SaveDialog si aprono da sole senza premere i pulsanti.

Ho aggiunto 2 variabili che mi serviranno per leggere e scrivere ma generano questo errore:

Codice: [Seleziona]

procedure CalculateEx(PInput, POutput, PUser: PDLLParams; PStrings: PStringParams);
  // Routine è permanente
  var
    s: shortstring;
     lettura:string;          // ho aggiunto queste variabili poichè mi serve leggere
     scrittura:string;         // ho aggiunto queste variabili poichè mi serve srivere
  begin

    if PInput^[I0] > 2.5 then
      ApriOpenDialog;

    if PInput^[I1] > 2.5 then
      ApriSaveDialog;

    PInput^[Leggi] := read(lettura);       //le due variabili le ho messe qui
    POutput^[Stampa] := write(scrittura);  //le due variabili le ho messe qui

    s := ApriSaveDialog;
    if s <> '' then
      SalvaArrayInFile(s, POutput);

    s := ApriOpenDialog;
    if s <> '' then
      CaricaArrayDaFile(s, POutput);

  end;                                                   


Codice: [Seleziona]

project1.lpr(122,22) Hint: Parameter "PInput" not used
project1.lpr(122,30) Hint: Parameter "POutput" not used
project1.lpr(122,39) Hint: Parameter "PUser" not used
project1.lpr(182,36) Error: Incompatible types: got "untyped" expected "Extended"
project1.lpr(183,40) Warning: Local variable "scrittura" does not seem to be initialized
project1.lpr(183,41) Error: Incompatible types: got "untyped" expected "Extended"
project1.lpr(218) Fatal: There were 2 errors compiling module, stopping



Ecco la dll completa:
Codice: [Seleziona]


library lib01;

{$mode objfpc}{$H+}

uses
  Interfaces,
  Classes,
  SysUtils,
  Windows,
  FileUtil,
  Forms,
  Controls,
  Graphics,
  Dialogs;

const
  Inputs = 3;  // quantita entrata
  Outputs = 1; // quantita uscita

  {INPUTS}// nome per numero di entrata
  I0 = 0;  // valore I0 = PInput[I0] ossia PInput[0]
  I1 = 1;  // valore I1 = PInput[I1] ossia PInput[1]
  Leggi = 2;
  // I3 = 3;
  // ... I99 = 99;

  {OUTPUTS}// nome per numero di uscita
  Stampa = 0;  // valore Q0 = POutput[Q0] ossia POutput[0]
  //Q1 = 1;  // valore Q1 = POutput[Q1] ossia POutput[1]

  // Q3 = 3;
  // ... Q99 = 99;

  {USER}// nome per numero di variabile, I valori vengono memorizzati

  U0 = 0; // valore U0 = PUser[U0] ossia PUser[0]
  // U1 = 1;
  // U2 = 2;
  // U3 = 3;
  // ... U99 = 99;

  // I0,I1,I2,I3,Q0,Q1,Q2,Q3,U0,U1,U2,U3
  // I nomi possono essere qualsiasi, sono case-insensitive
var
  globalDialog: TFileDialog;

type

  TDLLParams = array[0..100] of extended; //Type of ProfiLab DLL parameters
  PDLLParams = ^TDLLParams;               // Pointer to ProfiLab DLL parameters

  TStringParams = array[0..100] of PChar;   //Type of ProfiLab DLL parameters
  PStringParams = ^TStringParams;        // Pointer to ProfiLab DLL parameters


  function ApriSaveDialog: ShortString;

  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TSaveDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function ApriOpenDialog: ShortString;
  begin
    Result := '';
    if not assigned(globalDialog) then
    begin
      globalDialog := TOpenDialog.Create(nil);
      try
        globalDialog.Filter := '*.txt';
        if globalDialog.Execute then
          Result := globalDialog.FileName
        else
          Result := '';
      finally
        FreeAndNil(globalDialog);
      end;
    end;
  end;

  function NumInputs: byte;
  begin
    Result := Inputs; // trasferire quantita entrata
  end;

  function NumOutputs: byte;
  begin
    Result := Outputs; // trasferire quantita uscita
  end;

  function InputName(Channel: byte): ShortString; // trasferire nome di entrata
  begin
    case Channel of
      I0: Result := 'I0'; // nome di pin I0
      I1: Result := 'I1'; // nome di pin I1
      Leggi: Result := '$Leggi';
    end;
  end;

  function OutputName(Channel: byte): ShortString; // trasferire nome di uscita
  begin
    case Channel of
      Stampa: Result := '$Stampa'; // nome di pin Q0
      //Q1: Result := 'Q1'; // nome di pin Q1
    end;
  end;


  procedure SimStart(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo al primo avvio
  begin

  end;


  procedure SalvaArrayInFile(FileName: string; PAParams: PDLLParams);
  var
    i: integer;
    sl: TStringList;
  begin
    sl := TStringList.Create;
    try
      for i := low(PAParams^) to high(PAParams^) do
        sl.Values[IntToStr(i)] := FloatToStr(PAParams^[i]);

      sl.SaveToFile(FileName);
    finally
      sl.Free;
    end;
  end;

  procedure CaricaArrayDaFile(FileName: string; PAParams: PDLLParams);
  var
    i: integer;
    sl: TStringList;
    e: extended;
    s: string;
  begin
    sl := TStringList.Create;
    try
      sl.LoadFromFile(FileName);
      for i := low(PAParams^) to high(PAParams^) do
      begin
        // piuttosto che scrivere:
        // PAParams^[i] := StrToFloat(sl.Values[ inttostr(i) ]);
        // scrivendo come sotto è più facile fare il debug
        // quando l'hai testata puoi usare la forma commentata qui sopra
        s := sl.Values[IntToStr(i)];
        e := StrToFloat(s);
        PAParams^[i] := e;
      end;
    finally
      sl.Free;
    end;
  end;


  procedure CalculateEx(PInput, POutput, PUser: PDLLParams; PStrings: PStringParams);
  // Routine è permanente
  var
    s: shortstring;
     lettura:string;          // ho aggiunto queste variabili poichè mi serve leggere
     scrittura:string;         // ho aggiunto queste variabili poichè mi serve srivere
  begin

    if PInput^[I0] > 2.5 then
      ApriOpenDialog;

    if PInput^[I1] > 2.5 then
      ApriSaveDialog;

    PInput^[Leggi] := read(lettura);       //le due variabili le ho messe qui
    POutput^[Stampa] := write(scrittura);  //le due variabili le ho messe qui

    s := ApriSaveDialog;
    if s <> '' then
      SalvaArrayInFile(s, POutput);

    s := ApriOpenDialog;
    if s <> '' then
      CaricaArrayDaFile(s, POutput);

  end;




  procedure SimStop(PInput, POutput, PUser: PDLLParams);
  // Routine viene eseguita solo in fase di chiusura
  begin

  end;


  //export methods for ProfiLab
exports
  SimStart,
  SimStop,
  NumInputs,
  NumOutputs,
  CalculateEx,
  InputName,
  OutputName,
  ApriOpenDialog,
  ApriSaveDialog;
begin
end.       

Ringrazio per l'attenzione
« Ultima modifica: Gennaio 05, 2013, 12:18:57 am da Simon75 »
A volte bisogna commettere errori per capire qual è la cosa giusta da fare...

Const
Errori=Esperienza

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:DLL per salvare e richiamare file txt.
« Risposta #29 il: Gennaio 05, 2013, 12:41:36 pm »
Scusa, ma read e write sono quelle di sistema o le hai riscritte da qualche parte?
Non può andare in questo modo.
Se leggi un valore con read, essendo una procedura, cosa vuoi ottenere?
Che venga impostato il valore nelle varie celle dell'array?
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

 

Recenti

How To

Utenti
  • Utenti in totale: 785
  • Latest: gmax
Stats
  • Post in totale: 18772
  • Topic in totale: 2233
  • Online Today: 578
  • Online Ever: 900
  • (Gennaio 21, 2020, 08:17:49 pm)
Utenti Online
Users: 0
Guests: 388
Total: 388

Disclaimer:

Questo blog non rappresenta una testata giornalistica poiché viene aggiornato senza alcuna periodicità. Non può pertanto considerarsi un prodotto editoriale ai sensi della legge n. 62/2001.