From C to DLL

This is a discussion on From C to DLL within the C Programming forums, part of the General Programming Boards category; I have written a code for maximum power point tracking. The purpose was to create DLL file and use it ...

  1. #1
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15

    From C to DLL

    I have written a code for maximum power point tracking. The purpose was to create DLL file and use it in a circuit base simulation program. I used Microsoft visual c++ 2008 express and created that DLL file but I only get first value from the created array in C. I mean circuit based simulation program only get first value (although I see all array values after executing in "ChSciTE"). Do I need to store data somewhere? I am kind of rookie about writing C code? The code is below and the algorithm is attached as pdf. If anyone help me I would be so happy... I have been playing with this for 3 weeks...

    Code:
    #include <math.h>
    
        main() {
    
    	
        static double y[21];                       //controlled variable
        static double m_A[21];              //modulation index          
        double dm_A=0.05;                   //small change for modulation index
        int n;
        double i_cell[]={4.65, 4.68, 4.7, 3.7, 2.1, 2.8,4.68, 4.65, 3.9, 3.7, 2.1, 2.8,4.68, 4.65, 3.9, 4.3, 2.1, 3.3, 4, 3.7,4.6}; 
    	
            
            
            for (n=1;n<=19;n++){
            
            m_A[0]=0.75;
            m_A[1]=0.65;
                
            y[n-1]=i_cell[n-1]*m_A[n-1];                 //estimated initial controlled variable
            y[n]=i_cell[n]*m_A[n];  
    
        if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) ||    //algorithm for maximum power point tracking
            (((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
                       
            m_A[n+1]=m_A[n]+dm_A;           //increase modulation index
                                             
        else                                //while one of them is increasing and the other one decreasing
             
            m_A[n+1]=m_A[n]-dm_A;           //decrease modulation index  
      
     
            printf("&#37;f \n",m_A[n+1]);       //display modulation indices
                
                            }
                }
    The code above for seeing results in "ChSciTE" program but the one I pasted below creates that DLL file to use in simulation.

    Code:
    #include <math.h>
    __declspec(dllexport) void simuser (t, delt, in, out)
    double t, delt;
    double *in, *out;
    {
    
    
    	static double y[21];				//controlled variable
    	static double m_A[21];				//modulation index
    	double i_cell=4.65;			       //estimated initial current value
    	int n;
    		
    		
    	for (n=1;n<=19;n++){
    		m_A[0]=0.75;					//estimated initial current value
    		m_A[1]=0.65;					//second estimated initial current value
    	
    		y[n-1]=i_cell*m_A[0];			//estimated initial controlled variable
    		y[n]=in[0]*m_A[n];				//in[0] current input value which comes from simulation 					
    
    	
    	if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) || 
    		(((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
                
    		m_A[n+1]=m_A[n]+in[1];			//in[1] which is input 2 from circuit based simulation
    	
    	else								//while one of them increases and the other one decreases
            m_A[n+1]=m_A[n]-in[1];			//decrease modulation index
    
    	out[0]=m_A[n+1];					// out[0] is output which is transfered to the simulation with DLL
    	
    						}		
    		 	
    
    }
    Attached Images Attached Images
    Last edited by gurhan; 05-04-2008 at 07:36 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why not continue here C gives funny results
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15
    I also added there before. Sorry for any inconvenience.

  4. #4
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15
    in[0]: first input comes from simulation program
    in[1]: second input comes from simulation program
    out[0]: output from C code or DLL file goes to simulation or taken by simulation program

    Those values taken by simulation should be m_A[n+1]? As far as I understand which is an array. However only the first, I mean m_A[1+1] = m_A[2] has taken by simulation. How could I write the code all values from m_A[2], m_A[3]........m_A[20] can be taken by simulation or DLL sends those values to simulation program one by one for every 20 ms or different interval...

    C gives funny results

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by gurhan View Post
    in[0]: first input comes from simulation program
    in[1]: second input comes from simulation program
    out[0]: output from C code or DLL file goes to simulation or taken by simulation program

    Those values taken by simulation should be m_A[n+1]? As far as I understand which is an array. However only the first, I mean m_A[1+1] = m_A[2] has taken by simulation. How could I write the code all values from m_A[2], m_A[3]........m_A[20] can be taken by simulation or DLL sends those values to simulation program one by one for every 20 ms or different interval...

    C gives funny results
    Code:
    __declspec(dllexport) void simuser (t, delt, in, out)
    double t, delt;
    double *in, *out;
    Wrong. Or well, old. Does visual studio not cry havoc when you compile that?

  6. #6
    Registered User gurhan's Avatar
    Join Date
    Apr 2008
    Posts
    15
    Code:
    __declspec(dllexport) void simuser (double t, double delt, double *in, double *out)
    vart suggested to use this code. Old one and this one are working in vc++ and devc++. Any idea why I can not send all array values to DLL?
    Last edited by gurhan; 05-05-2008 at 08:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 08:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 03:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 01:34 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 06:24 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21