Home AMX User Forum NetLinx Studio

Communication between module and mainLine

Can I call a function defined in the main program inside a module?
How?
Thanks
alex

Comments

  • ColzieColzie Posts: 470
    From the module use send_string to send text to your module's virtual device. In your main program check the string: event for the virtual device. Something like this:

    MODULE
    send_string vdv_MODULE, 'FUNCTION_NAME'
    

    MAIN PROGRAM
    data_event[vdv_MODULE]
    {
      string:
      {
        if(data.text = 'FUNCTION_NAME')
        {
          fn_function_to_execute ()
        }
      }
    }
    

    Similar, but reversed, use send_command to send data TO a module FROM the main program.
  • If the routine you want to call is doing something minor and generic then put it in an include and include it in both the module and the mainline along with all the other routines that you collect over the years. My iStandardFunctions.axi is almost 4000 lines long. "But that would use up so much space!" I hear someone saying. I haven't run out yet.
  • viningvining Posts: 4,368
    eddymouse wrote:
    Can I call a function defined in the main program inside a module?
    The simple answer is no! The complicated answer is yes!

    If you mean a direct call from with in a module of a function that is not part (inside) of the module then no. Just putting the function call in the module will cause it to not compile because it is not defined with the scope of the module and can't be passed as a parameter to it.

    Simple remedy is to copy and paste the needed function into the module or do it as an include which is affectively the same. Now this is fine providing what you want the function to act upon (devices or variables) are defined, reference or passed to the module. Sometimes that's not all that practical to do so you may need to do what Colzie suggests and create a virtual in the main code and pass that virtual definition as a parameter to the module and then create a data_event for that virtual in both the module and main code and pass what ever you want back and forth. Of course this would not be a direct function call but a string that you would look for to initate the function call as well as any parameters you want the function to work with from the module.

    Confused yet?
  • Thanks a lot! It Works!
    ciao
    Alessandro
Sign In or Register to comment.