Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [Delphi] Creazione dll

  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    16

    Aiuto con Pascal

    Salve a tutti qualcuno sa darmi una spigazione di questo erempio di dll?
    Questo esempio serve per creare una dll che poi verrà caricata su un software di automatismi vorrei capirci qualcosa e poi modificare tale sorgente.



    Example DLL source (Delphi)

    The compiled DLL file can be found in the EXAMPLES directory (COUNTER.DLL).
    codice:
      
    library Counter;
      
    // Delphi 5 DLL-source (COUNTER.DLL)
    // Defines a simple 8-Bit counter component for ProfiLab
      
    uses
      SysUtils,Windows,Classes;
      
    {$R *.RES}
      
    Const  Inputs = 2;  // number of inputs
           Outputs = 8; // number of outputs
      
           {INPUTS}
           CLK   = 0;   // index of input variable CLK
           RST   = 1;   // index of input variable RST
      
           {OUTPUTS}
      
           {USER}
           CLK_old = 0; // index user variable CLK_old
           RST_old = 1; // index user variable RST_old
           Count = 2;   // index user variable COUNT
      
    Type TDLLParams = array[0..100] of extended;   //Type of ProfiLab DLL parameters
         PDLLParams = ^TDLLParams;                 // Pointer to ProfiLab DLL parameters
      
    function NumInputs: Byte;
    begin
      result:=Inputs; //Define number of component input pins
    end;
      
    function NumOutputs: Byte;
    begin
      result:=Outputs; //Define number of component output pins
    end;
      
    Function InputName(Channel: Byte): ShortString; // Return name for each component input pin
    begin
       case Channel of
       CLK: result:='CLK';   //  "CLK" (Clock)
       RST: result:='/RST';  //  "/RST" (NOT RESET)
       end;
    end;
      
    Function OutputName(Channel: Byte): ShortString; // Return name for each component output pin
    begin
       result:='Q'+intToStr(Channel); //"Q0".."Q7" (Binary count)
    end;
      
    Procedure SimStart(PInput,POutput,PUser: PDLLParams); //called when ProfiLab enters RUN mode
    var i: Integer;
    begin
       PUser^[Count]:=0; //RESET COUNTER
       For i:=0 to Outputs do
       begin
           POutput[i]:=0; //Set binary outputs with COUNT=0
       end;
    end;
      
    Procedure SimStop(PInput,POutput,PUser: PDLLParams); //called when ProfiLab RUN mode is terminated
    begin
      // nothing to be done
    end;
      
    Procedure Calculate(PInput,POutput,PUser: PDLLParams); //called regularly from ProfiLab
    var i: Integer;
    begin
       if PInput^[RST]<2.5 then //check RST input HIGH or LOW
       begin
          if (not (PInput^[RST]>=2.5)) and (PUser^[RST_old]>2.5) then //check out falling edge at RST input
          begin
             PUser^[Count]:=0; //RESET COUNT
             For i:=0 to Outputs do
             begin
                 POutput[i]:=0; //Set binary outputs with COUNT=0
             end;
          end;
          exit;
       end;
       PUser^[RST_old]:=PInput^[RST]; //Remember RST status for next call
      
       if PInput^[CLK]>2.5 then //check CLK input HIGH or LOW
       begin
          if (PInput^[CLK]>2.5) and not(PUser^[CLK_old]>2.5) then //check out rising edge at CLK input
          begin
             PUser^[Count]:=PUser^[Count]+1; // increase COUNT
             if PUser^[Count]>255 then PUser^[Count]:=0; //check overflow
             For i:=0 to Outputs do
             begin
                if (round(PUser^[Count]) and (1 shl i))>0 then POutput^[i]:=5 else POutput[i]:=0; //Set binary outputs with current COUNT
             end;
          end;
       end;
       PUser^[CLK_old]:=PInput^[CLK]; //Remember CLK status for next call
    end;
      
      
    //export methods for ProfiLab
    exports SimStart,
            SimStop,
            NumInputs,
            NumOutputs,
            Calculate,
            InputName,
            OutputName;
      
    begin
    end.

  2. #2

    Moderazione

    Ho sistemato il titolo (troppo vago e senza tag del linguaggio di riferimento) e ho aggiunto i tag [CODE] (altrimenti il codice perde l'indentazione); a tal proposito, dai un'occhiata al regolamento prima di proseguire.

    In ogni caso, dovresti chiarire esattamente quale sia la domanda/cosa non ti è chiaro del codice in questione, in modo che ti si possa dare un aiuto mirato...
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    16
    Cerco di farmi capire: Attualmete sto usando un software di automatismi (Profilabexpert della Abacom), questo software all'interno ha delle sue librerie già pronte dove posso prendere il mio oggetto e collegarlo a mio piacere. http://abacom-online.de/html/profilab.html
    Purtoppo in questo software manca una libreria per scrivere file txt su disco ma in compenso uno può creare una sua DLL creando così l'oggetto che vuole.
    Ora per creare tale DLL bisogna seguire queste regole:




    DLL-Import

    Included in version:
    DMM-ProfiLab: No
    Digital-ProfiLab: No
    ProfiLab-Expert: Yes

    This component offers a programming interface, which makes it possible to create your own ProfiLab components, for example to control self-made hardware devices, etc.
    For that purpose you need a programming language, that allows you to compile DLL-files
    (Dynamic Link Libraries), and you need some programming experience, as well.
    Programming your own component, you have to meet some requirements of ProfiLab.
    For example your DLL must export certain functions, that define the numbers of component inputs and outputs, the pin names and the internal function of the component.

    The following function are necessary for a component. The modified C++ versions use DOUBLE instead of EXTENDED and PCHAR instead of STRING.

    Delphi: function NumInputs: Byte;
    C++: unsigned char _stdcall NumInputs()
    alternative:
    Delphi: function NumInputsEx(PUser: PDLLParams): Byte;
    C++: unsigned char _stdcall CNumInputsEx(double *PUser)
    The result of this function must return a byte value that defines the number of inputs of your component. The extended function type NumInputsEx is useful, if the number of inputs depends on configuration data stored in PUser.

    Delphi: function NumOutputs: Byte;
    C++: unsigned char _stdcall NumOutputs()
    alternative:
    Delphi: function NumOutputsEx(PUser: PDLLParams): Byte;
    C++: unsigned char _stdcall CNumOutputsEx(double *PUser)
    The result of this function must return a byte value that defines the number of outputs of your component. The extended function type NumOutputsEx is useful, if the number of outputs depends on configuration data stored in PUser.

    Delphi: function InputName(Channel: Byte): ShortString;
    void _stdcall GetInputName(unsigned char Channel, unsigned char *Name)
    The result of this function must deliver a short text for the pin description for each input pin (channel) of your component. ProfiLab calls this function for each input pin, to request the corresponding description. The parameter CHANNELS identifies the pin and runs from 0 to NumInputs-1.

    Delphi: function OutputName(Channel: Byte): ShortString;
    C++: void _stdcall GetOutputName(unsigned char Channel, unsigned char *Name)
    The result of this function must deliver a short text for the pin description for each output pin (channel) of your component. ProfiLab calls this function for each output pin, to request the corresponding description. The parameter CHANNELS identifies the pin and runs from 0 to NumOutputs-1.

    Delphi: Procedure Calculate(PInput,POutput,PUser: PDLLParams);
    C++: void _stdcall CCalculate(double *PInput, double *POutput, double *PUser)
    This is the main calculation procedure of your component, which defines how your component works. The procedure parameters PINPUT, POUTPUT and PUSER offer three pointer variables with the following function:

    The pointer PINPUT points to a memory area, in which input values are stored, so that the DLL can access the input values of the component.
    The pointer POUTPUT points to a memory area, in which output values are stored, so that the DLL can set the output values of the component.
    The pointer PUSER points to a memory area, where the DLL can store its own (local) values. Background: Variables defined in the DLL are global variables. Values stored to global variables will overwrite each other, if a DLL component is used more than once in a ProfiLab project. To have local variables available, ProfiLab hands out the pointer PUSER to the DLL, so that the DLL can store local data in the memory area that PUSER points to.

    If you don´t want to use PUSER, but you need to declare variables in the DLL that are meant to be local (for components that are used more than once in a ProfiLab project), you can rename the DLL file and import it in ProfiLab with different filenames, as well.

    Each of the three pointers PINPUT, POUTPUT and PUSER points to an array of 100 EXTENDED variables. All three pointer are declared as type PDLLParams. The declaration in Delphis is as follows:

    type TDLLParams = array[0..100] of extended;
    PDLLParams = ^TDLLParams;

    C++ function types hand out this kind of memory pointer as (double *PInput) parameter for example.

    The array of PINPUT offers the input values of the component. The input values can be accessed as follows:

    PInput^[0] contains the numeric value of the first input,
    PInput^[1] contains the numeric value of the second input, and so on...

    The array of POUTPUT offers the output values of the component. The output values can be set as follows:

    POutput^[0] must be set with the numeric value for the first output,
    POutput^[1] must be set with the numeric value for the second output, and so on...

    PUser^[0] to PUser^[99] can be used to store numeric user values. The values of these variables are saved in the ProfiLab project file, so that values are available agein, when the project is loaded next time. The variable PUser^[100] is set by ProfiLab and contains the number of the DLL component: 1 fo DLL1, 2 for DLL2, and so on.

    The procedure CALCULATE is called repeatedly while ProfiLab is in RUN mode, to hand out new input values to the DLL and to request new output values from the DLL. This means that this procedure must be programmed as short as possible, and must not contain any pauses (WAIT loops or SLEEP commands) that waste time. After reading input values and setting new output vaues this routine should be terminated as soon as possible. The time spent in this procedure will directly influence the simulation frequency of ProfiLab.

    Delphi: Procedure CalculateEx(PInput,POutput,PUser: PDLLParams; PStrings: PStringParams);
    C++: void _stdcall CCalculateEx(double *PInput, double *POutput, double *PUser; StringParam PStrings)
    This method was introduced to allow string processing with DLL´s. It may be used as alternative for CALCULATE. Parameter PSTRINGS were added for interfacing string data. Its Delphi delclaration is as follows:

    type TStringParams = array[0..100] of PChar;
    PStringParams = ^TStringParams;

    Each input/output (max. 100) has a null-terminated character pointer (PChar) assigned, which points to a memory space that is provided by ProfiLab. Right before ProfiLab enters the method, data is fetched from the $inputs. After leaving the method data is handed out through the $outputs. It is not distinguished between inputs and outputs. This means that Input 0 and Output 0 for example share the same PChar. To make a pin become a string input or string output its pin name must be declared with a leading '$' character. Examples for string processing with DLL´s and ProfiLab are available.

    Delphi: Procedure SimStart(PInput,POutput,PUser: PDLLParams);
    C++: void _stdcall CSimStart(double *PInput, double *POutput, double *PUser)
    This procedure is called once, when the ProfiLab project enters the RUN mode, and can be used to initialize DLL variables, etc. The parameters have been explained before.

    Delphi: Procedure SimStop(PInput,POutput,PUser: PDLLParams);
    C++: void _stdcall CSimStop(double *PInput, double *POutput, double *PUser)
    This procedure is called once, when RUN mode is terminated, and can be used to close open files, etc. The parameters have been explained before.

    Delphi: Procedure Configure(UserValues: PDLLParam);
    C++: void _stdcall CConfigure(double *PUser)
    As soon as your DLL exports this procedure, the button CONFIGURE... in the property dialogue of the component is enabled. With a click on this button, ProfiLab will jump to your CONFIGURE procedure, where you can add your own setup dialogue for your DLL.

    These very few routines make it possible to program any ProfiLab component you have in mind. For example you could program hardware components that control special hardware devices, or create components that execute complex calculations.
    If you want to program a component with digital outputs, simply set the numeric output vaules to 5 for HIGH levels, or to 0 for LOW levels. Numeric inputs higher than 2.5 should be interpreted as HIGH levels, numeric inputs lower then 2,5 as LOW leves.

    Your compiled DLL file can be loaded in the property dialogue of the component. All imported functions and procedures are listed in the dialogue. The component will then appear in the circuit as it is defined in the DLL. To be conform with C-Compiler conventions, names of functions and procedure may begin with an underline character _ as well. For example _SimStart instead of SimStart.

    Compiling your own DLL project make sure that the linker option "Dynamic RTL" is disabled. Otherwise the DLL can not be loaded on systems without installed C++ environment.

    L'esempio che ho postato ieri e una counter realizzato dalla Abacom, ma a me servirebbe una DLL per salvare su disco in formato txt.
    Inserisco anche questo per darvi una ideaa però questa persona lo fa in Lua...
    http://www.askrprojects.net/software...lua/index.html

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    16
    Se dovesi scrivere questo codice, come lo devo inserire in base alle caratteristiche che chiede Abacom per creare un loro componente?
    Ringrazio saluti

    codice:
    program fileTesto;
    VAR T:text;
    begin
    assign(T,'c:\pippo.txt');
    rewrite(T);
    writeln(T,'ecco una prova');
    close(T);
    end.
    Uso Dev-Pascal

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    16
    Delphi: function NumInputs: Byte;

    Delphi: function NumOutputs: Byte;

    Delphi: function InputName(Channel: Byte): ShortString;

    Delphi: function OutputName(Channel: Byte): ShortString;

    Delphi: Procedure Calculate(PInput,POutput,PUser: PDLLParams);

    Delphi: Procedure CalculateEx(PInput,POutput,PUser: PDLLParams; PStrings: PStringParams);

    Delphi: Procedure SimStart(PInput,POutput,PUser: PDLLParams);

    Delphi: Procedure SimStop(PInput,POutput,PUser: PDLLParams);

    Delphi: Procedure Configure(UserValues: PDLLParam);

    Non so come creare la DLL con Dev-Pascal e inserire il codice da me creato dentro queste funzioni di Abacom per salvare su disco C:\
    Ringrazio per l'attenzione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.